jdavidbakr / MultiServerEvent

Laravel plugin to allow scheduled events across multiple servers with the same scheduler to not overlap.
MIT License
37 stars 17 forks source link

Lock cleared before task exits! #6

Closed paras-malhotra closed 7 years ago

paras-malhotra commented 7 years ago

We need to add tests to this package - it's not working properly!

Try $schedule->command('inspire')->withoutOverlappingMultiServer()->everyMinute(); by adding a sleep in Inspire like so:

public function handle()
 {
        $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
    sleep(120);
    Log::info('ran cron job inspire now');
 }

You will find that the withoutOverlappingMultiServer doesn't work. In the laravel log, you would find Inspire running every minute (not every 2 minutes) despite the locks!

The reason?

public function run(Container $container)
 {
      parent::run($container);
      $this->clearMultiserver();
 }

What's happening is that the lock is clearing before the task completes!

The lock clears before the task completes because if there are no before/after callbacks, the task runs in the background by triggering runCommandInBackground(). Then clearMultiServer is called and the lock is cleared before the task exits.

A quick fix is modify the Event.php to:

public function run(Container $container)
 {
      $this->runCommandInForeground($container);
      $this->clearMultiserver();
 }

Hope this helps!

jdavidbakr commented 7 years ago

Fixed in release 2.0. Note this also requires Laravel 5.4.

You should be able to avoid this issue in version 1.0 by keeping runInBackround set to false.