1

I have a question about the task scheduling in laravel framework. I already have defined the commands currency:update and currency:archive in the list of console commands. Now, I want to run these two commands in the schedule method but with the following condition:

if this is a time to run the currency:archive command, don't run the other command currency:update until the previous command (i.e. currency:archive) ends; otherwise run the currency:update command.

This is my current code in schedule method:

$schedule->command('currency:archive')
         ->dailyAt('00:00')
         ->withoutOverlapping();

$schedule->command('currency:update')
         ->everyMinute()
         ->withoutOverlapping();

How should I modify it?

Thanks.

dparoli
  • 8,891
  • 1
  • 30
  • 38
Alireza
  • 129
  • 2
  • 9

2 Answers2

3

In the laravel schedule docs the following two features are mentioned:

  1. Truth test contraints docs
$schedule->command('emails:send')->daily()->skip(function () {
    return true;
});
  1. Task hooks docs
$schedule->command('emails:send')
         ->daily()
         ->before(function () {
             // Task is about to start...
         })
         ->after(function () {
             // Task is complete...
         });

You could consider setting a variable like $achriveCommandIsRunning to true in the before() closure. In the skip() closure you can return $archiveCommandIsRunning; and in the after() closure you can set the variable back to false again.

PtrTon
  • 3,705
  • 2
  • 14
  • 24
2

Thanks,

I modified my code as you said and it works now. Here is the final code:

$isArchiveCommandRunning = false;
$schedule->command('currency:archive')
    ->dailyAt('00:00')
    ->before(function () use (&$isArchiveCommandRunning) {
        $isArchiveCommandRunning = true;
    })
    ->after(function () use (&$isArchiveCommandRunning) {
        $isArchiveCommandRunning = false;
    })
    ->withoutOverlapping();

$schedule->command('currency:update')
    ->everyMinute()
    ->skip(function () use (&$isArchiveCommandRunning) {
        return $isArchiveCommandRunning;
    })
    ->withoutOverlapping();
Alireza
  • 129
  • 2
  • 9