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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Nov 28, 2022
Merged

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

merged 3 commits into from Nov 28, 2022

Conversation

mihaliak
Copy link
Contributor

@mihaliak mihaliak commented Nov 28, 2022

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.

None yet

2 participants