/** * 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} */ public function executeJob(Job $job, VirtualExecution $execution, ProcessEngine $engine) { if ($execution->isTerminated()) { throw new \RuntimeException(sprintf('%s is terminated', $execution)); } $data = (array) $job->getHandlerData(); $command = $data[self::PARAM_COMMAND]; if (!$command instanceof CommandInterface) { throw new \RuntimeException(sprintf('Expecting command, given %s', is_object($command) ? get_class($command) : gettype($command))); } // Move execution to start node if param is set. if (array_key_exists(self::PARAM_NODE_ID, $data)) { $execution->setNode($execution->getProcessModel()->findNode($data[self::PARAM_NODE_ID])); $engine->debug('Moved {execution} to node "{node}"', ['execution' => (string) $execution, 'node' => $execution->getNode()->getId()]); } $engine->debug('Executing async command {cmd} using {execution}', ['cmd' => get_class($command), 'execution' => (string) $execution]); $engine->pushCommand($command); }
/** * {@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); }
protected function unserializeJob(array $row) { $job = new Job($row['id'], $row['execution_id'], $row['handler_type'], unserialize(BinaryData::decode($row['handler_data'])), new \DateTimeImmutable('@' . $row['created_at']), $row['retries'], $row['lock_owner']); $job->setExternalId($row['external_id']); if ($row['scheduled_at'] !== NULL) { $job->setScheduledAt(new \DateTimeImmutable('@' . $row['scheduled_at'], new \DateTimeZone('UTC'))); } if ($row['run_at'] !== NULL) { $job->setRunAt(new \DateTimeImmutable('@' . $row['run_at'], new \DateTimeZone('UTC'))); } if ($row['locked_at'] !== NULL) { $job->setLockedAt(new \DateTimeImmutable('@' . $row['locked_at'], new \DateTimeZone('UTC'))); } $job->setExceptionType($row['exception_type']); $job->setExceptionMessage($row['exception_message']); if ($row['exception_data'] !== NULL) { $job->setExceptionData(unserialize(BinaryData::decode($row['exception_data']))); } $locked = false; if ($row['lock_owner'] !== NULL && $row['locked_at'] !== NULL) { if ($row['locked_at'] > time() - $this->engine->getJobExecutor()->getLockTimeout()) { $locked = true; } } $job->setLocked($locked); return $job; }