diff --git a/workflow/engine/src/ProcessMaker/Core/JobsManager.php b/workflow/engine/src/ProcessMaker/Core/JobsManager.php new file mode 100644 index 000000000..e77b0d00d --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Core/JobsManager.php @@ -0,0 +1,170 @@ +delay = $envs['delay']; + $this->tries = $envs['tries']; + $this->retryAfter = $envs['retry_after']; + + config(['queue.connections.database.retry_after' => $this->retryAfter]); + } + + /** + * This obtains a status of the current values that are running. The status + * of the values will be used by the Job at a future time when the job is + * launched. + * @return array + */ + private function getDataSnapshot() + { + $constants = get_defined_constants(true); + $session = $this->getSessionValues(); + return [ + 'errorReporting' => ini_get('error_reporting'), + 'configuration' => Propel::getConfiguration(), + 'constants' => $constants['user'], + 'session' => $session, + 'server' => $_SERVER, + ]; + } + + /** + * This sets the status of the values when the job is launched. Accepts the + * result of the execution of the getDataSnapshot() method. + * @param array $environment + */ + private function recoverDataSnapshot($environment) + { + $this->prepareEnvironment($environment); + + $_SESSION = $environment['session']; + $_SERVER = $environment['server']; + Propel::initConfiguration($environment['configuration']); + foreach ($environment['constants'] as $key => $value) { + if (!defined($key)) { + define($key, $value); + } + } + } + + /** + * This allows you to configure the PHP environment policies. The parameter + * must contain the correct indices. + * @param array $environment + */ + private function prepareEnvironment($environment) + { + ini_set('error_reporting', $environment['errorReporting']); + } + + /** + * This gets the values defined in the $this->sessionValues property from + * the current $_SESSION. + * @return array + */ + private function getSessionValues() + { + $result = []; + foreach ($this->sessionValues as $key) { + if (array_key_exists($key, $_SESSION)) { + $result[$key] = $_SESSION[$key]; + } + } + return $result; + } + + /** + * Dispatch a job to its appropriate handler. + * @param string $name + * @param Closure $callback + * @return object + */ + public function dispatch($name, $callback) + { + $environment = $this->getDataSnapshot(); + + $instance = Jobs::create($name, function() use ($callback, $environment) { + try { + $this->recoverDataSnapshot($environment); + $callback($environment); + } catch (Exception $e) { + Log::error($e->getMessage() . ": " . $e->getTraceAsString()); + } + }); + $instance->delay($this->delay); + + return $instance; + } +}