摘要:表示該工作應該在每個月日上午運行這里還有一些其他的示例表示工作應該在星期三每分鐘運行一次。表示該工作應該每天在凌晨點和點運行兩次。方法調用的實例作為唯一的參數,這是用于記錄您提供的作業的計劃任務管理器,并決定每次守護進程應該運行什么。
譯文GitHub https://github.com/yuansir/diving-laravel-zh
原文鏈接 https://divinglaravel.com/task-scheduling/before-the-dive
Imagine this scenario, as a developer of a large SaaS you"re tasked with finding a way to select 10 random customers every minute during the weekend and offer them a discounted upgrade, the job for sending the discount can be pretty easy but we need a way to run it every minute, for that let me share a brief introduction about CRON for those who"re not familiar with it.
想象這種情況,作為一個大型SaaS的開發者,您需要找到一種在周末每分鐘選擇10個隨機客戶的方式,并提供折扣升級,發送折扣的工作可能非常簡單,但我們需要每分鐘運行一次,為此我分享一些CRON的簡要介紹給還不熟悉人。
CRONCRON is a daemon that lives inside your linux server, it"s not awake most of the time but every minute it"ll open its eyes and see if it"s time to run any specific task that was given to it, you communicate with that daemon using crontab files, in most common setups that file can be located at /etc/crontab, here"s how a crontab file might look like:
CRON是一個守護進程,它駐留在你的linux服務器中,大部分時間都沒有喚醒,但是每一分鐘它都會睜開雙眼,看看是否運行任何給定的任務,你使用crontab文件與該守護進程通信,在大多數常見的設置文件可以位于/etc/crontab,crontab文件可能看起來像這樣:
0 0 1 * * /home/full-backup 0 0 * * * /home/partial-backup 30 5 10 * * /home/check-subscriptions
In the crontab file each line represents a scheduled job, and each job definition contains two parts:
The * part represents the timer for that job to run.
The second part is the command that should run
在crontab文件中,每行表示一個計劃任務作業,每個作業定義包含兩部分:
*部分代表該作業運行的計時器。
第二部分是應運行的命令
CRON Timing Syntax CRON時序語法The 5 asterisks represent the following in order:
Minute of an hour
Hour of a day
Day of a month
Month of a year
Day of a week
5個星號按順序排列如下:
一小時內的分鐘
一天內的小時
一個月內的日期
一年內的月份
一周的內的天
示例:
0 0 1 * * in the first example indicates that the job should run on every month, at the first day of the month, at 12 AM, at the first minute of the hour. Or simply it should run every 1st day of the month at 12:00 AM.
0 0 1 * * 在第一個例子中,表示該工作應在每月,每個月的第一個天,上午12點,每小時第一分鐘運行。 或者簡單地說,它應該在每月的第一天上午12:00運行。
0 * * * * in the second example indicates that the job should run every hour.
0 * * * * 在第二個例子中,表示該工作應該每小時運行一次。
30 5 10 * * indicates that the job should run on the 10th of every month at 5:30 AM
30 5 10 * * 表示該工作應該在每個月10日上午5:30運行
Here are a few other examples:
* * * * 3 indicates that the job should run every minute on Wednesdays.
* * * * 1-5 indicates that the job should run every minute Monday to Friday.
0 1,15 * * * indicates that the job should run twice a day at 1AM and 3PM.
*/10 * * * * indicates that the job should run every 10 minutes.
這里還有一些其他的示例:
* * * * 3 indicates that the job should run every minute on Wednesdays.
* * * * 3 表示工作應該在星期三每分鐘運行一次。
* * * * 1-5 indicates that the job should run every minute Monday to Friday.
* * * * 1-5 表示該工作應該每周一至周五運行。
0 1,15 * * * indicates that the job should run twice a day at 1AM and 3PM.
0 1,15 * * * 表示該工作應該每天在凌晨1點和3點運行兩次。
*/10 * * * * indicates that the job should run every 10 minutes.
*/10 * * * * 表示該工作應該每10分鐘運行一次。
So we register a cron task for our job? 所以我們為我們的工作注冊一個cron任務?Yeah we can simply register this in our crontab file:
是的,我們可以在我們的crontab文件中注冊:
* * * * php /home/divingLaravel/artisan send:offer
This command will inform the CRON daemon to run the php artisan send:offer artisan command every minute, pretty easy right? But it then gets confusing when we want to run the command every minute only on Thursdays and Tuesdays, or on specific days of the month, having to remember the syntax for cron jobs is not an easy job and also having to update the crontab file every time you want to add a new job or update the schedule can be pretty time consuming sometimes, so a few releases back Laravel added some interesting feature that provides an easy to remember syntax for scheduling tasks:
該命令將通知CRON守護程序每分鐘運行 php artisan send:offer artisan命令,是不是很容易? 但是,當我們想要在星期四和星期二或每個特定日子里每分鐘運行命令時會感到困惑,記住cron作業的語法不是一件容易的事,而且還需要更新crontab文件,你想添加一個新的工作或更新的時間表可能是相當耗時的時間,所以幾個版本發布后Laravel添加了一些有趣的功能,為調度任務提供了一個容易記住的語法:
$schedule->command("send:offer") ->everyFiveMinutes() ->wednesdays();
You only have to register one cron jobs in your crontab and laravel takes care of the rest under the hood:
你只需要在你的crontab中注冊一個cron工作,laravel會處理剩下的事:
* * * * * php /divingLaravel/artisan schedule:run >> /dev/null 2>&1
You may define your scheduled commands inside the schedule method of your AppConsoleKernel class:
您可以在AppConsoleKernel類的schedule方法中定義預定的命令:
protected function schedule(Schedule $schedule) { $schedule->command("send:offer") ->everyFiveMinutes() ->wednesdays(); }
If you"d like more information about the different Timer options, take a look at the official documentation.
如果您想了解有關不同計時器選項的更多信息,請查看 官方文檔。
While the Console Kernel instance is being instantiated, Laravel registers a listener to the Kernel"s booted event that binds the Scheduler to the container and calls the schedule() method of the kernel:
當Console Kernel被實例化時,Laravel向內核的booted事件注冊一個偵聽器,該事件將Scheduler綁定到容器并調用kernel的schedule()方法:
// in IlluminateFoundationConsoleKernel public function __construct(Application $app, Dispatcher $events) { $this->app->booted(function () { $this->defineConsoleSchedule(); }); } protected function defineConsoleSchedule() { // Register the Scheduler in the Container $this->app->instance( Schedule::class, $schedule = new Schedule($this->app[Cache::class]) ); // Call the schedule() method that we override in our AppConsoleKernel $this->schedule($schedule); }
This booted event is fired once the console kernel finishes the bootstrapping sequence defined in the Kernel class.
一旦console kernel完成Kernel類中定義的引導順序,這個booted事件就被觸發。
Inside the handle() method of the Kernel, Laravel checks if FoundationApplication was booted before, and if not it calls the bootstrapWith() method of the Application and passes the bootstrappers array defined in the console Kernel.
在Kernel的handle()方法中,Laravel會檢查FoundationApplication是否已啟動,如果不是調用應用程序的bootstrapWith()方法,并傳遞在console Kernel定義的引導程序數組。
Simply put: 簡單地說:When the CRON daemon calls the php artisan schedule:run command every minute, the Console Kernel will be booted up and the jobs you defined inside your AppConsoleKernel::schedule() method will be registered into the scheduler.
當CRON守護程序每分鐘都調用php artisan schedule:run命令時,控制臺Console Kernel將被啟動,您在AppConsoleKernel::schedule()方法中定義的作業將被注冊到調度程序。
The schedule() method takes an instance of IlluminateConsoleSchedulingSchedule as the only argument, this is the schedule manager used to record the jobs you give it and decides what should run every time the CRON daemon pings it.
schedule()方法調用IlluminateConsoleSchedulingSchedule的實例作為唯一的參數,這是用于記錄您提供的作業的計劃任務管理器,并決定每次CRON守護進程應該運行什么。
轉載請注明:?轉載自Ryan是菜鳥 | LNMP技術棧筆記
如果覺得本篇文章對您十分有益,何不 打賞一下
本文鏈接地址:?剖析Laravel計劃任務--初探
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/23238.html
摘要:配有內置的隊列系統,可幫助您在后臺運行任務,并通過簡單的來配置系統在不同情況下起作用。您可以在中管理隊列配置,默認情況下它有使用不同隊列驅動的幾個連接,您可以看到項目中可以有多個隊列連接,也可以使用多個隊列驅動程序。 原文鏈接https://divinglaravel.com/queue-system/before-the-dive Laravel receives a request...
摘要:持有雞的人是唯一被允許談話的人。這樣可以確保人們互不說話,也有自己的空間。所以當作業第一次啟動時,創建一個互斥,然后每次作業運行時,它檢查互斥是否存在,只有在沒有工作的情況下運行。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/pr...
摘要:所以在這里創建一個事件的兩個實際方法是通過調用或,第一個提交一個的實例,后者提交來做一些特殊處理。那么會用表達式檢查命令是否到期嗎恰恰相反,使用庫來確定命令是否基于當前系統時間相對于我們設置的時區。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-sche...
摘要:譯文原文鏈接在啟動計劃任務的事件的時候,的進度管理器在對象上調用方法,表示該事件發生在內。在方法里面定義每一個命令的互斥所以它是事件的表達式和命令字符串的組合。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/building-and...
閱讀 2882·2021-09-28 09:36
閱讀 3608·2021-09-27 13:59
閱讀 2484·2021-08-31 09:44
閱讀 2278·2019-08-30 15:54
閱讀 2352·2019-08-30 15:44
閱讀 1180·2019-08-30 13:45
閱讀 1223·2019-08-29 18:38
閱讀 1207·2019-08-29 18:37