-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run as daemon with workers #134
Comments
Ok, so this is very interesting idea. Here are my thoughts: Basically we have 4 different options:
We do have a nice PHP library that comes with a OOP interface and some nice feature on top of the pcntl_* function which unfortunately is called "Threadi" (but basically it does forking, not threading): https://github.com/AOEpeople/Threadi - Wrapped in a Magento module: https://github.com/AOEpeople/Aoe_Threadi While this works and we've been using this for indexers and importer this is not an ideal/generic solution:
With threading we'd most likely run into similar issues (never tried threading in PHP). Running all tasks in a single process will most likely run into memory issues very quickly. That leaves us with running every job in a separate process, and since the overhead of starting a new process is acceptable I think this is the way to go. So the first step could be to introduce an action in the Aoe_Scheduler script that will run an individual task by id. That shouldn't be a big deal and could be a handy action for manual debugging. Looking at the daemon portion of this idea we'd face following challenges:
Since the actual goal that we're trying to accomplish here is running tasks in parallel and reducing the wait time for overdue tasks (e.g. being used as queue - which is the case for the EE indexers) my suggestion is to simply add a "daemon" mode to the existing scheduler script which would result in the process not to stop, but to keep looking for new jobs instead in a loop (making sure to have a minimum time span between database polls). So I think this could be a solution (and some documentation to help people understand what's going on and set it up): Running an individual (existing) schedule Run schedules in individual processes Run in daemon mode (don't stop, but go back and poll schedules) And since But this should still be a separate option since people might prefer not run it in daemon mode but still in separate processes. Also this minimalistic approach would allow allow you to continue using the cron_group concept to have separate pools of workers for different cron tasks (you might want to configure your setup to run them on different server instances). So the daemon mode basically would be an endless loop polling the database, launching new processes ( Using the word "workers" here might be confusing since it implies that it's the same worker process that will be assigned a new schedule. But really the worker will simply stop and the daemon process will start a new one. Since starting a new working might be overkill for some of the tiny cron tasks you could configure this behavior only for some jobs (by whitelisting or blacklisting the codes) using the cron-group mechanism. |
Btw, Laravel has a very similar concept when it comes to processing queues (which basically is what we're doing here as well): |
One issue I see with It might be better to I also wanted to mention that the daemon can launch/replace the watch dog. I say replace because the daemon will always be "watching" after all. On the note about a daemon not being a good fit for everyone, I agree. The current cron behavior should still work. One can choose to use the daemon option instead. Documentation should be added to make that clear. |
Thanks for sharing that link, Fabrizo! Have you used the laravel system We run Magento on Zend Server and I've been playing around with an adapter I was endearing if anyone has worked on adapting the Scheduler to a Zend
On Thursday, July 9, 2015, Fabrizio Branca [email protected] wrote:
Sent from Gmail Mobile |
This might be a bit unicorny (ctrl+f Unicorny) of a feature but I would like to discuss it.
Is it possible to run cron schedules through a daemon rather than a cron job? For instance, the daemon is started and runs with 10 or so workers. The first 10 cron jobs to be processed this minute are selected and ran as 10 separate threads. Once one (or more) completes, the daemon takes the next 10 - running schedules and adds them to the queue.
The advantage of this is that several jobs can run asynchronously instead one after the other. A long running job can hold back others in the queue. The is something currently handled by Aoe_Scheduler with cron groups, but it requires setup. A daemon would require less setup.
There is the risk of not knowing the daemon is running. For instance, after a server restart cron starts up again without action. It would be easy to forget to restart the daemon, though it could be configured to start on boot.
I once attempted to build such a solution, but soon realized it would be a tougher task than I originally thought. I wouldn't be able to use the
default
andalways
event dispatchers. Logic would be needed to select schedules for this minute (or older), locate theirmodel/name::method()
, and run them independently. This is a very different approach from the default logic.It looks like the event dispatches are being used here too, but perhaps it's something that could be worked in? It would be similar the scheduleNowAction().
The text was updated successfully, but these errors were encountered: