/** * @param $job Job * @param $can_slice bool * @return Job * @throws Exception */ public function grabJob($job, $can_slice = true) { $grabAttemptSQL = "\n UPDATE jobs\n SET bot_id =\n CASE\n WHEN bot_id=0\n THEN\n ?\n ELSE\n bot_id\n END\n WHERE id = ?\n "; // Attempt to grab the job unless another bot already has db()->execute($grabAttemptSQL, array($this->id, $job->id)); $job = new Job($job->id); // Reload the job if ($job->get('bot_id') != $this->id) { // We didn't grab it in time. throw new Exception("Unable to lock job #{$job->id}"); } $job->setStatus('taken'); $job->set('taken_time', date('Y-m-d H:i:s')); $job->save(); //do we need to slice this job? if (!$job->getFile()->isHydrated() && $can_slice) { //pull in our config and make sure it's legit. $config = $this->getSliceConfig(); if (!$config->isHydrated()) { $job->setStatus('available'); $job->set('bot_id', 0); $job->set('taken_time', 0); $job->save(); throw new Exception("This bot does not have a slice engine + configuration set."); } //is there an existing slice job w/ this exact file and config? $sj = SliceJob::byConfigAndSource($config->id, $job->get('source_file_id')); if ($sj->isHydrated()) { //update our job status. $job->set('slice_job_id', $sj->id); $job->set('slice_complete_time', $job->get('taken_time')); $job->set('file_id', $sj->get('output_id')); $job->save(); } else { //nope, create our slice job for processing. $sj->set('user_id', User::$me->id); $sj->set('job_id', $job->id); $sj->set('input_id', $job->get('source_file_id')); $sj->set('slice_config_id', $config->id); $sj->set('slice_config_snapshot', $config->getSnapshot()); $sj->set('add_date', date("Y-m-d H:i:s")); $sj->setStatus('available'); $sj->save(); //update our job status. $job->setStatus('slicing'); $job->set('slice_job_id', $sj->id); $job->save(); } } $log = new JobClockEntry(); $log->set('job_id', $job->id); $log->set('user_id', User::$me->id); $log->set('bot_id', $this->id); $log->set('queue_id', $job->get('queue_id')); $log->set('start_date', date("Y-m-d H:i:s")); $log->setStatus('working'); $log->save(); $this->set('job_id', $job->id); $this->setStatus(BotState::Working); $this->set('last_seen', date("Y-m-d H:i:s")); $this->save(); return $job; }
public function job_update() { $this->set('area', 'jobs'); try { //load the data and check for errors. $job = new SliceJob($this->args('id')); if (!$job->isHydrated()) { throw new Exception("That slice job does not exist."); } if ($job->get('user_id') != User::$me->id) { throw new Exception("You do not have access to view this slice job."); } if ($job->get('status') != 'pending') { throw new Exception("This slice job is not in a pending state."); } if ($this->args('pass')) { $job->pass(); } if ($this->args('fail')) { $job->fail(); } $this->forwardToUrl($job->getUrl()); } catch (Exception $e) { $this->setTitle("Slice Job - Error"); $this->set('megaerror', $e->getMessage()); } }
public function grab($uid) { if ($this->get('status') == 'available') { $this->setStatus('slicing'); $this->set('taken_date', date('Y-m-d H:i:s')); $this->set('uid', $uid); $this->save(); // Begin a transaction to avoid the race condition db()->beginTransaction(); $sj = new SliceJob($this->id); if ($sj->get('uid') != $uid) { db()->rollBack(); // Nothing really to rollback throw new Exception("Unable to lock slice job #{$this->id}"); } db()->commit(); $bot = $this->getBot(); $bot->setStatus(BotState::Slicing); $bot->save(); } }
public function api_updateslicejob() { if (!$this->args('job_id')) { throw new Exception("You must provide the 'bot_id' parameter."); } $sj = new SliceJob($this->args('job_id')); if (!$sj->isHydrated()) { throw new Exception("Slice job does not exist."); } if ($sj->get('user_id') != User::$me->id) { throw new Exception("This slice job is not yours."); } //load up our objects $job = $sj->getJob(); //upload our file $file = $_FILES['file']; $emptyFile = false; try { $this->ensureGoodFile($file); } catch (InvalidUploadedFile $ex) { if ($ex->getCode() == UPLOAD_ERR_EMPTY) { $emptyFile = true; } else { throw $ex; } } $data_file = Storage::newFile(); if (!$emptyFile) { //okay, we're good.. do it. $data_file->set('user_id', User::$me->id); $data_file->upload($file['tmp_name'], StorageInterface::getNiceDir($file['name'])); } //update our status. $sj->set('output_log', $this->args('output')); $sj->set('error_log', $this->args('errors')); if (!$emptyFile) { $sj->set('output_id', $data_file->id); } $sj->save(); //update our job $job->set('slice_complete_time', date("Y-m-d H:i:s")); if (!$emptyFile) { $job->set('file_id', $data_file->id); } $job->save(); //what do do with it now? if ($this->args('status') == 'complete') { $sj->pass(); } else { if ($this->args('status') == 'failure') { $sj->fail(); } else { if ($this->args('status') == 'pending') { $sj->setStatus('pending'); $sj->set('finish_date', date("Y-m-d H:i:s")); $sj->save(); } } } Activity::log($job->getBot()->getLink() . " sliced the " . $job->getLink() . " job via the API."); return $job->getBot()->getAPIData(); }