R
R
Ruslan Ibragimov2018-11-03 02:18:16
Task Schedulers
Ruslan Ibragimov, 2018-11-03 02:18:16

How to start a Beanstalk queue with Cron?

the queue is needed to send emails to users, notifications, etc.
if I set the cron task to run the queue handler script, for example, every minute, then I can run several processes in parallel, since at some point there may be a large number of tasks in the queue and they simply will not have time be executed until another process starts,
and it may be that two processes pull the same task until it is removed from the queue
. Is it possible to somehow run this script sequentially, that is, preferably without any delay at all, and as soon as one is completed, the next one starts immediately?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Ibragimov, 2018-11-03
@RuslanIbragimov

did so. I had to make a delay of 5 seconds inside the php script because systemd cursed that I had to restart the process too often

[Unit]
After=network.target
Description=Runs worker
[Service]
ExecStart=/usr/bin/php7.0 /vagrant/www/public/deployer/app/cli.php
PIDFile=/vagrant/www/public/deployer/app/cli_bean.pid
TimeoutStopSec=300
Restart=always
[Install]
WantedBy=multi-user.target
Alias=bean.service

task code in cli falcon
use Phalcon\Cli\Task;

class MainTask extends Task
{
    public function mainAction()
    {
        $this->queue->watch('mail');
        while ($this->queue->statsTube('mail')["current-jobs-ready"] > 0 && ($job = $this->queue->reserve())) {
            $message = $job->getBody();
            $mailer = $this->mailer->send($message['to'], $message['subject'] , $message['view'], $message['params']);
            $job->delete();
        }
        $this->mailer->close();

        sleep(5);
    }
}

systemctl status
beanworker.service - Runs worker
   Loaded: loaded (/etc/systemd/system/beanworker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-11-03 20:21:05 MSK; 1s ago
 Main PID: 23075 (php7.0)
    Tasks: 1
   Memory: 9.4M
      CPU: 87ms
   CGroup: /system.slice/beanworker.service
           └─23075 /usr/bin/php7.0 /vagrant/www/public/deployer/app/cli.php

Nov 03 20:21:05 vagrant systemd[1]: beanworker.service: Service hold-off time over, scheduling restart.
Nov 03 20:21:05 vagrant systemd[1]: Stopped Runs worker.
Nov 03 20:21:05 vagrant systemd[1]: Started Runs worker.

 memory eats up almost 10mb, and php memory_get_peak_usage gives out 2mb

W
wol_fi, 2018-11-03
@wol_fi

It is possible to make an infinite process without cron via pcntl_fork();
You start the process, it processes the task from the queue, gives birth to a child process, and closes itself. The child process repeats this entire circle. There are a lot of ready-made implementations on the network.
Pros: pure php, no cron required, no memory leak.
Cons: It is very difficult to synchronize several of these processes with each other.
You can use a task queue, for example the same rabbitmq
Pros: it is easy to synchronize the processes of workers, workers can be written on anything
Cons: plus one technology in the stack

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question