摘要:配有內(nèi)置的隊列系統(tǒng),可幫助您在后臺運(yùn)行任務(wù),并通過簡單的來配置系統(tǒng)在不同情況下起作用。您可以在中管理隊列配置,默認(rèn)情況下它有使用不同隊列驅(qū)動的幾個連接,您可以看到項目中可以有多個隊列連接,也可以使用多個隊列驅(qū)動程序。
原文鏈接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接收請求,做一些工作,然后向用戶返回響應(yīng),這是處理請求的Web服務(wù)器的正常同步工作流程,但有時您需要在后臺執(zhí)行不中斷或減慢的一些流程,例如在訂單之后向用戶發(fā)送發(fā)票電子郵件,你不想讓用戶等待郵件服務(wù)器接收請求,構(gòu)建電子郵件消息,然后分派給用戶,你只要向屏幕發(fā)送“謝謝!”給用戶,電子郵件在后臺準(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)置的隊列系統(tǒng),可幫助您在后臺運(yùn)行任務(wù),并通過簡單的API來配置系統(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中管理隊列配置,默認(rèn)情況下它有使用不同隊列驅(qū)動的幾個連接,您可以看到項目中可以有多個隊列連接,也可以使用多個隊列驅(qū)動程序。
We"ll be looking into the different configurations down the road, but let"s first take a look at the API:
我們將研究不同的配置,但請先看看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:
隊列Queue facade 是 queue 容器別名,如果我們看看QueueQueueServiceProvider ,我們可以看到這個別名是如何注冊的:
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 代理到在容器中注冊為 QueueQueueManager 類的單例,我們還將連接器注冊到Laravel所支持使用的registerConnectors()的不同隊列驅(qū)動程序中:
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)用注冊 register{DriverName}Connector方法,例如注冊一個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() 方法將值存儲到 QueueManager::$connectors 類屬性。
連接器只是一個類,它包含一個 connect() 方法,它根據(jù)需要創(chuàng)建所需驅(qū)動的一個實例,方法看起來像在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被注冊到容器中,它知道如何連接到不同的內(nèi)置隊列驅(qū)動,如果我們看下這個類,我們將在最后找到一個__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.
對 QueueManager 類中不存在的方法的所有調(diào)用將被發(fā)送到加載的驅(qū)動中,例如當(dāng)我們調(diào)用 Queue::push() 方法時,所發(fā)生的是manager選擇了連接到它的所需隊列驅(qū)動 ,并在該驅(qū)動上調(diào)用 push 方法。
How does the manager know which driver to pick? 管理器如何知道要選哪個驅(qū)動?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)沒有指定連接名稱時,Laravel將根據(jù)配置文件使用默認(rèn)隊列連接, 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ū)動名稱,管理器將檢查該驅(qū)動是否已被加載,只有當(dāng)它不是開始連接到該驅(qū)動程序并使用 resolve() 方法加載它時:
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ū)動,調(diào)用 connect() 方法,最后設(shè)置連接名稱以供進(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() 時,您正在使用的隊列驅(qū)動中調(diào)用 push 方法,每個驅(qū)動以其自己的方式處理不同的操作,但Laravel為您提供了一個統(tǒng)一的接口,您可以使用它告訴隊列管理器你使用了什么驅(qū)動程序。
What if I want to use a driver that"s not built-in? 如果我想使用不是內(nèi)置的驅(qū)動程序怎么辦?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.
簡單來說,您可以使用自定義驅(qū)動的名稱調(diào)用 Queue::addConnector() ,以及一個解釋如何獲取與該驅(qū)動程序的連接的閉包,還要確保您的連接器實現(xiàn) QueueConnectorsConnectorInterface 接口。
Once you register the connector, you can use any connection that uses this driver:
注冊連接器后,您可以使用任何使用此驅(qū)動的連接:
Queue::connection("my-connection")->push(...);
Continue to "Preparing Jobs For Queue"
繼續(xù)“準(zhǔn)備隊列作業(yè)”
轉(zhuǎn)載請注明:?轉(zhuǎn)載自Ryan是菜鳥 | LNMP技術(shù)棧筆記
如果覺得本篇文章對您十分有益,何不 打賞一下
本文鏈接地址:?剖析Laravel隊列系統(tǒng)之初探
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/23161.html
摘要:表示該工作應(yīng)該在每個月日上午運(yùn)行這里還有一些其他的示例表示工作應(yīng)該在星期三每分鐘運(yùn)行一次。表示該工作應(yīng)該每天在凌晨點和點運(yùn)行兩次。方法調(diào)用的實例作為唯一的參數(shù),這是用于記錄您提供的作業(yè)的計劃任務(wù)管理器,并決定每次守護(hù)進(jìn)程應(yīng)該運(yùn)行什么。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglar...
摘要:一旦這一切完成,方法會運(yùn)行在類屬性在命令構(gòu)造后設(shè)置容器解析實例,在中我們設(shè)置了將使用的緩存驅(qū)動,我們也根據(jù)命令來決定我們調(diào)用什么方法。作業(yè)只在以上起效在上也無效處理作業(yè)方法調(diào)用觸發(fā)事件觸發(fā)事件。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接https://divinglaravel.com/queue-system...
摘要:有幾種有用的方法可以使用將作業(yè)推送到特定的隊列在給定的秒數(shù)之后推送作業(yè)延遲后將作業(yè)推送到特定的隊列推送多個作業(yè)推送特定隊列上的多個作業(yè)調(diào)用這些方法之后,所選擇的隊列驅(qū)動會將給定的信息存儲在存儲空間中,供按需獲取。 原文鏈接https://divinglaravel.com/queue-system/pushing-jobs-to-queue There are several ways...
摘要:原文鏈接我們推送到隊列的每個作業(yè)都存儲在按執(zhí)行順序排序的某些存儲空間中,該存儲位置可以是數(shù)據(jù)庫,存儲或像這樣的第三方服務(wù)。這個數(shù)字從開始,在每次運(yùn)行作業(yè)時不斷增加。 原文鏈接https://divinglaravel.com/queue-system/preparing-jobs-for-queue Every job we push to queue is stored in som...
摘要:所以在這里創(chuàng)建一個事件的兩個實際方法是通過調(diào)用或,第一個提交一個的實例,后者提交來做一些特殊處理。那么會用表達(dá)式檢查命令是否到期嗎恰恰相反,使用庫來確定命令是否基于當(dāng)前系統(tǒng)時間相對于我們設(shè)置的時區(qū)。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-sche...
閱讀 1672·2021-09-26 10:00
閱讀 2935·2021-09-06 15:00
閱讀 3538·2021-09-04 16:40
閱讀 2298·2019-08-30 15:44
閱讀 715·2019-08-30 10:59
閱讀 1883·2019-08-29 18:34
閱讀 3616·2019-08-29 15:42
閱讀 2292·2019-08-29 15:36