当前位置:网站首页>laravel run scheduler command on weekdays (except holidays)
laravel run scheduler command on weekdays (except holidays)
2022-08-08 19:24:00 【rorg】
wpcmf: wpcmf cms ,内容管理系统,类似 wordpress 系统
Laravel There are some very useful methods that allow us to customize when commands are executed.不幸的是,Sometimes we need more control,而 Laravel And it doesn't work out of the box.
例如,When you need to execute orders every working day,That means every working day,但不包括假期.You don't want your colleagues(或您自己)Receive daily emails on Christmas morning.
在本文中,我将向您展示如何做到这一点.
避开周末
Laravel 默认自带 helper 方法weekdays
来配置 schedule Orders are only executed on weekdays.Suppose we have a command with a signaturesend:daily-email
,And we want this order only on weekdays(周一到周五)发送,We can do this by adding it to oursschedule
method in the console kernel to do this easily:
// app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
//...
protected function schedule(Schedule $schedule)
{
$schedule->command('send:daily-email')->weekdays();
}
//...
}
就是这样,我们只需要调用weekdays
method on the command,Laravel It will only be executed automatically on weekdays.
Now we have a working day.We need to figure out how to make sure Laravel skip vacation.
skip vacation
为此,我们将在 schedule 命令中使用 skip 方法.This method executes the function passed as an argument,如果该函数返回 true,then skip this execution.This way we can check if the day is a holiday.
Now we need to figure out if today is a holiday.有很多方法可以做到这一点,But there is a library you can pass to Carbon 添加一个 mixin 来帮助我们解决这个问题.您可以在此处阅读更多相关信息:https ://github.com/kylekatarnls/business-day
使用 composer 安装库:
composer require cmixin/business-day
安装后,We need to enable in the service provider mixin.AppServiceProvider 是一个很好的地方.
// app/Providers/AppServiceProvider.php
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Carbon;
use Cmixin\BusinessDay;
class AppServiceProvider extends ServiceProvider
{
// ...
public function boot()
{
BusinessDay::enable(Carbon::class);
}
}
在boot
in the service provider's method,We call the classenable
方法BusinessDay
,It's part of the library we just installed.We also need to pass the class where we will add the extra method.Illuminate\Support\Carbon
.
Now we need to tell the library which region holiday we are using.You can find all supported regions here:https ://github.com/kylekatarnls/business-day/tree/master/src/Cmixin/Holidays
例如,I'm going to take advantage of American holidays.This means the file I'm looking for is us-national.php
,We'll use this filename to configure our vacation.
public function boot()
{
BusinessDay::enable(Carbon::class);
Carbon::setHolidaysRegion('us-national');
}
就是这样,现在我们可以通过调用isHoliday
carbon method on the instance to know if a day is a holiday,例如:
now()->isHoliday();
把它们放在一起
We can now complete our command configuration by adding a holiday check to our skip method:
protected function schedule(Schedule $schedule)
{
$schedule->command('send:daily-email')->weekdays()->skip(function () {
return now()->isHoliday();
});
}
边栏推荐
猜你喜欢
Geometric g6 will carry harmonyos system, a comprehensive upgrade competitiveness of products
Lecture 4: Database Definition Language of DDL Type of SQL Statements
PX4-做飞控二次开发需要知道的事情-Cxm
大学生图书馆网页设计模板代码 DIV布局书店网页作业成品 学校书籍网页制作模板 学生简单书籍阅读网站设计成品
室外光纤资源管理——可视化管理平台
对话框管理器第六章:消息循环中的细节
Generate captchas tools
十六、一起学习Lua 文件 I/O
What are the benefits of knowledge sharing for businesses?
Open Office XML 格式中的 Style 设计原理
随机推荐
软件测试主要是做什么的?
Dandelion R300A 4G router, remote monitoring PLC tutorial
数据库学习之库的操作
Architecture Design Fundamentals
用工具实现 Mock API 的整个流程
期货开户哪家公司好,要正规安全的
[BJDCTF2020]Easy MD5
Dataworks上的ODPS spark处理数据会比直接用ODPS SQL效率高吗?
经验分享|低成本快节奏搭建企业知识管理系统的方法
nyoj714 Card Trick(第六届河南省程序设计大赛)
计算机网络面试常问知识
Is it safe to open an account with Qiniu Business School?Is it reliable to open an account?
How to add F4 Value Help trial version to the input parameters of the report in the ABAP report
view, index
C language elementary - structure
性能优化|从ping延时看CPU电源管理
PG's huge page
nyoj 712 Exploring treasure
视图,索引
Implementing Forward+ in Unity URP