#Laravel Envoy and Continuous Worker Operation

3 messages · Page 1 of 1 (latest)

cinder raven
#

Hello everyone,

I'm facing an issue while trying to use Laravel Envoy.
Currently, I'm working on a Laravel project (distinct from the Envoy project) where I manage a series of jobs through the worker. These jobs are generated every X minutes 24/7, so I need the worker to be active at all times, even in my absence. I'm considering handling this through an Envoy task, in the following way:

@servers(['localhost' => '127.0.0.1'])

@task('restart-queues', ['on' => 'localhost', 'confirm' => true])
    cd /project/folder

    php artisan queue:restart
    nohup php artisan queue:work --queue=high,default --timeout=5000 &

@endtask

Additionally, I have a fully functional code; however, I need a trigger to determine if the worker has stopped for any reason. I want to avoid closing and reopening a new worker every time I launch this Envoy task. So, I came up with the idea of trying to manage a conditional statement based on the output of 'ps aux'. Unfortunately, the following code returns two different results depending on where it is launched. When executed through Laravel task and Envoy, the outputs are different compared to running the same command directly from the terminal. In the first case, I receive '3' as a result, and in the second, '1'. This seems to be happening because running it through Laravel task appears to concatenate my task text, causing 'ps aux' to return different values.

@servers(['localhost' => '127.0.0.1'])

@task('restart-queues', ['on' => 'localhost', 'confirm' => true])
     cd /project/folder

    n=$(ps aux | grep -e 'php artisan queue:work --queue=high,default' | wc -l)
    
    echo $n
    
@endtask

I don't know if this is the best approach to handle this issue; other features/methods are welcome!

Any insights or suggestions on how to resolve this discrepancy would be highly appreciated!

Thank you very much 🙂

twin ember
#

I don't know much about Envoy, but shouldn't the workers be managed by Supervisor in stead of starting them manually? Supervisor can automatically restart workers if they die.

#

Detecting whether the worker has stopped can be done in lots of ways. I usually create a HeartbeatJob that's dispatched from the scheduler every minute. When the job runs, it just writes the current time to Cache. Then I can have another Cron task somewhere that checks how old the latest heartbeat was, and send an alert or whatever.