php / pecl-networking-gearman

PHP wrapper to libgearman
https://pecl.php.net/package/gearman
Other
33 stars 24 forks source link

How to start a worker on reboot #29

Closed tree28 closed 3 months ago

tree28 commented 3 months ago

Hello, I'm new to Gearman. I've been able to write worker and client PhP scripts that successfully execute tasks.

Background: I have also been trying to implement crontab on my gearman server so that the worker php scripts are loaded automatically on restarting the machine.

ie. crontab -e @reboot php /var/www/scripts_gearman/worker_one.php @reboot php /var/www/scripts_gearman/worker_two.php

Observation (Using crontab) While the scripts do load via crontab (see initial status) they are not stable. For example after running the client (ie. client_one) for workerone the worker produces no result_ and upon reviewing its status (see reviewed status) no available workers are report.

Reboot (crontab initializes gearman workers)

Initial status (on reboot): gearadmin -h 127.0.0.1 -p 4730 --status one 0 0 1 two 0 0 1

client one now called (triggering a worker one process)

Reviewed status (after client one called): gearadmin -h 127.0.0.1 -p 4730 --status one 0 0 0 two 0 0 1

Observation (using tutorial example) The getting started tutorial here shows the php cli method for running a worker. This method is stable. For example after running the same client (ie. client_one) for workerone the worker successfully produces a result_ and upon reviewing its status the worker continues to be available.

Initialize worker one php /var/www/scripts_gearman/worker_one.php &

Initial status (after initializing worker one): gearadmin -h 127.0.0.1 -p 4730 --status one 0 0 1 two 0 0 1

client one now called (triggering a worker one process)

Reviewed status (after client one called): gearadmin -h 127.0.0.1 -p 4730 --status one 0 0 1 two 0 0 1

Question: Is the crontab behavior expected? How can gearman workers be run correctly from crontab? What is the best way to initialize a worker on restarting a machine? Note the machine is a fresh ubuntu 24.04 server.

ngmlabs commented 3 months ago

Hello,

Running your workers in supervisord or as systemd unit files would be more appropriate than running them as cron jobs.

tree28 commented 3 months ago

@ngmlabs Sincerely appreciate the tip.

I found some examples here

If there are any other references you can recommend they'd be appreciated.

ngmlabs commented 3 months ago

Running processes in supervisord or in systemd has nothing to do with Gearman.

You can follow a tutorial like this one to create a systemd unit file for each of your workers. The only thing that I would change there is the user that runs the script - you should run the scripts as any other user (www-data for example), but not root.

tree28 commented 3 months ago

@ngmlabs,

Thanks for the reference. The instructions well successful! For anyone looking for a recipe here is the code I decided to use:

[Unit]
Description=MyGearmanWorker
Requires=memcached.service mysql.service
After=network.target
[Service]
User=www-data
Type=simple
ExecStart=/usr/bin/php /var/www/MyGearmanWorker.php
Restart=always
[Install]
WantedBy=default.target