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

資訊專(zhuān)欄INFORMATION COLUMN

剖析Laravel隊(duì)列系統(tǒng)--初探

pubdreamcc / 1085人閱讀

摘要:配有內(nèi)置的隊(duì)列系統(tǒng),可幫助您在后臺(tái)運(yùn)行任務(wù),并通過(guò)簡(jiǎn)單的來(lái)配置系統(tǒng)在不同情況下起作用。您可以在中管理隊(duì)列配置,默認(rèn)情況下它有使用不同隊(duì)列驅(qū)動(dòng)的幾個(gè)連接,您可以看到項(xiàng)目中可以有多個(gè)隊(duì)列連接,也可以使用多個(gè)隊(duì)列驅(qū)動(dòng)程序。

原文鏈接https://divinglaravel.com/queue-system/before-the-dive

Laravel receives a request, does some work, and then returns a response to the user, this is the normal synchronous workflow of a web server handling a request, but sometimes you need to do some work behind the scenes that doesn"t interrupt or slowdown this flow, say for example sending an invoice email to the user after making an order, you don"t want the user to wait until the mail server receives the request, builds the email message, and then dispatches it to the user, instead you"d want to show a "Thank You!" screen to the user so that he can continue with his life while the email is being prepared and sent in the background.

Laravel接收請(qǐng)求,做一些工作,然后向用戶(hù)返回響應(yīng),這是處理請(qǐng)求的Web服務(wù)器的正常同步工作流程,但有時(shí)您需要在后臺(tái)執(zhí)行不中斷或減慢的一些流程,例如在訂單之后向用戶(hù)發(fā)送發(fā)票電子郵件,你不想讓用戶(hù)等待郵件服務(wù)器接收請(qǐng)求,構(gòu)建電子郵件消息,然后分派給用戶(hù),你只要向屏幕發(fā)送“謝謝!”給用戶(hù),電子郵件在后臺(tái)準(zhǔn)備和發(fā)送,他繼續(xù)做他自己的事。

Laravel is shipped with a built-in queue system that helps you run tasks in the background & configure how the system should react in different situation using a very simple API.

Laravel配有內(nèi)置的隊(duì)列系統(tǒng),可幫助您在后臺(tái)運(yùn)行任務(wù),并通過(guò)簡(jiǎn)單的API來(lái)配置系統(tǒng)在不同情況下起作用。

You can manage your queue configurations in config/queue.php, by default it has a few connections that use different queue drivers, as you can see you can have several queue connections in your project and use multiple queue drivers too.

您可以在 config/queue.php中管理隊(duì)列配置,默認(rèn)情況下它有使用不同隊(duì)列驅(qū)動(dòng)的幾個(gè)連接,您可以看到項(xiàng)目中可以有多個(gè)隊(duì)列連接,也可以使用多個(gè)隊(duì)列驅(qū)動(dòng)程序。

We"ll be looking into the different configurations down the road, but let"s first take a look at the API:

我們將研究不同的配置,但請(qǐng)先看看API:

Queue::push(new SendInvoice($order));

return redirect("thank-you");

The Queue facade here proxies to the queue container alias, if we take a look at QueueQueueServiceProvider we can see how this alias is registered:

隊(duì)列Queue facade 是 queue 容器別名,如果我們看看QueueQueueServiceProvider ,我們可以看到這個(gè)別名是如何注冊(cè)的:

protected function registerManager()
{
    $this->app->singleton("queue", function ($app) {
        return tap(new QueueManager($app), function ($manager) {
            $this->registerConnectors($manager);
        });
    });
}

So the Queue facade proxies to the QueueQueueManager class that"s registered as a singleton in the container, we also register the connectors to different queue drivers that Laravel supports using registerConnectors():

所以 Queue facade 代理到在容器中注冊(cè)為 QueueQueueManager 類(lèi)的單例,我們還將連接器注冊(cè)到Laravel所支持使用的registerConnectors()的不同隊(duì)列驅(qū)動(dòng)程序中:

public function registerConnectors($manager)
{
    foreach (["Null", "Sync", "Database", "Redis", "Beanstalkd", "Sqs"] as $connector) {
        $this->{"register{$connector}Connector"}($manager);
    }
}

This method simply calls the register{DriverName}Connector method, for example it registers a Redis connector:

該方法只需調(diào)用注冊(cè) register{DriverName}Connector方法,例如注冊(cè)一個(gè)Redis連接器:

protected function registerRedisConnector($manager)
{
    $manager->addConnector("redis", function () {
        return new RedisConnector($this->app["redis"]);
    });
}

The addConnector() method stores the values to a QueueManager::$connectors class property.

A connector is just a class that contains a connect() method which creates an instance of the desired driver on demand, here"s how the method looks like inside QueueConnectorsRedisConnector:

addConnector() 方法將值存儲(chǔ)到 QueueManager::$connectors 類(lèi)屬性。
連接器只是一個(gè)類(lèi),它包含一個(gè) connect() 方法,它根據(jù)需要?jiǎng)?chuàng)建所需驅(qū)動(dòng)的一個(gè)實(shí)例,方法看起來(lái)像在QueueConnectorsRedisConnector里面:

public function connect(array $config)
{
    return new RedisQueue(
        $this->redis, $config["queue"],
        Arr::get($config, "connection", $this->connection),
        Arr::get($config, "retry_after", 60)
    );
}

So now The QueueManager is registered into the container and it knows how to connect to the different built-in queue drivers, if we look at that class we"ll find a __call() magic method at the end:

所以現(xiàn)在QueueManager被注冊(cè)到容器中,它知道如何連接到不同的內(nèi)置隊(duì)列驅(qū)動(dòng),如果我們看下這個(gè)類(lèi),我們將在最后找到一個(gè)__call() 魔術(shù)方法:

public function __call($method, $parameters)
{
    return $this->connection()->$method(...$parameters);
}

All calls to methods that don"t exist in the QueueManager class will be sent to the loaded driver, for example when we called the Queue::push() method, what happened is that the manager selected the desired queue driver, connected to it, and called the push method on that driver.

對(duì) QueueManager 類(lèi)中不存在的方法的所有調(diào)用將被發(fā)送到加載的驅(qū)動(dòng)中,例如當(dāng)我們調(diào)用 Queue::push() 方法時(shí),所發(fā)生的是manager選擇了連接到它的所需隊(duì)列驅(qū)動(dòng) ,并在該驅(qū)動(dòng)上調(diào)用 push 方法。

How does the manager know which driver to pick? 管理器如何知道要選哪個(gè)驅(qū)動(dòng)?

Let"s take a look at the connection() method:

讓我們看下 connection() 方法

public function connection($name = null)
{
    $name = $name ?: $this->getDefaultDriver();

    if (! isset($this->connections[$name])) {
        $this->connections[$name] = $this->resolve($name);

        $this->connections[$name]->setContainer($this->app);
    }

    return $this->connections[$name];
}

When no connection name specified, Laravel will use the default queue connection as per the config files, the getDefaultDriver() returns the value of config/queue.php["default"]:

當(dāng)沒(méi)有指定連接名稱(chēng)時(shí),Laravel將根據(jù)配置文件使用默認(rèn)隊(duì)列連接, getDefaultDriver() 返回config/queue.php["default"]的值:

public function getDefaultDriver()
{
    return $this->app["config"]["queue.default"];
}

Once a driver name is defined the manager will check if that driver was loaded before, only if it wasn"t it starts to connect to that driver and load it using the resolve() method:

一旦定義了驅(qū)動(dòng)名稱(chēng),管理器將檢查該驅(qū)動(dòng)是否已被加載,只有當(dāng)它不是開(kāi)始連接到該驅(qū)動(dòng)程序并使用 resolve() 方法加載它時(shí):

protected function resolve($name)
{
    $config = $this->getConfig($name);

    return $this->getConnector($config["driver"])
                ->connect($config)
                ->setConnectionName($name);
}

First it loads the configuration of the selected connection from your config/queue.php file, then it locates the connector to the selected driver, calls the connect() method on it, and finally sets the connection name for further use.

首先從 config/queue.php 文件加載所選連接的配置,然后將連接器定位到所選驅(qū)動(dòng),調(diào)用 connect() 方法,最后設(shè)置連接名稱(chēng)以供進(jìn)一步使用。

Now we know where to find the push method 現(xiàn)在我們知道在哪里可以找到push方法

Yes, when you do Queue::push() you"re actually calling the push method on the queue driver you"re using, each driver handles the different operations in its own way but Laravel provides you a unified interface that you can use to give the queue manager instructions despite of what driver you use.

是的,當(dāng)您執(zhí)行 Queue::push() 時(shí),您正在使用的隊(duì)列驅(qū)動(dòng)中調(diào)用 push 方法,每個(gè)驅(qū)動(dòng)以其自己的方式處理不同的操作,但Laravel為您提供了一個(gè)統(tǒng)一的接口,您可以使用它告訴隊(duì)列管理器你使用了什么驅(qū)動(dòng)程序。

What if I want to use a driver that"s not built-in? 如果我想使用不是內(nèi)置的驅(qū)動(dòng)程序怎么辦?

Easy, you can call Queue::addConnector() with the name of your custom driver as well as a closure that explains how a connection to that driver could be acquired, also make sure that your connector implements the QueueConnectorsConnectorInterface interface.

簡(jiǎn)單來(lái)說(shuō),您可以使用自定義驅(qū)動(dòng)的名稱(chēng)調(diào)用 Queue::addConnector() ,以及一個(gè)解釋如何獲取與該驅(qū)動(dòng)程序的連接的閉包,還要確保您的連接器實(shí)現(xiàn) QueueConnectorsConnectorInterface 接口。

Once you register the connector, you can use any connection that uses this driver:

注冊(cè)連接器后,您可以使用任何使用此驅(qū)動(dòng)的連接:

Queue::connection("my-connection")->push(...);

Continue to "Preparing Jobs For Queue"

繼續(xù)“準(zhǔn)備隊(duì)列作業(yè)”

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

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

本文鏈接地址:?剖析Laravel隊(duì)列系統(tǒng)之初探

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/23161.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隊(duì)列系統(tǒng)--Worker

    摘要:一旦這一切完成,方法會(huì)運(yùn)行在類(lèi)屬性在命令構(gòu)造后設(shè)置容器解析實(shí)例,在中我們?cè)O(shè)置了將使用的緩存驅(qū)動(dòng),我們也根據(jù)命令來(lái)決定我們調(diào)用什么方法。作業(yè)只在以上起效在上也無(wú)效處理作業(yè)方法調(diào)用觸發(fā)事件觸發(fā)事件。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接https://divinglaravel.com/queue-system...

    CollinPeng 評(píng)論0 收藏0
  • 剖析Laravel隊(duì)列系統(tǒng)--推送作業(yè)到隊(duì)列

    摘要:有幾種有用的方法可以使用將作業(yè)推送到特定的隊(duì)列在給定的秒數(shù)之后推送作業(yè)延遲后將作業(yè)推送到特定的隊(duì)列推送多個(gè)作業(yè)推送特定隊(duì)列上的多個(gè)作業(yè)調(diào)用這些方法之后,所選擇的隊(duì)列驅(qū)動(dòng)會(huì)將給定的信息存儲(chǔ)在存儲(chǔ)空間中,供按需獲取。 原文鏈接https://divinglaravel.com/queue-system/pushing-jobs-to-queue There are several ways...

    maochunguang 評(píng)論0 收藏0
  • 剖析Laravel隊(duì)列系統(tǒng)--準(zhǔn)備隊(duì)列作業(yè)

    摘要:原文鏈接我們推送到隊(duì)列的每個(gè)作業(yè)都存儲(chǔ)在按執(zhí)行順序排序的某些存儲(chǔ)空間中,該存儲(chǔ)位置可以是數(shù)據(jù)庫(kù),存儲(chǔ)或像這樣的第三方服務(wù)。這個(gè)數(shù)字從開(kāi)始,在每次運(yùn)行作業(yè)時(shí)不斷增加。 原文鏈接https://divinglaravel.com/queue-system/preparing-jobs-for-queue Every job we push to queue is stored in som...

    marek 評(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

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

0條評(píng)論

閱讀需要支付1元查看
<