/** * Marks the job is being scheduled by writing a timestamp to the DB. * * @param Job $job */ protected function markJobAsScheduled(Job $job, $externalId = NULL) { $stmt = $this->engine->prepareQuery("UPDATE `#__bpmn_job` SET `scheduled_at` = :scheduled, `external_id` = :ex WHERE `id` = :id"); $stmt->bindValue('scheduled', time()); $stmt->bindValue('ex', $externalId === NULL ? NULL : (string) $externalId); $stmt->bindValue('id', $job->getId()); $stmt->execute(); }
/** * {@inheritdoc} */ protected function executeCommand(ProcessEngine $engine) { $execution = $engine->findExecution($this->job->getExecutionId()); $engine->debug('Executing job <{job}> using handler "{handler}" ({impl}) within {execution}', ['job' => (string) $this->job->getId(), 'handler' => $this->handler->getType(), 'impl' => get_class($this->handler), 'execution' => (string) $execution]); try { $this->handler->executeJob($this->job, $execution, $engine); // Delete job when it has been completed successfully. $engine->getConnection()->delete('#__bpmn_job', ['id' => $this->job->getId()]); } catch (\Exception $e) { $engine->warning('Job <{job}> failed with exception {exception}: "{message}"', ['job' => (string) $this->job->getId(), 'exception' => get_class($e), 'message' => $e->getMessage()]); $stmt = $engine->prepareQuery("\n\t\t\t\tUPDATE `#__bpmn_job`\n\t\t\t\tSET `retries` = `retries` - 1,\n\t\t\t\t\t`scheduled_at` = NULL,\n\t\t\t\t\t`lock_owner` = NULL,\n\t\t\t\t\t`locked_at` = NULL,\n\t\t\t\t\t`exception_type` = :type,\n\t\t\t\t\t`exception_message` = :message,\n\t\t\t\t\t`exception_data` = :data\n\t\t\t\tWHERE `id` = :id\n\t\t\t"); $stmt->bindValue('id', $this->job->getId()); $stmt->bindValue('type', get_class($e)); $stmt->bindValue('message', (string) $e->getMessage()); $stmt->bindValue('data', new BinaryData(serialize($e->getTraceAsString()))); $stmt->execute(); } }
/** * Find a handler for the given job by matching the handler type value of the job. * * @param Job $job * @return JobHandlerInterface * * @throws \OutOfBoundsException When no handler for the job could be resolved. */ protected function findJobHandler(Job $job) { foreach ($this->handlers as $type => $handler) { if ($job->getHandlerType() == $type) { return $handler; } } throw new \OutOfBoundsException(sprintf('Job handler "%s" not found for job %s', $job->getHandlerType(), $job->getId())); }
/** * Create an event subscription entry in the DB. * * @param ProcessEngine $engine * @param Job $job */ protected function createSubscription(ProcessEngine $engine, Job $job = NULL) { $execution = $engine->findExecution($this->executionId); $nodeId = $this->nodeId === NULL ? NULL : $execution->getProcessModel()->findNode($this->nodeId)->getId(); $data = ['id' => UUID::createRandom(), 'execution_id' => $execution->getId(), 'activity_id' => $this->activityId, 'node' => $nodeId, 'process_instance_id' => $execution->getRootExecution()->getId(), 'flags' => $this->getSubscriptionFlag(), 'boundary' => $this->boundaryEvent ? 1 : 0, 'name' => $this->name, 'created_at' => time()]; if ($job !== NULL) { $data['job_id'] = $job->getId(); } $engine->getConnection()->insert('#__bpmn_event_subscription', $data); }