2015-03-02 176 views
1

我是使用隊列進行密集進程的新手。我有一個應用程序,可以上傳短視頻,使用FFMPEG處理它,然後使用API​​上傳到YouTube,然後與我的數據庫進行交互。我的問題:PHP隊列實現

我應該使用兩個不同的隊列嗎?一個處理,然後把它交給不同的隊列上傳?或者我應該把所有的處理放在一個工人身上?

可以從工作人員與數據庫進行交互還是應該以其他方式進行操作?

+0

使用2個不同的隊列來處理不同的事情可能會更好。如果你在一個過程中做所有事情,那麼完成所有事情可能需要很長時間。如果你使用2個不同的流程,那麼它將能夠在更短的時間內分配工作並完成整個事情 – Virendra 2015-03-02 16:25:32

回答

0

我建議使用兩個不同的隊列,一個用於圖像處理,另一個用於將其上傳到YouTube。從隊列中查詢數據庫是完全可以的,儘管您可能會在消息中傳遞所有必需的數據,因此您不需要數據庫。

下面介紹如何使用enqueue庫來實現類似的功能。

您必須安裝enqueue/simple-client庫和one of the transports。假設你選擇了文件系統的,所以讓我們來安裝它:

composer require enqueue/simple-client enqueue/fs 

現在讓我們看看如何從您的文章腳本發送消息:

<?php 
// producer.php 

use Enqueue\SimpleClient\SimpleClient; 

include __DIR__.'/vendor/autoload.php'; 

$client = new SimpleClient('file://'); // the queue will store messages in tmp folder 

// you uploaded the file to your server, 
// and now you have a path to the file. 
// I assume it is in the $movieFile var. 

$client->sendCommand('process_movie', $movieFile); 

消費腳本:

<?php 
// consumer.php 

use Enqueue\Client\Config; 
use Enqueue\SimpleClient\SimpleClient; 
use Enqueue\Psr\PsrProcessor; 
use Enqueue\Psr\PsrMessage; 

include __DIR__.'/vendor/autoload.php'; 

$client = new SimpleClient('file://'); 

$client->bind(Config::COMMAND_TOPIC, 'process_movie', function(PsrMessage $psrMessage) use ($client) { 
    $movieFile = $psrMessage->getBody(); 

    // a movie processing logic here 

    // once we have done with processing we can send a message to upload_movie queue. 

    $client->sendCommand('upload_movie', $movieFile); 

    return PsrProcessor::ACK; 
}); 

$client->bind(Config::COMMAND_TOPIC, 'upload_movie', function(PsrMessage $psrMessage) { 
    $movieFile = $psrMessage->getBody(); 

    // a movie uploading logic here 

    return PsrProcessor::ACK; 
}); 

// this call is optional but it worth to mention it. 
// it configures a broker, for example it can create queues and excanges on RabbitMQ side. 
$client->setupBroker(); 

$client->consume(); 

在本地計算機上使用supervisord或其他進程管理器運行儘可能多的consumer.php進程,您可以在本地計算機上運行它,而無需任何額外的庫或程序包。

這是一個基本的例子,排隊有很多其他功能可能派上用場。如果您有興趣,請查看enqueue documentation