Skip to content

[7.x] Add "every 2 / 3 / 4 minutes" methods to scheduler#33379

Merged
taylorotwell merged 1 commit into
laravel:7.xfrom
SjorsO:cron-gran
Jun 29, 2020
Merged

[7.x] Add "every 2 / 3 / 4 minutes" methods to scheduler#33379
taylorotwell merged 1 commit into
laravel:7.xfrom
SjorsO:cron-gran

Conversation

@SjorsO

@SjorsO SjorsO commented Jun 29, 2020

Copy link
Copy Markdown
Contributor

This PR adds the following methods to the scheduler:

  • everyTwoMinutes()
  • everyThreeMinutes()
  • everyFourMinutes()

These new methods go alongside the currently available everyMinute and everyFiveMinutes methods.

I need these methods for an application that syncs data from a legacy system. We have to sync the data "in real time" (as in, so that it feels like "real time"). Using everyMinute seems like overkill, and might overload their server. On the other hand, everyFiveMinutes doesn't feel "real time" enough. Being able to schedule the sync every 2 or 3 minutes would be perfect.

Scheduling jobs that should run very often is a common use-case. These new methods give developers more choice in exactly how often.

@browner12

Copy link
Copy Markdown
Contributor

I think you may be better off running a custom expression for this:

->cron('* */2 * * *');
->cron('* */3 * * *');
->cron('* */4 * * *');
php

@thomasm789

thomasm789 commented Jun 29, 2020

Copy link
Copy Markdown
Contributor

I agree with browner12. Although it wouldn't necessarily be an issue including more methods. Your immediate problem can be resolved using a custom cron schedule.

https://laravel.com/docs/7.x/scheduling#schedule-frequency-options

@devcircus

Copy link
Copy Markdown
Contributor

I believe this is macroable as well

@SjorsO

SjorsO commented Jun 29, 2020

Copy link
Copy Markdown
Contributor Author

As @browner12 said, you can currently achieve the same result by using a custom cron expression. But considering how cryptic the cron expression syntax is, you should probably always add a comment so the rest of your team can understand what's going on:

$schedule->job(SyncSomething::class)->cron('*/2 * * * *'); // every 2 minutes

// or, with this PR

$schedule->job(SyncSomething::class)->everyTwoMinutes();

@taylorotwell taylorotwell merged commit 5c841b3 into laravel:7.x Jun 29, 2020
@oliverquynh

Copy link
Copy Markdown
Contributor

I think too many proxy methods don't make sense and the framework core code becomes more and more giant.

@ragingdave

Copy link
Copy Markdown
Contributor

Boy I wish I would have seen this, rather than adding all those proxy methods, I would have suggested using everyMinute with a param that could optionally change the frequency instead. That way everyTwoMinutes could have just been everyMinute(2) instead of all these added methods...

@SjorsO

SjorsO commented Jul 1, 2020

Copy link
Copy Markdown
Contributor Author

Boy I wish I would have seen this, rather than adding all those proxy methods, I would have suggested using everyMinute with a param that could optionally change the frequency instead. That way everyTwoMinutes could have just been everyMinute(2) instead of all these added methods...

See #33380

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.

7 participants