Inheritance: implements Serializable, use trait Cronario\TraitOptions
Example #1
0
 /**
  * @param AbstractJob $job
  *
  * @return bool
  * @throws \Cronario\Exception\JobException
  */
 public function save(AbstractJob $job)
 {
     $data = $job->getData();
     if (!$job->isStored()) {
         $job->setId(uniqid());
     }
     $this->redis->set($this->namespace . $job->getId(), json_encode($data));
     return true;
 }
Example #2
0
 /**
  * @param AbstractJob $job
  *
  * @return mixed
  * @throws \Cronario\Exception\JobException
  */
 public function save(AbstractJob $job)
 {
     $data = $job->getData();
     if ($job->isStored()) {
         $data['_id'] = new \MongoId($data[AbstractJob::P_ID]);
         unset($data[AbstractJob::P_ID]);
     } else {
         $data['_id'] = new \MongoId();
         $job->setId((string) $data['_id']);
     }
     $result = static::getCollection()->update(array('_id' => $data['_id']), array('$set' => $data), array('upsert' => true));
     return $result;
 }
Example #3
0
 /**
  * @param AbstractJob|Job $job
  *
  * @return array
  */
 protected function send(AbstractJob $job)
 {
     $result = ['errors' => [], 'vendor_id' => null];
     try {
         $transport = $this->getTransport();
         $smsMessage = new \infobip\models\SMSRequest();
         $smsMessage->senderAddress = $job->getSender();
         $smsMessage->address = $job->getRecipient();
         $smsMessage->message = $job->getText();
         $response = $transport->sendSMS($smsMessage);
         $result['vendor_id'] = $response->clientCorrelator;
         if ($response->exception != null) {
             $result['errors'][] = $response->exception;
         }
     } catch (\Exception $ex) {
         $result['errors'][] = $ex->getMessage();
     }
     return $result;
 }
Example #4
0
 /**
  * @param AbstractJob $parentJob
  *
  * @return ResultException|null
  */
 public function __invoke(AbstractJob $parentJob = null)
 {
     if (null !== $parentJob) {
         $this->setParentId($parentJob->getId())->setAuthor($parentJob->getAuthor());
     }
     $this->save();
     try {
         if ($this->isSync()) {
             $worker = AbstractWorker::factory($this->getWorkerClass());
             $worker($this);
         } else {
             $this->setResult(new ResultException(ResultException::R_QUEUED));
             $this->putIntoQueue();
             $this->save();
         }
     } catch (\Exception $ex) {
         $this->setResult($ex);
         $this->save();
     }
     return $this->getResult();
 }
Example #5
0
 /**
  * @param $jobId
  * @param $appId
  *
  * @return mixed
  */
 public static function actionJobGetResult($jobId, $appId = Producer::DEFAULT_APP_ID)
 {
     // sanitize user inputs
     $jobId = filter_var($jobId, FILTER_SANITIZE_STRING);
     $appId = filter_var($appId, FILTER_SANITIZE_STRING);
     /** @var AbstractJob $job */
     $job = AbstractJob::find($jobId, $appId);
     if (is_object($job)) {
         $result = $job->getResult();
         if ($result instanceof ResultException) {
             $response[self::P_RESPONSE_MSG] = 'Job result ' . $jobId;
             $response[self::P_RESPONSE_RESULT] = $result->toArray(true);
         } else {
             $response[self::P_RESPONSE_MSG] = 'Job result not ready : ' . $jobId;
         }
     } else {
         $response[self::P_RESPONSE_OK] = false;
         $response[self::P_RESPONSE_MSG] = 'Job not exists : ' . $jobId;
     }
     return $response;
 }
Example #6
0
 /**
  * @param AbstractJob $job
  *
  * @return $this
  */
 protected function invokeCallbacks(AbstractJob $job)
 {
     $result = $job->getResult();
     if ($result->isRedirect() || $result->isRetry()) {
         return $this;
     }
     if ($result->isError()) {
         $callbackJobs = $job->getCallbacksError();
     } else {
         $callbackJobs = $job->getCallbacksDone();
         if ($result->isSuccess()) {
             $callbackJobs = $callbackJobs + $job->getCallbacksSuccess();
         } else {
             $callbackJobs = $callbackJobs + $job->getCallbacksError();
         }
     }
     if (!is_array($callbackJobs) || count($callbackJobs) === 0) {
         return $this;
     }
     foreach ($callbackJobs as $index => $callbackJob) {
         /** @var $callbackJob AbstractJob */
         $callbackJob($job);
     }
     return $this;
 }
Example #7
0
 /**
  * @param AbstractJob $job
  *
  * @throws ResultException
  */
 protected function doJob(AbstractJob $job)
 {
     /** @var $job Job */
     $resultData = null;
     try {
         $curl = new CurlWrapper();
         $curl->request($job->getUrl(), $job->getMethod(), $job->getRequestParams());
         $response['content'] = $curl->getResponse();
         $response['info'] = $curl->getTransferInfo();
     } catch (CurlWrapperCurlException $a) {
         $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
         throw new ResultException(ResultException::ERROR_CURL);
     } catch (CurlWrapperException $a) {
         $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
         throw new ResultException(ResultException::ERROR_CURL);
     } catch (\Exception $a) {
         $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
         throw new ResultException(ResultException::ERROR_CURL);
     }
     /**
      * saving data
      */
     if ($job->getSaveContent()) {
         $resultData['content'] = $response['content'];
     }
     if ($job->getSaveInfo()) {
         $resultData['info'] = $response['info'];
     }
     /**
      * analise response
      */
     if ($job->getExpectCode() && $job->getExpectCode() != $response['info']['http_code']) {
         throw new ResultException(ResultException::RETRY_EXPECTED_HTTP_CODE, $resultData);
     } elseif ($job->getExpectContent() && !$this->isContain($job->getExpectContent(), $response['content'])) {
         throw new ResultException(ResultException::RETRY_EXPECTED_CONTENT, $resultData);
     }
     /**
      * success result
      */
     throw new ResultException(ResultException::R_SUCCESS, $resultData);
 }
Example #8
0
 /**
  * @param AbstractJob|Job $job
  *
  * @throws ResultException
  */
 protected function doJob(AbstractJob $job)
 {
     $job->addDebugData('event', 'workerDoJob');
     // generate getter config =====================================================
     $job->addDebugData('getConfig', static::getConfig());
     $job->addDebugData('getConfigSuperKey', static::getConfig('superKey'));
     // generate response ==========================================================
     $sleep = $job->getSleep() ?: 0;
     $job->addDebugData('event', "before sleep {$sleep} / time: " . time());
     sleep($sleep);
     $job->addDebugData('event', "after sleep {$sleep} / time: " . time());
     $response = ['uniqid' => uniqid()];
     $job->addDebugData('response', $response);
     $resultData = $response;
     // generate new ResultException ===============================================
     $expectedResult = $job->getExpectedResult();
     $job->addDebugData('expect', $expectedResult);
     $job->addDebugData('event', "now will get ResultException ...");
     if ($expectedResult === Job::P_PARAM_EXPECTED_RESULT_T_SUCCESS) {
         $job->addDebugData('throw', "code " . ResultException::R_SUCCESS);
         throw new ResultException(ResultException::R_SUCCESS, $resultData);
     }
     if ($expectedResult === Job::P_PARAM_EXPECTED_RESULT_T_FAILURE) {
         $job->addDebugData('throw', "code " . ResultException::FAILURE_XXX);
         throw new ResultException(ResultException::FAILURE_XXX);
     }
     if ($expectedResult === Job::P_PARAM_EXPECTED_RESULT_T_ERROR) {
         $job->addDebugData('throw', "code " . ResultException::ERROR_XXX);
         throw new ResultException(ResultException::ERROR_XXX, $resultData);
     }
     if ($expectedResult === Job::P_PARAM_EXPECTED_RESULT_T_RETRY) {
         $newWorkerClass = $job->getWorkerClass();
         $job->setWorkerClass($newWorkerClass)->save();
         $job->addDebugData('setWorkerClass', $newWorkerClass);
         $job->addDebugData('throw', "code " . ResultException::RETRY_XXX);
         throw new ResultException(ResultException::RETRY_XXX, $resultData);
     }
     if ($expectedResult === Job::P_PARAM_EXPECTED_RESULT_T_REDIRECT) {
         $newWorkerClass = $job->getWorkerClass();
         $job->setWorkerClass($newWorkerClass)->save();
         $job->addDebugData('setWorkerClass', $newWorkerClass);
         $job->addDebugData('throw', "code " . ResultException::REDIRECT_XXX);
         throw new ResultException(ResultException::REDIRECT_XXX, $resultData);
     }
     $job->addDebugData('throw', "code " . ResultException::ERROR_PARAM_EXPECTED_RESULT);
     throw new ResultException(ResultException::ERROR_PARAM_EXPECTED_RESULT, $resultData);
 }