Skip to content

Add callback to resolve custom mutex name of schedule events - #45126

Merged
taylorotwell merged 3 commits into
laravel:9.xfrom
mihaliak:9.x
Nov 28, 2022
Merged

Add callback to resolve custom mutex name of schedule events#45126
taylorotwell merged 3 commits into
laravel:9.xfrom
mihaliak:9.x

Conversation

@mihaliak

@mihaliak mihaliak commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

Hello 👋

we have created schedule tasks monitoring and history module, we are passing uuid to every schedule task (this uuid is different on every server every time) to identify specific runs.

We are using withoutOverlapping and onOneServer but we had some issues since mutex name of scheduled tasks are generated from full command string. This may be okay for hardcoded scheduled tasks in kernel but not so good for dynamically created scheduled tasks (ie via database) while attaching different parameters to commands. (We want to share mutex / lock between those cron runs on multiple servers.)

I have added simple method to allow developers to use custom defined mutex name of each of scheduled tasks.

This could be done by using preventOverlapsUsing and creating new implementation of Illuminate\Console\Scheduling\EventMutex interface, however this is not really necessary since we do not want to change logic how mutex is created, forgotten etc. We just want to change name of mutex (key).

This can be also used for blocking one scheduled command until other command is finished.

Example usage:

$schedule->command('do-something', ['--uuid' => 'b4d5f926-e1b5-4285-b068-cf1d623a0635'])
         ->description('Do something important.')
         ->everyFiveMinutes()
         ->onOneServer()
         ->withoutOverlapping()
         ->createMutexNameUsing(fn (Event $event) => 'schedule-task-'.sha1($event->description));

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

Successfully merging this pull request may close these issues.

2 participants