国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

剖析 Laravel 計劃任務--創建和運行系統命令

luodongseu / 2820人閱讀

摘要:譯文原文鏈接在啟動計劃任務的事件的時候,的進度管理器在對象上調用方法,表示該事件發生在內。在方法里面定義每一個命令的互斥所以它是事件的表達式和命令字符串的組合。

譯文GitHub https://github.com/yuansir/diving-laravel-zh

原文鏈接 https://divinglaravel.com/task-scheduling/building-and-running-the-os-command

When it"s time to fire a scheduled event, Laravel"s schedule manager calls the run() method on the IlluminateConsoleSchedulingEvent object representing that event, this happens inside the IlluminateConsoleSchedulingScheduleRunCommand.

在啟動計劃任務的事件的時候,Laravel的進度管理器在IlluminateConsoleSchedulingEvent對象上調用 run() 方法,表示該事件發生在 IlluminateConsoleSchedulingScheduleRunCommand 內。

This run() method builds the command syntax and runs it on the operating system using the Symfony Process component, but before building the command it first checks if the command should be running in the background, by default all commands run in the foreground unless you use the following method while scheduling your command:

這個 run() 方法構建命令語法,并使用Symfony Process組件在操作系統上運行它,但在構建命令之前,它首先檢查該命令是否應該在后臺運行,默認情況下所有命令都在前臺運行 除非你使用以下方法來調度命令:

->runInBackground()
When do I need to run a command in the background? 什么時候我需要在后臺運行命令?

Imagine if you have several tasks that should run at the same time, say every hour, with the default settings Laravel will instruct the OS to run the commands one by one:

假設如果您有幾個任務應該同時運行,比如每個小時,Laravel默認設置會指示操作系統逐個運行命令:

~ php artisan update:coupons
# Waiting for the command to finish
# ...
# Command finished, now we run the next one
~ php artisan send:mail

However, you can instruct the OS to run the commands in the background so that you can continue pushing more commands even if the other ones haven"t finished yet:

但是,你可以指示操作系統在后臺運行命令,以便您可以繼續推送更多命令,即使其他命令尚未完成:

~ php artisan update:coupons &
~ php artisan send:mail &

Using the ampersand at the end of a command lets you continue pushing commands without having to wait for the initial ones to finish.

使用命令末尾的&符號可以繼續推送命令,而無需等待初始化完成。

The run() method checks the value of the runInBackground property and decides which method to call next, runCommandInForeground() or runCommandInBackground().

run() 方法檢查 runInBackground 屬性的值,并決定下一個調用哪個方法runCommandInForeground() 還是 runCommandInBackground()

In case the command is to be run in the foreground the rest is simple:

如果命令要在前臺運行,其余部分就簡單了:

$this->callBeforeCallbacks($container);

(new Process(
    $this->buildCommand(), base_path(), null, null, null
))->run();

$this->callAfterCallbacks($container);

Laravel executes any before-callbacks, sends the command to the OS, and finally executes any after-callbacks.

Laravel執行任意before-callbacks,將命令發送到OS,最后執行任意before-callbacks。

However, if the command is to run the background Laravel calls callBeforeCallbacks(), sends the command, but doesn"t call the after-callbacks, the reason is as you might think, because the command will be executed in the background so if we call callAfterCallbacks() at this point it won"t be running after the command finishes, it"ll run once the command is sent to the OS.

但是,如果命令是在后臺運行Laravel調用 callBeforeCallbacks(),發送命令,但不調用after-callbacks,原因正如你所想的,因為該命令將在后臺執行 如果我們在這個時候調用 callAfterCallbacks() ,那么在命令完成之后,它將不會運行,一旦命令被發送到操作系統,它就會運行。

So no after-callbacks are executed when we run commands in the background? 那么當我們在后臺運行命令時,沒有執行after-callbacks?

They run, laravel does that using another command that runs after the original one finishes:

他們運行了,laravel在原來的一個命令完成后使用另一個命令運行:

(php artisan update:coupons ; php artisan schedule:finish eventMutex) &

This command will cause a sequence of two commands to run one after another but in the background, so after your update:coupons command finishes a schedule:finish command will run given the Mutex of the current event, using this ID Laravel locates the event and runs its after-callbacks.

這個命令會導致一系列的兩個命令一個接一個地運行,但在后臺運行,所以在你的 update:coupons 命令完成一個 schedule:finish 命令后,會運行給定當前事件的 Mutex,使用這個ID Laravel 查找事件并運行其回調。

Building the command string 構建命令字符串

When the scheduler calls the runCommandInForeground() or runCommandInBackground()methods, a buildCommand() is called to build the actual command that the OS will run, this method simply does the following:

當調度程序調用 runCommandInForeground()runCommandInBackground() 方法時,調用一個buildCommand() 來構建操作系統將運行的實際命令,這個方法只需執行以下操作:

return (new CommandBuilder)->buildCommand($this);

To build the command, the following configurations need to be known:

The command mutex

The location that output should be sent to

Determine if the output should be appended

The user to run the command under

Background vs Foreground

構建命令,需要知道以下配置:

命令mutex

輸出應發送到的位置

確定輸出是否應該追加

運行命令的用戶

后臺還是前臺

The command mutex 命令互斥

A mutex is a unique ID set for every command, Laravel uses it mainly to prevent command overlapping which we will discuss later, but it also uses it as a unique ID for the command.

互斥是每個命令的唯一ID集合,Laravel主要使用它來防止命令重疊,我們稍后將討論,但它也將其用作命令的唯一ID。

Laravel defines the mutex of each command inside the Event::mutexName() method:

Laravel在 Event::mutexName() 方法里面定義每一個命令的互斥:

return "framework".DIRECTORY_SEPARATOR."schedule-".sha1($this->expression.$this->command);

So it"s a combination of the CRON expression of the event as well as the command string.

所以它是事件的CRON表達式和命令字符串的組合。

However, for callback events the mutex is created as follows:

但是,對于回調事件是這樣創建互斥的:

return "framework/schedule-".sha1($this->description);

So to ensure having a correct mutex for your callback event you need to set a description for the command:

所以為了確保你的回調事件有一個正確的互斥,你需要為命令設置一個描述:

$schedule->call(function () {
    DB::table("recent_users")->delete();
})->daily()->description("Clear recent users");
Handling output 控制輸出

By default the output of commands is sent to /dev/null which is a special file that discards data written to it, however if you want to send the command output somewhere you can change that using the sendOutputTo() method while defining the command:

默認情況下,命令的輸出被發送到 /dev/null ,這是一個寫入丟棄數據的特殊文件,但是如果你想在某個地方發送命令輸出,可以使用 sendOutputTo() 定義命令:

$schedule->command("mail:send")->sendOutputTo("/home/scheduler.log");

But this will cause the output to overwrite whatever is written to the scheduler.log file every time, to append the output instead you can use appendOutputTo(). Here"s how the command would look like:

但這會導致輸出覆蓋每次寫入 scheduler.log 文件的任何東西,如果用追加的方式輸出可以使用appendOutputTo()。 命令如下所示:

// Append output to file
php artisan mail:send >> /home/scheduler.log 2>&1

// Overwrite file
php artisan mail:send > /home/scheduler.log 2>&1

2>&1 instructs the OS to redirect error output to the standard output channel, in short words that means errors and output will be logged into your file.

2>&1 指示操作系統將錯誤輸出重定向到標準輸出通道,簡而言之,這意味著錯誤和輸出將被記錄到您的文件中。

Using the correct user 使用正確的用戶

When you set a user to run the command:

當你設置一個用戶去執行命令的時候:

$schedule->command("mail:send")->user("forge");

Laravel will run the command as follows:

Laravel會像下面這樣執行命令:

sudo -u forge -- sh -c "php artisan mail:send >> /home/scheduler.log 2>&1"
Running in the background 在后臺允許

As we discussed before, the command string will look like this in case it"s desired for it to run in the background:

As we discussed before, the command string will look like this in case it"s desired for it to run in the background:

正如我們之前討論過的,命令字符串將如下所示,它在后臺運行:

(php artisan update:coupons ; php artisan schedule:finish eventMutex) &

But that"s just a short form, here"s the complete string that"ll actually run:

但這只是一個簡短的形式,這里是完整的字符串,實際上可以運行:

(php artisan update:coupons >> /home/scheduler.log 2>&1 ; php artisan schedule:finish eventMutex) > /dev/null 2>&1  &

Or this if you don"t set it to append output & didn"t define a custom destination:

或者如果您沒有將其設置為追加輸出,也沒有定義自定義輸出位置:

(php artisan update:coupons > /dev/null 2>&1 ; php artisan schedule:finish eventMutex) > /dev/null 2>&1  &
Sending the output via email 通過郵件發送輸出

You can choose to send the command output to an email address using the emailOutputTo()method:

你可以通過調用 emailOutputTo() 方法來將命令輸出發送到電子郵件

$schedule->command("mail:send")->emailOutputTo(["myemail@mail.com", "myOtheremail@mail.com"]);

You can also use emailWrittenOutputTo() instead if you want to only receive emails if there"s an output, otherwise you"ll receive emails even if now output for you to see, it"ll be just a notification that the command ran.

如果有一個輸出,你只想接收電子郵件你也可以使用 emailWrittenOutputTo() ,否則你會收到電子郵件即使現在輸出給你你看到了,它也只是命令運行一個通知。

This method will update the output property of the Scheduled Event and point it to a file in the storage/logs directory:

這個方法將更新計劃事件的輸出屬性并將其輸出到 storage/logs 目錄中的文件:

if (is_null($this->output) || $this->output == $this->getDefaultOutput()) {
    $this->sendOutputTo(storage_path("logs/schedule-".sha1($this->mutexName()).".log"));
}

Notice that this will only work if you haven"t already set a custom output destination.

注意,只有在尚未設置自定義輸出目標位置時,此操作才會起作用。

Next Laravel will register an after-callback that"ll try to locate that file, read its content, and send it to the specified recipients.

接下來,Laravel將注冊一個回調,嘗試找到該文件,讀取其內容,并將其發送到指定的收件人。

$text = file_exists($this->output) ? file_get_contents($this->output) : "";

if ($onlyIfOutputExists && empty($text)) {
    return;
}

$mailer->raw($text, function ($m) use ($addresses) {
    $m->to($addresses)->subject($this->getEmailSubject());
});

轉載請注明:?轉載自Ryan是菜鳥 | LNMP技術棧筆記

如果覺得本篇文章對您十分有益,何不 打賞一下

本文鏈接地址:?剖析Laravel計劃任務--創建和運行系統命令

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/23239.html

相關文章

  • 剖析 Laravel 計劃任務--事件屬性

    摘要:所以在這里創建一個事件的兩個實際方法是通過調用或,第一個提交一個的實例,后者提交來做一些特殊處理。那么會用表達式檢查命令是否到期嗎恰恰相反,使用庫來確定命令是否基于當前系統時間相對于我們設置的時區。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-sche...

    xiaowugui666 評論0 收藏0
  • 剖析 Laravel 計劃任務--避免重復

    摘要:持有雞的人是唯一被允許談話的人。這樣可以確保人們互不說話,也有自己的空間。所以當作業第一次啟動時,創建一個互斥,然后每次作業運行時,它檢查互斥是否存在,只有在沒有工作的情況下運行。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/pr...

    li21 評論0 收藏0
  • 剖析 Laravel 計劃任務--初探

    摘要:表示該工作應該在每個月日上午運行這里還有一些其他的示例表示工作應該在星期三每分鐘運行一次。表示該工作應該每天在凌晨點和點運行兩次。方法調用的實例作為唯一的參數,這是用于記錄您提供的作業的計劃任務管理器,并決定每次守護進程應該運行什么。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglar...

    mo0n1andin 評論0 收藏0
  • 使用 Laravel 制定 MySQL 數據庫備份計劃任務

    摘要:現在讓我們將這個命令通過使用命令進行封裝,使其更易于運行和可加入計劃任務。編寫備份任務的計劃任務首先,在中能夠輕松創建計劃任務。 譯文首發于 使用 Laravel 制定 MySQL 數據庫備份計劃任務,轉載請注明出處。 你可以在終端里通過運行一行命令導出整個數據庫。這種方案不僅簡單直接而且有效。不過有更加自動化的解決方案。讓我們來看看究竟是什么! showImg(https://seg...

    wangshijun 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<