Skip to content

Commit 9c96af2

Browse files
committed
Scheduled tasks
1 parent 9487fd3 commit 9c96af2

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

docs/.vuepress/sets/craft-cms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module.exports = {
6868
"extend/mail",
6969
"extend/migrations",
7070
"extend/models",
71+
"extend/scheduling",
7172
"extend/services",
7273
"extend/session",
7374
"extend/templates",

docs/6.x/extend/avenues.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ Instead of setting up an event listener to, say, register a field type,
127127
- `HasFrontendAssets``$vite`, `$styles`, `$scripts` — Configuration or path maps for various [asset publishing](assets.md) options.
128128
- `HasListeners``$events` — Bind [events](events.md) (keys) to listeners (values).
129129
- `HasPermissions``getPermissions()` (Array) — `CraftCms\Cms\User\Data\Permission` objects, with optional nesting.
130-
- `HasRoutes` → Loads [routes](http.md) defined in `routes/[web|cp|~~actions~~].php`.
130+
- `HasRoutes` → Loads [routes](http.md) defined in `routes/[web|cp|actions].php`.
131+
- `HasScheduling``schedule()` — Register [recurring tasks](scheduling.md).
131132
- `HasSettings``$hasCpSettings`, `$hasReadOnlyCpSettings` (Boolean) — Enables automatic routing to a dedicated [settings](config.md) screen in the control panel.
132133
- `HasTranslations``$t9nCategory`, `$sourceLanguage` — Customize the translation category. Defaults to your plugin handle.
133134
- `HasUtilities``$utilities` (Array) — Register utilities.

docs/6.x/extend/queue.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use MyOrg\Activity\Reporting\Manager;
1212
class GenerateReport extends Job
1313
{
1414
public function __construct(
15-
public int $reportId,
15+
public int $templateId,
1616
public bool $notifyOwner,
1717
) {}
1818

@@ -21,7 +21,7 @@ class GenerateReport extends Job
2121
Manager $manager,
2222
): void
2323
{
24-
$report = $manager->getReportById($this->reportId);
24+
$report = $manager->getTemplateById($this->templateId);
2525
}
2626
}
2727
```
@@ -50,3 +50,7 @@ A few other changes are worth noting:
5050
- `ttr` has been renamed `timeout`
5151
- Job are identified by UUIDs instead of IDs.
5252
- New kinds of jobs are available to projects that support them: consider implementing [chained jobs](laravel:queues#chains-and-batches), [collision-avoidance](laravel:queues#preventing-job-overlaps), or [parallelization](laravel:queues#job-batching)
53+
54+
## Scheduling
55+
56+
Jobs can also be [executed on a schedule](laravel:scheduling#scheduling-queued-jobs).

docs/6.x/extend/scheduling.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Scheduled Tasks
2+
3+
Your plugin can register recurring workloads with Laravel’s internal [scheduler](laravel:scheduling).
4+
5+
<!-- more -->
6+
7+
These tasks can be implemented as [commands](laravel:artisan), [jobs](laravel:queues), or plain closures.
8+
To configure a recurring task, define a `schedule()` method on your plugin’s base class:
9+
10+
```php
11+
protected function schedule(Schedule $schedule): void
12+
{
13+
// Send a test email every day:
14+
$schedule->command('craft:mailer:test')
15+
->daily();
16+
17+
// Queue a report at the top of every hour:
18+
$schedule->job(new GenerateReport(['templateId' => 1234]))
19+
->hourly();
20+
21+
// Report
22+
$schedule->call(function () {
23+
Log::info('Scheduler healthy!');
24+
})
25+
->everyTenMinutes()
26+
->thenPing(env('UPTIME_KUMA_SERVICE_URL'));
27+
}
28+
```
29+
30+
This `$schedule` instance is the same underlying class that Laravel’s documentation uses via a facade, so the APIs are identical.
31+
Refer to the [scheduling documentation](laravel:scheduling) for a complete list of capabilities and options.
32+
33+
## Running the Schedule
34+
35+
The scheduler requires regular invocation by the host; it does not run automatically.
36+
A typical CRON task would look something like this:
37+
38+
```
39+
* * * * * /usr/bin/env php /var/www/html/artisan schedule:run
40+
```
41+
42+
::: tip
43+
This expression is compatible with DDEV’s [CRON add-on](https://github.com/ddev/ddev-cron).
44+
:::
45+
46+
Some projects may not have a reliable schedule runner set up, so it’s important that you mention any reliance on scheduled tasks in your plugin’s installation instructions.

0 commit comments

Comments
 (0)