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

Multiple Schedule objects from one task-file #53

Open
gennadiylitvinyuk opened this issue Feb 6, 2023 · 5 comments
Open

Multiple Schedule objects from one task-file #53

gennadiylitvinyuk opened this issue Feb 6, 2023 · 5 comments

Comments

@gennadiylitvinyuk
Copy link

Description
It would be very helpful to have a possibility to return multiple different Schedule objects from one task-file.
One possible usage scenario is external schedule source for Schedules.

Possible implementations are simple array or a Schedule-collection object.

Example

use Crunz\Schedule;
//...
$schedules = []; //  new ScheduleArrayObject();
forearch ($externalDefinitions as $definition) {
  $schedule = new Schedule();
  // configure $schedule
  // ...

  $schedules[] = $schedule;
}

return $schedules;

@PabloKowalczyk
Copy link
Member

Hello, You can do it already, but with multiple events:

$commands = [
    [
        'command' => 'php -v',
        'cron' => '* * * * *',
    ],
    [
        'command' => 'php -v',
        'cron' => '* * * * *',
    ],
];

$schedule = new Schedule();
foreach ($commands as $command) {
    $task = $schedule->run($command['command']);
    $task->cron($command['cron']);
}

return $schedule;

@ivankuraev
Copy link

Hello, You can do it already, but with multiple events:

$commands = [
    [
        'command' => 'php -v',
        'cron' => '* * * * *',
    ],
    [
        'command' => 'php -v',
        'cron' => '* * * * *',
    ],
];

$schedule = new Schedule();
foreach ($commands as $command) {
    $task = $schedule->run($command['command']);
    $task->cron($command['cron']);
}

return $schedule;

Hello.
This doesn't work with preventOverlapping().
Only the first event is executed.

@PabloKowalczyk
Copy link
Member

@ivankuraev could You post some code to reproduce?

@ivankuraev
Copy link

$schedule = new Schedule();

$onDemandTasks = Tasks::instance()->getActive('ondemand');
foreach ($onDemandTasks as $onDemandTask) {
    $task = $schedule->run(function() use ($onDemandTask) {
        switch ($onDemandTask->command) {
            case 'cmd1':
                ToolsOne::instance()->run();
                break;
            case 'cmd2':
                ToolsTwo::instance()->run();
                break;
            // .....
        }
    });

    $task
        ->everyMinute()
        ->description($onDemandTask->title)
        ->preventOverlapping()
        ->appendOutputTo(sprintf('logs/ondemand_%s.log', $onDemandTask->command))
        ->after(function() use ($onDemandTask) {
            Tasks::instance()->toggleActiveStatus($onDemandTask->id);
        })
    ;
}

return $schedule;

It's a little problematic to give complete working code to run it.
There are tasks that can be launched periodically.
If we have, for example, 3 tasks to run.
When using preventOverlapping(), only one task is performed.
If you don't specify preventOverlapping(), all three are executed simultaneously.

@PabloKowalczyk
Copy link
Member

It happens because both commands generate same hash for preventOverlapping, try not using closure.

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

3 participants