Webador / SlmQueue

Laminas / Mezzio module that integrates with various queue management systems.
Other
138 stars 57 forks source link

Updated docs about stopping the queues #252

Closed MarcinOrlowski closed 2 years ago

MarcinOrlowski commented 2 years ago

Documentation shows how to start the queue but I see no word about stopping the queue in the clean way. Can it be added please?

MarcinOrlowski commented 2 years ago

For clarification I went thru the docs and from sources I see that perhaps(?) I should extend DoctrineWorker and make it conditionally return ExitWorkerLoopResult. I am still digging there but but for now that looks bit like overkill and I suspect there's better approach. Any guidance is welcome.

basz commented 2 years ago

Register the InterruptStrategy and sent the running process a signal. Eg by pressing control+c.

Any running job will finish and then the worker will exit

MarcinOrlowski commented 2 years ago

Thanks, but this would work in my case. I forgot to mention that I run multiple my queues in background (started like vendor/bin/laminas slm-queue:start myqueue > /dev/null &) therefore CTRL+C is not usable. Also I need stopping to be scripted and I got limited possibilities on the production environment, i.e. I cannot send any signal via kill (or in general run anything via CLI but PHP scripts).

basz commented 2 years ago

Usually you would use some process manager like supervisor or docker to run/stop these.

Not sure how you would be able stop them if you run them in the background (with &).

Maybe write a strategy that looks for a special file and if present exit just like the interupt strategy.

MarcinOrlowski commented 2 years ago

I figured out clearer (I think) solution - I will inject a high priority job (so it will be picked up next) that only thing in execute() would be to return ExitWorkerLoopResult. That way should satisfy workers' loop and clearly terminate the worker

EDIT: I seem to misunderstood the docs and it's not job to emit ExitWorkerLoopResult. How to do that properly then?

MarcinOrlowski commented 2 years ago

While I still think the docs should be more clear I solved my problem now. So for future readers: the SLM defaults include InterruptStrategy and that strategy will tell the worker to stop if there's SIGTERM or SIGINT sent. So all is needed here to properly shut down the worker is ensure InterruptStrategy is involved (I just copied default config from vendor/slm/queue/config/slm_queue.global.php.dist) and tweaked it a bit leaving this strategy included) and then create a job that will send that signal like

class StopQueueWorkerJob extends AbstractJob
{
    public function execute() {
        \posix_kill(\posix_getpid(), SIGTERM);
    }
}

and then enqueue that job (with high priority) in the queue you want to stop.