a-bashtannik / fasti
基于日历管理的 Laravel 任务调度器。
Requires
- php: ^8.2
Requires (Dev)
- laravel/framework: ^11.0
- laravel/pint: ^1.13.7
- orchestra/canvas: ^9.0
- phpstan/phpstan: 1.10.56
- phpunit/phpunit: ^11.3
- rector/rector: 0.19.5
Suggests
- illuminate/contracts: for the Laravel integration
- illuminate/support: for the Laravel integration
This package is auto-updated.
Last update: 2024-09-29 15:21:31 UTC
README
⚠️ 进行中注意:此包目前正在开发中,尚未准备好用于生产使用。
Fasti 是一个 Laravel 包,允许开发者精确地安排未来任务的执行。使用 Fasti,您可以将任何 Job
延迟任何时间段,指定到分钟的时间。在指定的时间,Fasti 从存储库中检索所需的 Job,并同步执行它或将它调度到队列中。
与 Laravel 强大的重复任务调度器不同,Fasti 专注于为特定时间安排单个任务。它易于使用,允许您按需轻松创建、列出或取消任务。
$job = new SendGreetingEmail($user); Fasti::schedule($job, '2024-12-31 23:59:59'); // Schedule a job for New Year's Eve 2024
安装
注意
此包需要 PHP 8.2+ 和 Laravel 11+
composer require a-bashtannik/fasti
Fasti 是为开发者设计的。
使用
使用知名 Laravel 模式和接口安排您的作业。
如果作业类实现了 handle()
方法,则 Fasti 允许作业进行调度。在安排的时间,Fasti 在 cron 作业进程线程中同步执行您的作业,与其他任何您定义的 cron 作业一起。要利用 Laravel 的队列系统,只需在您的作业类上实现 ShouldQueue
接口。这允许 Fasti 在幕后使用标准的 Bus 门面将作业调度到 Laravel 的队列。
use \Bashtannik\Fasti\Facades\Fasti; class SendGreetingEmail implements ShouldQueue { public function handle() { // Send the email } } $job = new SendGreetingEmail($user); Fasti::schedule($job, '2024-12-31 23:59:59'); // Push job to the queue for New Year's Eve 2024
此作业将在指定的时间同步执行
use \Bashtannik\Fasti\Facades\Fasti; class SendAlarmNotification { public function handle() { // Send the notification } } $job = new SendAlarmNotification(); Fasti::schedule($job, '2025-01-01 08:00:00'); // Execute job synchronously on New Year's Day 2025
如果类实现了 ShouldEncrypt
接口,Fasti 支持加密作业。
使用微小的 Eloquent 模型存储安排的任务
Fasti 包含一个轻量级的 Eloquent 模型,用于存储作业的基本信息:ID、有效负载和日期。虽然 Fasti 提供了方便的作业管理服务,但您不必局限于它。在您的控制器中,如果您需要更精细的控制或自定义操作,可以直接使用标准查询与 Eloquent 模型交互。
// Use the facade use \Bashtannik\Fasti\Facades\Fasti; Fasti::all(); // Retrieve all scheduled tasks Fasti::scheduled($at); // Retrieve all tasks scheduled at a specific time Fasti::cancel($id); // Cancel a scheduled task Fasti::cancelled(); // Retrieve all canceled tasks Fasti::find($id); // Retrieve a scheduled task by ID // Or use the Eloquent model directly use \Bashtannik\Fasti\Models\ScheduledJob; ScheduledJob::where('scheduled_at', '=', '2024-12-31 23:59:59')->get();
测试
使用 Fasti 的 Fasti::fake()
方法内置断言轻松测试安排的作业。
use \Bashtannik\Fasti\Facades\Fasti; Fasti::fake(); Fasti::assertScheduled($job); Fasti::assertNotScheduled($job); Fasti::assertScheduledAt(); Fasti::assertNotScheduledAt(); Fasti::assertCancelled()
Fasti 作为 Laravel 作业的调度层运行,专注于何时启动它们。需要注意的是,Fasti 不管理作业执行、队列、发布、尝试或失败。
相反,当安排的时间到达时,Fasti 简单地将作业交给 Laravel 的默认 Bus
。
这意味着您可以使用 Laravel 一起提供的 Bus 断言方法。
Bus::assertDispatched($job); Bus::assertNotDispatched($job); // etc.
使用自定义模型或存储来管理安排的作业
对于简单的应用程序,Fasti 内置的 Eloquent 模型足够使用。但是,如果您需要将安排的作业存储在自定义数据库表中或使用不同的存储机制,Fasti 提供了这样做的方法。
定义自己的模型实现 Bashtannik\Fasti\Contracts\SchedulableJob
接口。
需要注意的是,尽管您的模型必须实现此接口,但它不会添加新的属性或方法。这种方法允许 Eloquent 正常运行。您只需在模型或数据传输对象中提供一组标准字段即可,以确保 Fasti 运行顺畅,并且您的 IDE 的类型检查满意。
use Illuminate\Database\Eloquent\Model; use Bashtannik\Fasti\Contracts\SchedulableJob; class MyOwnModel extends Model implements SchedulableJob { // Your code }
创建自己的实现 FastiRepository
接口的存储库,并在您的应用程序服务提供商的 register()
方法中绑定它。
use Bashtannik\Fasti\Repositories\FastiRepository; use App\Repositories\MyOwnRepository; $this->app->bind( FastiScheduledJobsRepository::class, MyOwnRepository::class );
或使用更具有上下文感知的方法,在飞行中切换您的存储库。
use \Bashtannik\Fasti\Facades\Fasti; $repository = new MyOwnRepository( user: $user, company: $company, ); Fasti::setRepository($repository);
您已完成,现在Fasti将使用您的自定义仓库来管理计划任务。