当前位置:网站首页>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 oursschedulemethod 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();
}
//...
}就是这样,我们只需要调用weekdaysmethod 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);
}
}在bootin 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');
}就是这样,现在我们可以通过调用isHolidaycarbon 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();
});
}边栏推荐
- Open Office XML 格式中的 Style 设计原理
- The history of cartoon rendering
- USB CY68013设备描述符识别失败
- PyTorch入门:(二)Tensorboard的使用
- 为啥程序员下班后只关显示器从不关电脑?看看各大网站的答案~
- Fortinet全新云原生保护产品上线亚马逊云科技平台
- synApps -- Autosave
- Codeforces Round #705 (Div. 2)
- We want to replace the RDS database and upgrade from sqlserver 2016 web to 2017 enterprise cluster version, with expert consultation
- 性能优化|从ping延时看CPU电源管理
猜你喜欢

Research on ORACLE subqueries that lead to inability to push predicates

Architecture Design Fundamentals

What is the main purpose of software testing?

Implementing Forward+ in Unity URP

PX4-Things you need to know for secondary development of flight control-Cxm

golang for循环详解

What are the benefits of knowledge sharing for businesses?

SSM项目整合——综合案例

架构设计基本原则

【kali-权限提升】(4.2.6)社会工程学工具包(上):中间人攻击原理
随机推荐
Vue program of web cache problem after packaging
SSM项目整合——综合案例
企业进行知识共享的好处有哪些?
Research on ORACLE subqueries that lead to inability to push predicates
nyoj 712 探 寻 宝 藏(双线dp 第六届河南省程序设计大赛)
数字化工厂建设的内容主要有哪三个方面
架构设计基本原则
Securities account is better to choose which brokerage platform, which is more safe
腾讯云原生成本优化平台FinOps Crane荣获国家级大奖!
数据库学习之表的操作
Monaco-Editor 多人协作 编辑器
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄
What is the main purpose of software testing?
PyTorch入门:(四)torchvision中数据集的使用
Fortinet全新云原生保护产品上线亚马逊云科技平台
synApps -- Autosave
uniapp父组件使用prop将异步的数据传给子组件
Group DETR:分组一对多匹配是加速DETR收敛的关键
odoo 登录布局调整
制造企业为什么要部署数字化工厂系统