Reputation: 2143
The specific scenario is this: a server running beanstalkd, using the PHP libary Pheanstalk. I have a PHP worker running as a service, so it theoretically never stops running.
The initial code for the worker is just a function call made to do the work. I wanted to shift that over to OOP. This would instantiate an object for every job that was processed. I imagine that without proper clean up, this would create a memory leak.
What would be the proper way to create/manage/destroy objects for use in this kind of scenario without causing memory leaks or excess system usage?
Upvotes: 0
Views: 159
Reputation: 35169
I've run hundreds of millions of jobs through PHP workers, with SES, and Beanstalkd as the queue systems. I didn't worry about trying to keep things running forever. If you find that after a job, memory use is getting high, restart the worker. Likewise, if you've just completed your 100th or 1000th job with that worker, restart from scratch, just to clean up.
It's easy to run more workers, and it's fast to start a new one. Use it. If, while developing, plug in enough debugging so if you do find a memory leak, there's enough information there to figure out where, and then deal with it.
This is the shell script I have to keep the PHP worker going. When I exit(98);
from the script, it recognises this and immediately restarts. I'll usually add others for planned pauses and to exit the script. Start this with whatever init-style system you may have (upstart, supervisord, etc) and the script will keep going, and restarting at will, until you decide otherwise.
#!/bin/bash
# runBeanstalkd-worker.sh
# a shell script that keeps looping until an exit code is given
# if it does an unplanned exit, restart after a second - or if it's
# some other declared error.
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script
nice php -q -f ./cli-beanstalk-worker.php -- $@
ERR=$?
if [ $ERR -eq 98 ]
then
# a planned restart - instantly
exec $0 $@;
fi
# unplanned exit, pause, and restart
sleep 10
exec $0 $@
Upvotes: 0