Laravel Cloud 来了!为 Laravel 应用提供零配置的托管基础​​设施。 立即部署

Model-Based Scheduling for Laravel with Cadence

最后更新于 经过

Model-Based Scheduling for Laravel with Cadence image

Cadence 经过 史蒂夫·鲍曼 takes a different approach to scheduling in Laravel. Rather than centralising all timed tasks in a single scheduler file, it lets you attach one or many schedules directly to individual Eloquent model instances, each with its own expression and timezone, and fires events when they're due.

工作原理

After installing the package and publishing the migration, you get a schedules table that holds a polymorphic reference to any model, the schedule expression, an optional timezone, and precomputed next_run_at / last_run_at timestamps.

添加 Schedulable 接口和 HasSchedules trait to any model you want to schedule:

使用 DirectoryTree\Cadence\HasSchedules ;
使用 DirectoryTree\Cadence\Schedulable ;
班级 订阅 延伸 模型 实现 Schedulable
{
使用 HasSchedules ;
}

Then attach a schedule to a model instance. Cadence ships with drivers for cron expressions and RRULE patterns, and you pick which libraries to install:

# For cron expressions
作曲家 要求 dragonmantank/cron-expression
# For RRULE (pick one)
作曲家 要求 rlanvin/php-rrule
# 或者
作曲家 要求 simshaun/recurr

A cron-based schedule is straightforward:

使用 DirectoryTree\Cadence\Drivers\CronSchedule ;
// Bill this subscription every month on the 1st at midnight
$subscription -> addSchedule 新的 CronSchedule '0 0 1 * *' ));

For more expressive recurrence, RRULE gives you a lot more control over things that are awkward to express in cron:

使用 DirectoryTree\Cadence\Drivers\RruleSchedule ;
// Every two weeks on Tuesday and Thursday
$subscription -> addSchedule 新的 RruleSchedule 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH' ));

Schedules are timezone-aware, so users in different regions get the right local time:

$subscription -> addSchedule
新的 CronSchedule '0 8 * * 1' , 'Australia/Sydney' // Monday 8am Sydney time
(英文):

Dispatching and Reacting

Cadence provides a schedules:run Artisan command that you register with Laravel's scheduler to run every minute:

// routes/console.php
日程 :: 命令 'schedules:run' -> 不重叠 () -> 每分钟 ();

Each time it runs, it finds all records where next_run_at is past due, fires a ScheduleTriggered event for each one, and advances the next occurrence. Your application reacts through a listener:

使用 DirectoryTree\Cadence\Events\ScheduleTriggered ;
班级 ProcessDueSubscription
{
民众 功能 处理 ScheduleTriggered $event) : 空白
{
$subscription = $事件 -> 日程 -> schedulable;
$subscription -> processRenewal ();
}
}

The event carries the schedule record, giving you access to the parent model, the expression, and timestamps for branching on model type or logging what ran and when.

可扩展性

If neither cron nor RRULE fits, you can implement a custom driver by extending the base Schedule class and implementing resolveNextOccurrence() , which receives a CarbonInterface and returns the next occurrence. Drivers are registered by name, so swapping or extending them later is straightforward.

You can learn more and view the source code on GitHub

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

阅读文章
DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell image

DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell

阅读文章
Laravel's AI SDK adds sub-agents image

Laravel's AI SDK adds sub-agents

阅读文章
Laravel Introduces First-Party Passkey Authentication Support image

Laravel Introduces First-Party Passkey Authentication Support

阅读文章
Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0 image

Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0

阅读文章
Attach Addresses to Any Eloquent Model with Laravel Addressable image

Attach Addresses to Any Eloquent Model with Laravel Addressable

阅读文章