E
E
Eugene2012-03-15 15:19:53
PHP
Eugene, 2012-03-15 15:19:53

Sending sms packages: run php script every 5-10 seconds

For the internal service, we decided to attach sending SMS: according to a rough estimate, they will be accumulated and sent in batches of 100-200 pieces in 2-3 hours using http requests to the SMS gateway.
To simplify the scheme, we separate directly queuing messages (mysql database), sending them in the background, and checking statuses.
How can you check every 5-10 seconds for messages with a php script and start a long (up to a couple of minutes) sending process?
Can you please tell me if the above approach is correct?

ps: why often? - Efficiency is needed, and the minute period of cron is too large.
ps: and probably an offtopic question, how do SMS gateways work - they can’t immediately send each request received to the operator using the smpp protocol, or am I mistaken?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Melkij, 2012-03-15
@melkij

Daemon?
Simple while(true) { check if there are messages in the queue, if there are - exec the mailing script; sleep(10)}
Don't forget to just unbind stdin and stdout, otherwise exec will wait for the end of the script.
Well, in the cron, you can stick a check on whether the daemon is running and run it if not.

S
storms, 2012-03-15
@storms

I would use Gearman here. This is a server for organizing and distributing tasks, or, more simply, a message queue server.
You can read about it, for example , here
It will work like this:
A script that initiates sending SMS:

# Подключаемся к серверу
$client= new GearmanClient();
# Регистрируем задачу для фонового выполнения
# "sendsms" - это тип задачи
# $sms - это данные письма
$result = $client->doBackground("sendmail", serialize($sms));

At this stage, your SMS will be added to the queue and the script continues to execute, and as soon as one of the senders (workers in the Gearman terminalogy) becomes free, it will be delivered.
In this case, all the rough work of storing SMS in the queue, in order of sending, will be taken over by a third-party application

A
avalak, 2012-03-15
@avalak

> Tell me, is the above approach correct in principle?
I would implement differently. Redis pubsub for the queue + Node.js (you probably prefer phpDaemon) + database if you need to log. supervisord to monitor the service.

M
markoffko, 2012-03-15
@markoffko

You can code like this, for example (every 15 seconds):
*/1 * * * * root /home/mybin/script.sh; /bin/sleep15; /home/mybin/script.sh /bin/sleep15; /home/mybin/script.sh /bin/sleep15; /home/mybin/script.sh
Or do
exec('myscritp');
sleep(10);
exec('myscript');

W
WEBIVAN, 2012-03-15
@WEBIVAN

ps: and probably an offtopic question, how do SMS gateways work - they can’t immediately send each request received to the operator using the smpp protocol, or am I mistaken?

Well, why, I don’t see any problems in instant sending via smpp.
Concerning implementation, I do not see what for here to use base in general. Run a php daemon that listens on an arbitrary port. You send messages to the same port and queue them inside php itself (say, in an array), as soon as the number of messages reaches the required number, send them.
Ideally, you will need libevent so that you do not pull sockets in an eternal loop and pcntl for forks, so as not to call background jobs through exec and others.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question