成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

剖析 Laravel 計劃任務(wù)--避免重復(fù)

li21 / 1487人閱讀

摘要:持有雞的人是唯一被允許談話的人。這樣可以確保人們互不說話,也有自己的空間。所以當(dāng)作業(yè)第一次啟動時,創(chuàng)建一個互斥,然后每次作業(yè)運行時,它檢查互斥是否存在,只有在沒有工作的情況下運行。

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

原文鏈接 https://divinglaravel.com/task-scheduling/preventing-overlapping

Sometimes a scheduled job takes more time to run than what we initially expected, and this causes another instance of the job to start while the first one is not done yet, for example imagine that we run a job that generates a report every minute, after sometime when the data gets huge the report generation might take more than 1 minute so another instance of that job starts while the first is still ongoing.

有時一個預(yù)定的工作需要比我們最初預(yù)期的更多的時間運行,這樣會導(dǎo)致另外一個工作的實例開始,而第一個還沒有完成,例如,我們運行一個每分鐘生成報告的工作有時候當(dāng)數(shù)據(jù)變大時,報表生成可能需要1分鐘以上,這樣就可以在第一個還在進(jìn)行時啟動該作業(yè)的另一個實例。

In most scenarios this is fine, but sometimes this should be prevented in order to guarantee correct data or prevent a high server resources consumption, so let"s see how you can prevent such scenario in laravel:

在大多數(shù)情況下,這是很好的,但有時候應(yīng)該防止這種情況,以保證正確的數(shù)據(jù)或防止高的服務(wù)器資源消耗,所以讓我們看看如何防止這種情況在laravel中發(fā)生:

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

Laravel will check for the ConsoleSchedulingEvent::withoutOverlapping class property and if it"s set to true it"ll try to create a mutex for the job, and will only run the job if creating a mutex was possible.

Laravel將檢查 ConsoleSchedulingEvent::withoutOverlapping 類屬性,如果設(shè)置為true,它將嘗試為作業(yè)創(chuàng)建互斥,并且只有在創(chuàng)建互斥的情況下才能運行該作業(yè)。

But what"s a mutex? 但是上面是互斥?

Here"s the most interesting explanation I could find online:

這是我可以在網(wǎng)上找到最有趣的解釋:

When I am having a big heated discussion at work, I use a rubber chicken which I keep in my desk for just such occasions. The person holding the chicken is the only person who is allowed to talk. If you don"t hold the chicken you cannot speak. You can only indicate that you want the chicken and wait until you get it before you speak. Once you have finished speaking, you can hand the chicken back to the moderator who will hand it to the next person to speak. This ensures that people do not speak over each other, and also have their own space to talk. Replace Chicken with Mutex and person with thread and you basically have the concept of a mutex.

-- https://stackoverflow.com/questions/34524/what-is-a-mutex/34558#34558

當(dāng)我在工作中進(jìn)行熱烈的討論時,我使用一只橡膠雞,我在這樣的場合放在桌子上。 持有雞的人是唯一被允許談話的人。 如果你不握雞,你不會說話。 你只能指示你想要雞,等到你說話之前才能得到它。 一旦你完成演講,你可以將雞回到主持人,他將把它交給下一個人說話。 這樣可以確保人們互不說話,也有自己的空間。 用線替換雞與互斥和人,你基本上有一個互斥的概念。

-- https://stackoverflow.com/questions/34524/what-is-a-mutex/34558#34558

So Laravel creates a mutex when the job starts the very first time, and then every time the job runs it checks if the mutex exists and only runs the job if it doesn"t.

所以當(dāng)作業(yè)第一次啟動時,Laravel創(chuàng)建一個互斥,然后每次作業(yè)運行時,它檢查互斥是否存在,只有在沒有工作的情況下運行。

Here"s what happens inside the withoutOverlapping method:

這里是 withoutOverlapping 方法中做的事

public function withoutOverlapping()
{
    $this->withoutOverlapping = true;

    return $this->then(function () {
        $this->mutex->forget($this);
    })->skip(function () {
        return $this->mutex->exists($this);
    });
}

So Laravel creates a filter-callback method that instructs the Schedule Manager to ignore the task if a mutex still exists, it also creates an after-callback that clears the mutex after an instance of the task is done.

因此,Laravel創(chuàng)建一個filter-callback方法,指示Schedule Manager忽略任務(wù),如果互斥仍然存在,它還會創(chuàng)建一個在完成任務(wù)實例后清除互斥的回調(diào)。

Also before running the job, Laravel does the following check inside the ConsoleSchedulingEvent::run() method:

在運行該作業(yè)之前,Laravel會在ConsoleSchedulingEvent::run()方法中進(jìn)行以下檢查:

if ($this->withoutOverlapping && ! $this->mutex->create($this)) {
    return;
}
Where does the mutex property come from? 互斥體屬性來自哪里?

While the instance of ConsoleSchedulingSchedule is being instantiated, laravel checks if an implementation to the ConsoleSchedulingMutex interface was bound to the container, if yes it uses that instance but if not it uses an instance of ConsoleSchedulingCacheMutex:

當(dāng) ConsoleSchedulingSchedule 的實例被實例化時,laravel會檢查 ConsoleSchedulingMutex 接口的實現(xiàn)是否綁定到容器,如果是,則使用該實例,如果不是,使用ConsoleSchedulingCacheMutex實例:

$this->mutex = $container->bound(Mutex::class)
                        ? $container->make(Mutex::class)
                        : $container->make(CacheMutex::class);

Now while the Schedule Manager is registering your events it"ll pass an instance of the mutex:

現(xiàn)在,Schedule Manager正在注冊你的事件,它會傳遞互斥的一個實例:

$this->events[] = new Event($this->mutex, $command);

By default Laravel uses a cache-based mutex, but you can override that and implement your own mutex approach & bind it to the container.

默認(rèn)情況下,Laravel使用基于緩存的互斥,但您可以覆蓋它并實現(xiàn)自己的互斥方法并將其綁定到容器。

The cache-based mutex 基于緩存的互斥

The CacheMutex class contains 3 simple methods, it uses the event mutex name as a cache key:

CacheMutex 類包含3個簡單的方法,它使用事件互斥名作為緩存鍵:

public function create(Event $event)
{
    return $this->cache->add($event->mutexName(), true, 1440);
}

public function exists(Event $event)
{
    return $this->cache->has($event->mutexName());
}

public function forget(Event $event)
{
    $this->cache->forget($event->mutexName());
}
Mutex removal after task finishes 任務(wù)完成后的互斥刪除

As we"ve seen before, the manager registers an after-callback that removes the mutex after the task is done, for a task that runs a command on the OS that might be enough to ensure that the mutex is cleared, but for a callback task the script might die while executing the callback, so to prevent that an extra fallback was added in ConsoleSchedulingCallbackEvent::run():

如前所述,管理器注冊一個在完成任務(wù)之后刪除互斥的回調(diào),對于在操作系統(tǒng)上運行命令的任務(wù)可能足以確保互斥被清除,但是對于回調(diào) 執(zhí)行回調(diào)時腳本可能會死機(jī),所以為了防止這種情況在 ConsoleSchedulingCallbackEvent::run()中添加了一個額外的回退:

register_shutdown_function(function () {
    $this->removeMutex();
});

轉(zhuǎn)載請注明:?轉(zhuǎn)載自Ryan是菜鳥 | LNMP技術(shù)棧筆記

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

本文鏈接地址:?剖析Laravel計劃任務(wù)--避免重復(fù)

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/23237.html

相關(guān)文章

  • 剖析 Laravel 計劃任務(wù)--初探

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

    mo0n1andin 評論0 收藏0
  • 剖析 Laravel 計劃任務(wù)--事件屬性

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

    xiaowugui666 評論0 收藏0
  • 剖析 Laravel 計劃任務(wù)--創(chuàng)建和運行系統(tǒng)命令

    摘要:譯文原文鏈接在啟動計劃任務(wù)的事件的時候,的進(jìn)度管理器在對象上調(diào)用方法,表示該事件發(fā)生在內(nèi)。在方法里面定義每一個命令的互斥所以它是事件的表達(dá)式和命令字符串的組合。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/building-and...

    luodongseu 評論0 收藏0
  • 菜鳥理解setTimeout和setInterval

    摘要:也就是說,這僅僅是計劃在未來某一個時間執(zhí)行某個任務(wù),并不能保證精確的時間。重復(fù)執(zhí)行問題這個方法執(zhí)行時僅當(dāng)沒有該計時器的其他代碼示例時才進(jìn)行下一輪的執(zhí)行。這樣的規(guī)則就會導(dǎo)致某些間隔會被跳過,同時多個間隔可能比預(yù)期時間要短。 寫在前面,最近在準(zhǔn)備校招,陸陸續(xù)續(xù)做一些之前的總結(jié),寫了一個小系列的文章,想借此機(jī)會記錄下來,也能以后有個地方能進(jìn)行查閱,上一篇文章在css基礎(chǔ)總結(jié)希望能幫助一下和我...

    sixleaves 評論0 收藏0
  • 高性能千萬級定時任務(wù)管理服務(wù)forsun laravel插件使用詳解

    摘要:高性能高精度定時服務(wù),輕松管理千萬級定時任務(wù)。支持任務(wù)到期觸發(fā)和。支持創(chuàng)建延時任務(wù)和定時到期任務(wù),和原生保持相同接口,輕松使用。不支持任務(wù)輸出任務(wù)鉤子及維護(hù)模式。是不指定任務(wù)名時自動生成,每個任務(wù)名必須唯一,相同任務(wù)名重復(fù)定義將會自動覆蓋。 Forsun高性能高精度定時服務(wù),輕松管理千萬級定時任務(wù)。 定時服務(wù)項目地址:https://github.com/snower/forsun l...

    Muninn 評論0 收藏0

發(fā)表評論

0條評論

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