Skip to content
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 a worker process #26

Closed
lokhman opened this issue Oct 26, 2017 · 2 comments
Closed

Run as a worker process #26

lokhman opened this issue Oct 26, 2017 · 2 comments

Comments

@lokhman
Copy link
Contributor

lokhman commented Oct 26, 2017

First of all, thank you for a fantastic library!

My question is related to running the cron scheduler as a so called "clock" process in Heroku. Heroku instances don't have crontab. In order to start cron:run every minute, you need to have a kind of worker "daemon" process which never dies. One possible example of such process is here: https://devcenter.heroku.com/articles/php-workers.

Is there any possible way of starting cron:run as a worker process? If there is none, will it be hard to add this functionality, as it will be very helpful in such services as Heroku.

Thanks!

@NoUseFreak
Copy link
Contributor

Hi @lokhman! Thanks for using and trusing this project :-).
At the moment it's not possible to run this in daemon mode. I would love to add this as I like the feature! Maybe there are already other projects allowing you to get this behaviour but it feels like something this project can provide.

I would suggest implementing a -d flag on cron:run to make it start in daemon mode.
As my schedule is really packed at the moment, feel free to implement it.
Maybe https://reactphp.org/event-loop/ is a good starting point to get this working.

Feel free to ask any questions you want answered. I'll try to get back to you quicker than I did just now.

@lokhman
Copy link
Contributor Author

lokhman commented Nov 13, 2017

Thank you, @NoUseFreak, for the reply!

As this feature was not available in the library and I needed a fast solution to embed one in the project, I wrote a very simple console script, which so far works very well:

#!/usr/bin/env php
<?php

use Symfony\Component\Process\Process;

umask(0000);
set_time_limit(0);

require __DIR__.'/../vendor/autoload.php';

$process = new Process(__DIR__.'/console cron:run');
$process->setTimeout(0);

while (true) {
    $now = microtime(true);
    usleep((60 - ($now % 60) + (int) $now - $now) * 1e6);

    if (!$process->isRunning()) {
        $process->start();
    }
}

This behaves like a synchronous crontab, which runs cron:run command exactly every minute (0 * * * *). Saying "synchronous" I mean that if a command gets executed for more than a minute, it will skip the next execution. In my project this will never happen, although making it asynchronous should not be a big problem.

I was also looking at event-loop library, but thought that probably for this kind of task utilising a full-featured event extension is a bit of an overkill, which will require an extra dependency. Component symfony/process has enough functionality to execute commands asynchronously, while making a simple queue should be a trivial task. What do you think? Is there anything I'm missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants