public function perform() { echo "\n\nPerforming job...\n"; $payload = $this->args; $body = $payload['body']; try { $url = $payload['endpoint']['url']; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); } catch (Exception $e) { // if we don't have success with response, or success with no response - re-queue this message $attempt = array_key_exists('attempts', $body) ? $body['attempts'] += 1 : 1; if ($attempt <= $this->retry_max) { $payload['body']['attempts'] = $attempt; $delay = 60 * $attempt * $attempt * $attempt; // retry after 1, 8, 27 minutes before giving up error_log("Exception raised: '" . $e->getMessage() . "' - Retrying job in {$delay}"); ResqueScheduler::enqueueIn($delay, "content", "PostSaved", $payload); } else { error_log("Exception raised: '" . $e->getMessage() . "' - Not retrying"); } } }
/** * @param $in * @param Job $job * @return null */ public function enqueueIn($in, Job $job) { if ($job instanceof ContainerAwareJob) { $job->setKernelOptions($this->kernelOptions); } $this->attachRetryStrategy($job); \ResqueScheduler::enqueueIn($in, $job->queue, \get_class($job), $job->args); return NULL; }
/** * Create a new scheduled job and save it to the specified queue. * * @param int $in Second count down to job. * @param string $queue The name of the queue to place the job in. * @param string $class The name of the class that contains the code to execute the job. * @param array $args Any optional arguments that should be passed when the job is executed. * * @return string */ public function enqueueJobIn($in, $queue, $class, $args = array()) { return ResqueScheduler::enqueueIn($in, $queue, $class, $args); }
/** * The method to run after a Job is finished. */ public function tearDown() { //Verificar si existen argumentos if (!empty($this->args)) { //Establecer variables self::$datetime = $this->args["datetime"]; self::$quequename = $this->args["quequename"]; self::$jobclassname = $this->args["jobclassname"]; self::$id_configuracion_reporte = $this->args["idNotificacionReporte"]; } //DB Conection self::$db = new Database(); self::$db->connect(); ///Fecha y Hora Actual //$current_datetime = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone('America/Panama')) $current_datetime = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))); //Calcular nueva fecha para programar la tarea //Sumarle 7 dias a la fecha de Ejecucion //self::$datetime = date('Y-m-d H:i:s', strtotime($current_datetime. ' + 7 days')); self::$datetime = date('Y-m-d H:i:s', strtotime($current_datetime . '+20 minute')); //Actualizar fecha ultima fecha de ejecucion de la tarea //y fecha de proxima ejcucion $fields = array("ultimo_tiempo_ejecucion" => $current_datetime, "proximo_tiempo_ejecucion" => self::$datetime); self::$db->update('configuracion_notificaciones_reportes', $fields, 'id="' . $this->args["idNotificacionReporte"] . '"'); $result = self::$db->getResult(); //print echo "Proxima Fecha de Ejecucion: " . self::$datetime . PHP_EOL; $horas = strtotime(self::$datetime) - strtotime($current_datetime); //Agendar nuevamente el job ResqueScheduler::enqueueIn($horas, self::$quequename, self::$jobclassname, ['datetime' => self::$datetime, 'quequename' => self::$quequename, 'jobclassname' => self::$jobclassname, 'idNotificacionReporte' => "" . $this->args["idNotificacionReporte"] . ""]); //print echo "Proxima Fecha en Horas: " . $horas . PHP_EOL; Resque_Event::listen('afterPerform', function () { //echo "Se envio el correo :)) "; }); Resque_Event::listen('onFailure', function () { echo "Algo paso :-| ...."; }); }
<?php //if(empty($argv[1])) { // die('Specify the name of a job to add. e.g, php queue.php PHP_Job'); //} $job = 'App\\Job\\GetDayCandles'; $job = 'App\\Job\\GetAccountInfo'; require '../../vendor/chrisboulton/php-resque/lib/Resque.php'; require '../../vendor/chrisboulton/php-resque-scheduler/lib/ResqueScheduler.php'; date_default_timezone_set('GMT'); Resque::setBackend('127.0.0.1:6379'); $in = 3; //one hour //$args = array( // 'time' => time(), // 'array' => array( // 'test' => 'test', // ), //); $args = array('time' => time(), 'userid' => 'not needed', 'oanda' => array('apiKey' => '', 'accountId' => '', 'serverType' => 'Demo')); ResqueScheduler::enqueueIn($in, 'default', $job, $args); //$jobId = Resque::enqueue('default', $job, $args, true); echo "Queued job \n\n";
/** * Create a new scheduled job and save it to the specified queue. * * @param int $in Second count down to job. * @param string $queue The name of the queue to place the job in. * @param string $class The name of the class that contains the code to execute the job. * @param array $args Any optional arguments that should be passed when the job is executed. * @param bool $track_status Enable track status * * @return string */ public function enqueueJobIn($in, $queue, $class, $args = array(), $track_status = false) { return ResqueScheduler::enqueueIn($in, $queue, $class, $args, $track_status); }