Queue::getTasksForWorker PHP Method

getTasksForWorker() public method

public getTasksForWorker ( $worker, integer $limit = 10 ) : array | mixed | null
$worker
$limit integer
return array | mixed | null
    public function getTasksForWorker($worker, $limit = 10)
    {
        return $this->findAll(['condition' => 'worker = :worker AND status = :status', 'params' => [':worker' => (int) $worker, ':status' => Queue::STATUS_NEW], 'limit' => (int) $limit, 'order' => 'priority, id asc']);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param int $limit
  */
 public function actionIndex($limit = 5)
 {
     $limit = (int) $limit;
     $this->log("Try process {$limit} mail tasks...");
     $queue = new Queue();
     $models = $queue->getTasksForWorker(self::MAIL_WORKER_ID, $limit);
     $this->log("Find " . count($models) . " new mail task");
     foreach ($models as $model) {
         $this->log("Process mail task id = {$model->id}");
         $data = $model->decodeJson();
         if (!$data) {
             $model->completeWithError('Error json_decode', CLogger::LEVEL_ERROR);
             $this->log("Error json_decode");
             continue;
         }
         if (!isset($data['from'], $data['to'], $data['theme'], $data['body'])) {
             $model->completeWithError('Wrong data...');
             $this->log('Wrong data...', CLogger::LEVEL_ERROR);
             continue;
         }
         $from = $this->from ? $this->from : $data['from'];
         $replyTo = isset($data['replyTo']) ? $data['replyTo'] : [];
         $sender = Yii::app()->getComponent($this->sender);
         if ($sender->send($from, $data['to'], $data['theme'], $data['body'], false, $replyTo)) {
             $model->complete();
             $this->log("Success send mail");
             continue;
         }
         $this->log('Error sending email', CLogger::LEVEL_ERROR);
     }
 }