摘要:所以在這里創(chuàng)建一個(gè)事件的兩個(gè)實(shí)際方法是通過調(diào)用或,第一個(gè)提交一個(gè)的實(shí)例,后者提交來做一些特殊處理。那么會(huì)用表達(dá)式檢查命令是否到期嗎恰恰相反,使用庫來確定命令是否基于當(dāng)前系統(tǒng)時(shí)間相對于我們設(shè)置的時(shí)區(qū)。
譯文GitHub https://github.com/yuansir/diving-laravel-zh
原文鏈接 https://divinglaravel.com/task-scheduling/properties-of-an-event
Every entry you add is converted into an instance of IlluminateConsoleSchedulingEvent and stored in an $events class property of the Scheduler, an Event object consists of the following:
你添加的每個(gè)記錄都將轉(zhuǎn)換為 IlluminateConsoleSchedulingEvent 的實(shí)例,并存儲(chǔ)在Scheduler的 $events 類屬性中,Event對象由以下內(nèi)容組成:
Command to run
CRON Expression
Timezone to be used to evaluate the time
Operating System User the command should run as
The list of Environments the command should run under
Maintenance mode configuration
Event Overlapping configuration
Command Foreground/Background running configuration
A list of checks to decide if the command should run or not
Configuration on how to handle the output
Callbacks to run after the command runs
Callbacks to run before the command runs
Description for the command
A unique Mutex for the command
命令運(yùn)行
CRON表達(dá)式
用于評估時(shí)間的時(shí)區(qū)
操作系統(tǒng)運(yùn)行該命令的用戶
命令應(yīng)該運(yùn)行的環(huán)境列表
維護(hù)模式配置
事件重疊配置
命令前臺/后臺運(yùn)行配置
用于決定該命令是否運(yùn)行的檢查列表
如何處理輸出的配置
命令運(yùn)行后運(yùn)行的回調(diào)
在命令運(yùn)行前運(yùn)行的回調(diào)
命令說明
命令的唯一Mutex
The command to run could be one of the following:
命令可能像下面一種方式運(yùn)行:
A callback
A command to run on the operating system
An artisan command
A job to be dispatched
回調(diào)
在操作系統(tǒng)上運(yùn)行的命令
artisan命令
被調(diào)度的作業(yè)
Using a callback 使用回調(diào)In case of a callback, the Container::call() method is used to run the value we pass which means we can pass a callable or a string representing a method on a class:
在回調(diào)的情況下,Container::call() 方法用于運(yùn)行我們傳遞的值,這意味著我們可以傳遞一個(gè)可以調(diào)用或表示方法的字符串:
protected function schedule(Schedule $schedule) { $schedule->call(function () { DB::table("recent_users")->delete(); })->daily(); }
Or:
protected function schedule(Schedule $schedule) { $schedule->call("MetricsRepository@cleanRecentUsers")->daily(); }Passing a command for the operating system 調(diào)用操作系統(tǒng)的命令
If you would like to pass a command for the operating system to run you can use exec():
如果要運(yùn)行操作系統(tǒng)的命令,可以使用 exec():
$schedule->exec("php /home/sendmail.php --user=10 --attachInvoice")->monthly();
You can also pass the parameters as an array:
您還可以將數(shù)組作為參數(shù):
$schedule->exec("php /home/sendmail.php", [ "--user=10", "--subject" => "Reminder", "--attachInvoice" ])->monthly();Passing an artisan command 調(diào)用一個(gè)artisan命令
$schedule->command("mail:send --user=10")->monthly();
You can also pass the class name:
你也可以傳一個(gè)類名
$schedule->command("AppConsoleCommandsEmailCommand", ["user" => 10])->monthly();
The values you pass are converted under the hood to an actual shell command and passed to exec() to run it on the operating system.
你傳遞的值將轉(zhuǎn)換為實(shí)際的shell命令,并傳遞給 exec() 在操作系統(tǒng)上運(yùn)行。
Dispatching a Job 調(diào)度一個(gè)作業(yè)You may dispatch a job to queue using the Job class name or an actual object:
您可以使用Job類名稱或?qū)嶋H對象將作業(yè)分發(fā)到隊(duì)列中:
$schedule->job("AppJobsSendOffer")->monthly(); $schedule->job(new SendOffer(10))->monthly();
Under the hood Laravel will create a callback that calls the dispatch() helper method to dispatch your command.
Laravel會(huì)創(chuàng)建一個(gè)回調(diào)函數(shù),調(diào)用 dispatch() 輔助方法來分發(fā)你的命令。
So the two actual methods of creating an event here is by calling exec() or call(), the first one submits an instance of IlluminateConsoleSchedulingEvent and the latter submits IlluminateConsoleSchedulingCallbackEvent which has some special handling.
所以在這里創(chuàng)建一個(gè)事件的兩個(gè)實(shí)際方法是通過調(diào)用 exec() 或 call(),第一個(gè)提交一個(gè) IlluminateConsoleSchedulingEvent 的實(shí)例,后者提交 IlluminateConsoleSchedulingCallbackEvent來做一些特殊處理。
Building the cron expression 創(chuàng)建cron表達(dá)式Using the timing method of the Scheduled Event, laravel builds a CRON expression for that event under the hood, by default the expression is set to run the command every minute:
使用計(jì)劃事件的計(jì)時(shí)方法,laravel會(huì)為該事件創(chuàng)建一個(gè)CRON表達(dá)式,默認(rèn)情況下,表達(dá)式設(shè)置為每分鐘運(yùn)行一次命令:
* * * * * *
But when you call hourly() for example the expression will be updated to:
但當(dāng)你調(diào)用 hourly() 時(shí)表達(dá)式會(huì)更新成這樣:
0 * * * * *
If you call dailyAt("13:30") for example the expression will be updated to:
當(dāng)你調(diào)用 dailyAt("13:30") 時(shí)表達(dá)式會(huì)更新成這樣:
30 13 * * * *
If you call twiceDaily(5, 14) for example the expression will be updated to:
當(dāng)你調(diào)用 twiceDaily(5, 14) 時(shí)表達(dá)式會(huì)更新成這樣:
0 5,14 * * * *
A very smart abstraction layer that saves you tons of research to find the right cron expression, however you can pass your own expression if you want as well:
一個(gè)非常聰明的抽象層,可以節(jié)省大量的精力來找到正確的cron表達(dá)式,但是如果你只要你想你也可以傳遞你自己的表達(dá)式:
$schedule->command("mail:send")->cron("0 * * * * *");How about timezones? 如何設(shè)置時(shí)區(qū)?
If you want the CRON expression to be evaluated with respect to a specific timezone you can do that using:
如果您希望CRON表達(dá)式針對特定時(shí)區(qū),則可以使用以下方式進(jìn)行:
->timezone("Europe/London")
Under the hood Laravel checks the timezone value you set and update the Carbon date instance to reflect that.
Laravel檢查您設(shè)置的時(shí)區(qū)值,并更新 Carbon 日期實(shí)例使其起作用。
So laravel checks if the command is due using the CRON expression? 那么Laravel會(huì)用CRON表達(dá)式檢查命令是否到期嗎?Exactly, Laravel uses the mtdowling/cron-expression library to determine if the command is due based on the current system time (with respect to the timezone we set).
恰恰相反,Laravel使用 mtdowling/cron-expression 庫來確定命令是否基于當(dāng)前系統(tǒng)時(shí)間(相對于我們設(shè)置的時(shí)區(qū))。
Adding Constraints on running the command Duration constraints 在運(yùn)行命令時(shí)添加限制 持續(xù)時(shí)間限制For example if you want the command to run daily but only between two specific dates:
例如,如果您希望命令每天運(yùn)行,但只能在兩個(gè)特定日期之間運(yùn)行:
->between("2017-05-27", "2017-06-26")->daily();
And if you want to prevent it from running during a specific period:
如果你想防止它在一段特定的時(shí)間內(nèi)運(yùn)行:
->unlessBetween("2017-05-27", "2017-06-26")->daily();Environment constraints 環(huán)境限制
You can use the environments() method to pass the list of environments the command is allowed to run under:
您可以使用 environments() 設(shè)置傳遞命令允許運(yùn)行的環(huán)境列表:
->environments("staging", "production");Maintenance Mode 維護(hù)模式
By default scheduled commands won"t run when the application is in maintenance mode, however you can change that by using:
默認(rèn)情況下,當(dāng)應(yīng)用程序處于維護(hù)模式時(shí),調(diào)度的命令不會(huì)運(yùn)行,但是您可以通過使用以下命令來更改:
->evenInMaintenanceMode()OS User 系統(tǒng)用戶
You can set the Operating System user that"ll run the command using:
你可以設(shè)置那個(gè)操作系統(tǒng)用戶來執(zhí)行這個(gè)命令:
->user("forge")
Under the hood Laravel will use sudo -u forge to set the user on the operating system.
Laravel將使用 sudo -u forge 設(shè)置在操作系統(tǒng)上運(yùn)行的用戶。
Custom Constraints 自定義限制You can define your own custom constraint using the when() and skip() methods:
您可以使用 when() 和 skip() 方法定義自定義約束:
// Runs the command only when the user count is greater than 1000 ->when(function(){ return User::count() > 1000; }); // Runs the command unless the user count is greater than 1000 ->skip(function(){ return User::count() > 1000; });Before and After callbacks 之前和之后回調(diào)函數(shù)
Using the before() and then() methods you can register callbacks that"ll run before or after the command finishes execution:
使用 before() 和 then() 方法可以注冊在命令完成執(zhí)行之前或之后運(yùn)行的回調(diào)函數(shù):
->before(function(){ Mail::to("myself@Mail.com", new CommandStarted()); }) ->then(function(){ Mail::to("myself@Mail.com", new CommandFinished()); });
You can also ping URLs or webhooks using the pingBefore() and thenPing() methods:
您還可以使用 pingBefore() and thenPing() 方法ping URL或webhooks:
->ping("https://my-webhook.com/start")->thenPing("https://my-webhook.com/finish")
Using these commands laravel registers a before/after callbacks under the hood and uses Guzzle to send a GET HTTP request:
使用這些命令laravel在注冊一個(gè)前/后回調(diào),并使用Guzzle發(fā)送一個(gè) GET HTTP請求:
return $this->before(function () use ($url) { (new HttpClient)->get($url); });
轉(zhuǎn)載請注明:?轉(zhuǎn)載自Ryan是菜鳥 | LNMP技術(shù)棧筆記
如果覺得本篇文章對您十分有益,何不 打賞一下
本文鏈接地址:?剖析Laravel計(jì)劃任務(wù)--事件屬性
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/23240.html
摘要:持有雞的人是唯一被允許談話的人。這樣可以確保人們互不說話,也有自己的空間。所以當(dāng)作業(yè)第一次啟動(dòng)時(shí),創(chuàng)建一個(gè)互斥,然后每次作業(yè)運(yùn)行時(shí),它檢查互斥是否存在,只有在沒有工作的情況下運(yùn)行。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/pr...
摘要:譯文原文鏈接在啟動(dòng)計(jì)劃任務(wù)的事件的時(shí)候,的進(jìn)度管理器在對象上調(diào)用方法,表示該事件發(fā)生在內(nèi)。在方法里面定義每一個(gè)命令的互斥所以它是事件的表達(dá)式和命令字符串的組合。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/building-and...
摘要:表示該工作應(yīng)該在每個(gè)月日上午運(yùn)行這里還有一些其他的示例表示工作應(yīng)該在星期三每分鐘運(yùn)行一次。表示該工作應(yīng)該每天在凌晨點(diǎn)和點(diǎn)運(yùn)行兩次。方法調(diào)用的實(shí)例作為唯一的參數(shù),這是用于記錄您提供的作業(yè)的計(jì)劃任務(wù)管理器,并決定每次守護(hù)進(jìn)程應(yīng)該運(yùn)行什么。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglar...
摘要:文章轉(zhuǎn)自視頻教程優(yōu)雅的應(yīng)用調(diào)試工具新擴(kuò)展是由和開源的應(yīng)用的調(diào)試工具。計(jì)劃任務(wù)列出已運(yùn)行的計(jì)劃任務(wù)。該封閉函數(shù)會(huì)被序列化為一個(gè)長字符串,加上他的哈希與簽名如出一轍該功能將記錄所有異常,并可查看具體異常情況。事件顯示所有事件的列表。 文章轉(zhuǎn)自:https://laravel-china.org/topics/19013視頻教程:047. 優(yōu)雅的應(yīng)用調(diào)試工具--laravel/telesco...
摘要:用法顯示當(dāng)前的幫助信息不輸出任何信息顯示當(dāng)前版本強(qiáng)制輸出禁用輸出不進(jìn)行交互運(yùn)行環(huán)境詳細(xì)輸出普通更加詳細(xì)可用命令全局命令清除編譯生成的文件,相當(dāng)于的反操作將站點(diǎn)設(shè)為維護(hù)狀態(tài)顯示當(dāng)前運(yùn)行環(huán)境來源于 laravel artisan 用法 $ php artisan Laravel Framework version 5.1.46 (LTS) Usage: command [options] ...
閱讀 1870·2021-11-25 09:43
閱讀 2149·2021-11-19 09:40
閱讀 3428·2021-11-18 13:12
閱讀 1741·2021-09-29 09:35
閱讀 662·2021-08-24 10:00
閱讀 2508·2019-08-30 15:55
閱讀 1714·2019-08-30 12:56
閱讀 1818·2019-08-28 17:59