daemon = Supervisor::getDaemon(); $this->token = Security::generateRandomString(); $this->request_payload = $request_payload; $this->timer = microtime(true); } function run() { $daemon = $this->daemon; if(!$daemon) { $this->abortWithError(__('Daemon does not exist.')); } if(!isset($this->request_payload['token']) || $this->request_payload['token'] !== $daemon['token'] ) { $this->abortWithError(__('Invalid or missing token.')); } $this->abortIfStopped($daemon); try { $sending_queue = new SendingQueue($this->timer); $sending_queue->process(); } catch(Exception $e) { } $elapsed_time = microtime(true) - $this->timer; if($elapsed_time < 30) { sleep(30 - $elapsed_time); } // after each execution, re-read daemon data in case its status has changed $daemon = Supervisor::getDaemon(); // if the token has changed, abort further processing if ($daemon['token'] !== $this->request_payload['token']) { exit; } $daemon['counter']++; $this->abortIfStopped($daemon); if($daemon['status'] === 'starting') { $daemon['status'] = 'started'; } $daemon['token'] = $this->token; Supervisor::saveDaemon($daemon); $this->callSelf(); } function abortIfStopped($daemon) { if($daemon['status'] === 'stopped') exit; if($daemon['status'] === 'stopping') { $daemon['status'] = 'stopped'; Supervisor::saveDaemon($daemon); exit; } } function callSelf() { $payload = serialize(array('token' => $this->token)); Supervisor::accessRemoteUrl( '/?mailpoet-api§ion=queue&action=run&request_payload=' . base64_encode($payload) ); exit; } function abortWithError($message) { exit('[mailpoet_cron_error:' . base64_encode($message) . ']'); } }