コード例 #1
0
ファイル: bot.php プロジェクト: JoonasMelin/BotQueue
 /**
  * @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;
 }
コード例 #2
0
<?php

require "../extensions/global.php";
$start_time = microtime(true);
//delete our dev db.
db()->execute("TRUNCATE TABLE job_clock");
$rs = db()->query("SELECT * FROM jobs");
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
    $log = new JobClockEntry();
    $log->set('job_id', $row['id']);
    $log->set('bot_id', $row['bot_id']);
    $log->set('user_id', $row['user_id']);
    $log->set('queue_id', $row['queue_id']);
    if ($row['status'] == 'complete' || $row['status'] == 'failure' || $row['status'] == 'qa') {
        $log->set('start_date', $row['taken_time']);
        $log->set('end_date', $row['finished_time']);
        $log->setStatus('complete');
        $log->save();
    } else {
        if ($row['status'] == 'taken' || $row['status'] == 'slicing') {
            $log->set('start_date', $row['taken_time']);
            $log->setStatus('working');
            $log->save();
        } else {
            if ($row['status'] == 'cancelled') {
                $log->set('start_date', $row['taken_time']);
                $log->set('end_date', $row['finished_time']);
                $log->setStatus('dropped');
                $log->save();
            }
        }