/** * Create a new job and save it to the specified queue. * * @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 boolean $monitor Set to true to be able to monitor the status of a job. * * @return string */ public static function create($queue, $class, $args = null, $monitor = false) { if ($args !== null && !is_array($args)) { throw new InvalidArgumentException('Supplied $args must be an array.'); } $new = true; if (isset($args['id'])) { $id = $args['id']; unset($args['id']); $new = false; } else { $id = md5(uniqid('', true)); } if (empty($args)) { $args = []; } Resque::push($queue, array('class' => $class, 'args' => array($args), 'id' => $id)); if ($monitor) { if ($new) { Resque_Job_Status::create($id); } else { $statusInstance = new Resque_Job_Status($id); $statusInstance->update($id, Resque_Job_Status::STATUS_WAITING); } } return $id; }
/** * Create a new job and save it to the specified queue. * * @param string $queue The name of the queue to place the job in. * @param string|callback $method The name of the class that contains the code to execute the job, or an array containing a class name and method name * @param array $args Any optional arguments that should be passed when the job is executed. * @param boolean $monitor Set to true to be able to monitor the status of a job. * @param string $include Path to a file to include before attempting to execute the callback */ public static function create($queue, $method, array $args=array(), $monitor = false, $include = '') { if (is_array($method)) { $class = $method[0]; $method = $method[1]; } else { $class = ''; } $id = md5(uniqid('', true)); $payload = array( 'class' => $class, 'method' => $method, 'args' => $args, 'id' => $id, 'include' => $include, ); Resque::push($queue, $payload); if($monitor) { Resque_Job_Status::create($id); } return $id; }
/** * Create a new job and save it to the specified queue. * * @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 boolean $monitor Set to true to be able to monitor the status of a job. */ public static function create($queue, $class, $args = null, $monitor = false) { if ($args !== null && !is_array($args)) { throw new InvalidArgumentException('Supplied $args must be an array.'); } $id = md5(uniqid('', true)); Resque::push($queue, array('class' => $class, 'args' => $args, 'id' => $id)); if ($monitor) { Resque_Job_Status::create($id); } return $id; }
/** * Create a new job and save it to the specified queue. * * @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 boolean $monitor Set to true to be able to monitor the status of a job. * @param string $id Unique identifier for tracking the job. Generated if not supplied. * * @return string */ public static function create($queue, $class, $args = null, $monitor = false, $id = null) { if (is_null($id)) { $id = Resque::generateJobId(); } if ($args !== null && !is_array($args)) { throw new InvalidArgumentException('Supplied $args must be an array.'); } Resque::push($queue, array('class' => $class, 'args' => array($args), 'id' => $id, 'queue_time' => microtime(true))); if ($monitor) { Resque_Job_Status::create($id); } return $id; }
public static function create($queue, $class, $args = null, $monitor = false) { if ($args !== null && !is_array($args)) { throw new InvalidArgumentException('Supplied $args must be an array.'); } $id = md5(uniqid('', true)); $data = array('class' => $class, 'args' => array($args), 'id' => $id, 'closure' => true, 'queue_time' => microtime(true)); Log::info('Push closure:' . json_encode($data)); Resque::push($queue, $data); if ($monitor) { Resque_Job_Status::create($id); } return $id; }
/** * Create a new job and save it to the specified queue. * * @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 boolean $monitor Set to true to be able to monitor the status of a job. * * @return string */ public static function create($queue, $class, $args = null, $monitor = false) { if ($args !== null && !is_array($args)) { throw new InvalidArgumentException('Supplied $args must be an array.'); } // uniqid — 生成一个唯一ID,一个带前缀、基于当前时间微秒数的唯一ID $id = md5(uniqid('', true)); // 将相应的类名/参数和id入队 Resque::push($queue, array('class' => $class, 'args' => array($args), 'id' => $id)); if ($monitor) { // 创建job的状态,以便监控这个job Resque_Job_Status::create($id); } return $id; }
/** * @return string|void * @throws sfStopException */ public function executeEdit() { /** @var \FileImportHistory file_import_history */ $this->file_import_history = $this->getFileImportHistoryOrCreate(); $this->labels = $this->getLabels(); if ($this->getRequest()->getMethod() == sfRequest::POST) { $this->updateFileImportHistoryFromRequest(); //need an id if ($this->file_import_history->getVocabularyId()) { $schemaId = $this->file_import_history->getVocabularyId(); $type = 'vocabulary'; } else { $schemaId = $this->file_import_history->getSchemaId(); $type = 'schema'; } $filePath = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . $this->file_import_history->getFileName(); $import = new ImportVocab($type, $filePath, $schemaId); $prolog = $import->processProlog(); if (!$prolog) { $message = "Something went seriously wrong and we couldn't process your file at all (wish we could be more helpful)"; return $this->handleImportError($message, $filePath); } $type_id = 'vocabulary' === $type ? 'vocabulary_id' : 'schema_id'; //check to make sure that if there's a schema_id in the prolog that it matches the current ID if (isset($prolog['meta'][$type_id])) { if (is_array($prolog['meta'][$type_id])) { $message = "You have a duplicate of one of the prolog columns ('reg_id', 'uri', 'type') in your data<br />We can't process the file until it's removed."; return $this->handleImportError($message, $filePath); } if ($prolog['meta'][$type_id] != $schemaId) { $message = "The " . $type . "_id in the file you are importing (" . $prolog['meta'][$type_id] . ") does not match the Element Set Id of this\n Element Set (" . $schemaId . ")<br />You may be trying to import into the wrong Element Set?"; return $this->handleImportError($message, $filePath); } } else { $message = "Your file is missing a " . $type . "_id entry in the 'meta' section and we won't process it without one"; return $this->handleImportError($message, $filePath); } //todo identify and warn of more processing errors with the prolog //check to make sure the user is an admin /** @var myUser $user */ $user = $this->getUser(); if (!$user->hasCredential(array(0 => array(0 => 'administrator', 1 => 'schemaadmin', 2 => 'vocabularyadmin')))) { $message = 'You must be an administrator of this Element Set to import.'; return $this->handleImportError($message, $filePath); } $this->file_import_history->setResults("Queued for processing."); $this->saveFileImportHistory($this->file_import_history); $this->setFlash('notice', 'Your file has been accepted and queued for processing. Check back in a few minutes for the results'); unset($import); $environment = SF_ENVIRONMENT; $importId = $this->file_import_history->getId(); //todo it's at this point that we push this onto a queue for processing $job = Resque::push('ImportVocab\\ImportJob', array($schemaId, $filePath, $importId, $environment, $type)); $job2 = Resque::push('ImportVocab\\UpdateRelatedJob', array($environment, $importId)); return $this->redirect('import/show?id=' . $importId); } }
public function runJob($job, array $data = null, $queue = null) { return \Resque::push($job, $data, $queue); }
<?php use Resque\Event; use Resque\Logger; // Test job class class TestJob { public function perform($args) { // Don't do anything } } // Lets record the forking time Event::listen(array(Event::WORKER_FORK, Event::WORKER_FORK_CHILD), function ($event, $job) use($logger) { static $start = 0; if ($event === Event::WORKER_FORK_CHILD) { $exec = microtime(true) - $start; $logger->log('Forking process took ' . round($exec * 1000, 2) . 'ms', Logger::DEBUG); } else { $start = microtime(true); } }); // When the job is about to be run, queue another one Event::listen(Event::JOB_PERFORM, function ($event, $job) use($logger) { Resque::push('TestJob'); }); // Add a few jobs to the default queue for ($i = 0; $i < 10; $i++) { Resque::push('TestJob'); }
break; case 'failnoclass': $job = Resque::push('\\Does\\Not\\Exist', array()); break; case 'faillong': $job = Resque::push('FailLong', array()); break; case 'failexception': $job = Resque::push('FailException', array()); break; case 'failerror': $job = Resque::push('FailError', array()); break; case 'closure': $job = Resque::push(function ($job) { echo 'This is an inline job! #' . $job->getId() . PHP_EOL; }); break; case 'closure-delayed': $job = Resque::later(mt_rand(0, 30), function ($job) { echo 'This is a delayed inline job! #' . $job->getId() . PHP_EOL; }); break; } } if ($job) { header('Location: ?id=' . $job->getId()); exit; } echo '<pre><h1><a href="?">php-resque</a></h1><ul>' . '<li><a href="?action=reset">Reset</a></li>' . '<li><a href="?action=push">Push new job</a></li>' . '<li><a href="?action=delayed">Delayed job</a></li>' . '<li><a href="?action=delayedat">Delayed job in 2 mins</a></li>' . '<li><a href="?action=longrunning">Long running job</a></li>' . '<li><a href="?action=faillong">Fail due to running too long</a></li>' . '<li><a href="?action=failnoclass">Fail due to no class being found</a></li>' . '<li><a href="?action=failexception">Fail due to exception</a></li>' . '<li><a href="?action=failerror">Fail due to fatal error</a></li>' . '<li><a href="?action=closure">Push closure</a></li>' . '<li><a href="?action=closure-delayed">Delayed closure</a></li>' . '</ul>'; $rep = 150;