Replies: 5 comments 8 replies
-
And it's a bit inconsistent
but we already have that. |
Beta Was this translation helpful? Give feedback.
-
I had the same problem. My solution is the roadrunner service. First I install an extension package <?php
declare(strict_types=1);
namespace App\Command;
use App\Libraries\Core\Yii;
use Jobby\Jobby;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'scheduler',
description: '定时任务',
hidden: false,
)]
final class SchedulerCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$jobby = new Jobby();
$this->registerTasks($jobby);
while (true) {
$waitTime = 60 - (time() % 60);
sleep($waitTime);
$jobby->run();
}
}
private function registerTasks(Jobby $jobby)
{
$yii = Yii::aliases()->get('@root/yii');
$jobby->add('HelloCommand', [
'command' => "php {$yii} hello",
'output' => Yii::aliases()->get('@runtime/logs/cron/hello.log'),
'enable' => true,
'schedule' => '* * * * *',
]);
}
}
then add a cron service in rr.yml: service:
# 定时任务
cron:
service_name_in_log: true
command: "php php/yii scheduler:run"
timeout_stop_sec: 10 Isn't it more flexible to control how often timed tasks are executed in php? |
Beta Was this translation helpful? Give feedback.
-
One thing that worries me about having cron jobs in RR is that RR is supposed to be scaled horizontally meaning ran as multiple replicas, so you might be better off running a dedicated instance with crond, go-cron, or what have you. |
Beta Was this translation helpful? Give feedback.
-
Using crond or go-cron is better, but if the roadrunner has multiple instances, there is still a problem. So the most thorough timing task data should be stored externally, such as in a database or cache, and then locked to avoid repeated execution, but for simple cases, the implementation in php is quite sufficient. |
Beta Was this translation helpful? Give feedback.
-
I think we should implement a special scheduled task plugin, which is actually very general purpose. For example, in our application, users can create a schedule to send regular emails to a specified group of customers at 9 o 'clock every day. These scheduled tasks can be dynamically configured, rather than written in crontab. Of course, this can be done by writing a while (true) loop in php, and then monitoring and managing the process through roadrunner's service. At the php level, there are three ways to do this: If this is done by a roadrunner, it can be done:
|
Beta Was this translation helpful? Give feedback.
-
It can be a new plugin like a service
example:
Beta Was this translation helpful? Give feedback.
All reactions