成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

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

li21 / 1620人閱讀

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

譯文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.

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

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ù)情況下,這是很好的,但有時(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ùn)行該作業(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)行熱烈的討論時(shí),我使用一只橡膠雞,我在這樣的場(chǎng)合放在桌子上。 持有雞的人是唯一被允許談話的人。 如果你不握雞,你不會(huì)說(shuō)話。 你只能指示你想要雞,等到你說(shuō)話之前才能得到它。 一旦你完成演講,你可以將雞回到主持人,他將把它交給下一個(gè)人說(shuō)話。 這樣可以確保人們互不說(shuō)話,也有自己的空間。 用線替換雞與互斥和人,你基本上有一個(gè)互斥的概念。

-- 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è)第一次啟動(dòng)時(shí),Laravel創(chuàng)建一個(gè)互斥,然后每次作業(yè)運(yùn)行時(shí),它檢查互斥是否存在,只有在沒(méi)有工作的情況下運(yùn)行。

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)建一個(gè)filter-callback方法,指示Schedule Manager忽略任務(wù),如果互斥仍然存在,它還會(huì)創(chuàng)建一個(gè)在完成任務(wù)實(shí)例后清除互斥的回調(diào)。

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

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

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

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 的實(shí)例被實(shí)例化時(shí),laravel會(huì)檢查 ConsoleSchedulingMutex 接口的實(shí)現(xiàn)是否綁定到容器,如果是,則使用該實(shí)例,如果不是,使用ConsoleSchedulingCacheMutex實(shí)例:

$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正在注冊(cè)你的事件,它會(huì)傳遞互斥的一個(gè)實(shí)例:

$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使用基于緩存的互斥,但您可以覆蓋它并實(shí)現(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個(gè)簡(jiǎn)單的方法,它使用事件互斥名作為緩存鍵:

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():

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

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

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

如果覺(jué)得本篇文章對(duì)您十分有益,何不 打賞一下

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

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/23237.html

相關(guān)文章

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

    摘要:表示該工作應(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...

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

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

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

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

    luodongseu 評(píng)論0 收藏0
  • 菜鳥(niǎo)理解setTimeout和setInterval

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

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

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

    Muninn 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<