예제 #1
0
 /**
  * @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
파일: apiv1.php 프로젝트: eric116/BotQueue
 public function api_createjob()
 {
     $queue = new Queue($this->args('queue_id'));
     if (!$queue->isHydrated()) {
         $queue = User::$me->getDefaultQueue();
     }
     if (!$queue->isHydrated()) {
         throw new Exception("Could not find a queue.");
     }
     if (!$queue->isMine()) {
         throw new Exception("This is not your queue.");
     }
     //get our quantity and make sure its at least 1.
     if ($this->args('quantity')) {
         /* @var $quantity int */
         $quantity = (int) $this->args('quantity');
     } else {
         $quantity = 1;
     }
     $quantity = max(1, $quantity);
     $quantity = min(100, $quantity);
     // there are 3 ways to create a job:
     // #1 - existing job id
     if ($this->args('job_id')) {
         $oldjob = new Job($this->args('job_id'));
         if (!$oldjob->isHydrated()) {
             throw new Exception("Job does not exist.");
         }
         if (!$oldjob->getQueue()->isMine()) {
             throw new Exception("This job is not in your queue.");
         }
         $file = $oldjob->getSourceFile();
         if (!$file->isHydrated()) {
             $file = $oldjob->getFile();
         }
         if (!$file->isHydrated()) {
             throw new Exception("No file found!");
         }
         $jobs = $queue->addFile($file, $quantity);
     } else {
         if ($this->args('job_url')) {
             //download our file.
             $url = $this->args('job_url');
             $data = Utility::downloadUrl($url);
             //does it match?
             if (!preg_match("/\\.(" . ACCEPTABLE_FILES . ")\$/i", $data['realname'])) {
                 throw new Exception("The file <a href=\"" . $url . "\">{$data['realname']}</a> is not valid for printing.");
             }
             //create our file object.
             $file = Storage::newFile();
             $file->set('user_id', User::$me->id);
             $file->set('source_url', $url);
             $file->upload($data['localpath'], StorageInterface::getNiceDir($data['realname']));
             //okay, create our jobs.
             $jobs = $queue->addFile($file, $quantity);
         } else {
             if (!empty($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
                 //upload our file
                 $file = $_FILES['file'];
                 $this->ensureGoodFile($file);
                 //does it match?
                 if (!preg_match("/\\.(" . ACCEPTABLE_FILES . ")\$/i", $file['name'])) {
                     throw new Exception("The file '{$file['name']}' is not valid for printing.");
                 }
                 //okay, we're good.. do it.
                 $data_file = Storage::newFile();
                 $data_file->set('user_id', User::$me->id);
                 $data_file->upload($file['tmp_name'], StorageInterface::getNiceDir($file['name']));
                 //okay, create our jobs.
                 $jobs = $queue->addFile($data_file, $quantity);
             } else {
                 throw new Exception("Unknown job creation method.");
             }
         }
     }
     Activity::log("created " . count($jobs) . " new " . Utility::pluralizeWord('job', count($jobs)) . " via the API.");
     //did we get a name?
     if ($this->args('name')) {
         foreach ($jobs as $job) {
             /* @var $job Job */
             $job->setName($this->args('name'));
             $job->save();
         }
     }
     //load our api data.
     $data = array();
     if (!empty($jobs)) {
         foreach ($jobs as $job) {
             $data[] = $job->getAPIData();
         }
     }
     return $data;
 }
예제 #3
0
파일: apiv1.php 프로젝트: ricberw/BotQueue
 public function api_createjob()
 {
     if ($this->args('queue_id')) {
         $queue = new Queue($this->args('queue_id'));
     } else {
         $queue = User::$me->getDefaultQueue();
     }
     if (!$queue->isHydrated()) {
         throw new Exception("Could not find a queue.");
     }
     if (!$queue->isMine()) {
         throw new Exception("This is not your queue.");
     }
     //get our quantity and make sure its at least 1.
     if ($this->args('quantity')) {
         $quantity = (int) $this->args('quantity');
     }
     $quantity = max(1, $quantity);
     $quantity = min(100, $quantity);
     // there are 3 ways to create a job:
     // #1 - existing job id
     if ($this->args('job_id')) {
         $oldjob = new Job($this->args('job_id'));
         if (!$oldjob->isHydrated()) {
             throw new Exception("Job does not exist.");
         }
         if (!$job->getQueue()->isMine()) {
             throw new Exception("This job is not in your queue.");
         }
         $file = $oldjob->getFile();
         if (!$file->isHydrated()) {
             throw new Exception("That job does not exist anymore.");
         }
         $jobs = $queue->addGCodeFile($file, $quantity);
     } else {
         if ($this->args('job_url')) {
             throw new Exception("Job add via URL is not implemented yet.");
         } else {
             if (!empty($_FILES['job_data']) && is_uploaded_file($_FILES['job_data']['tmp_name'])) {
                 throw new Exception("Job add via HTTP POST is not implemented yet.");
             } else {
                 throw new Exception("Unknown job creation method.");
             }
         }
     }
     Activity::log("created " . count($jobs) . " new " . Utility::pluralizeWord('job', count($jobs)) . " via the API.");
     $data = array();
     if (!empty($jobs)) {
         foreach ($jobs as $job) {
             $data[] = $job->getAPIData();
         }
     }
     return $data;
 }
예제 #4
0
파일: job.php 프로젝트: ricberw/BotQueue
 public function create()
 {
     $this->assertLoggedIn();
     if ($this->args('step2')) {
         $this->setTitle('Step 2 of 2: Create Job');
     } else {
         $this->setTitle('Create new Job');
     }
     try {
         if ($this->args('job_id')) {
             $job = new Job($this->args('job_id'));
             if (!$job->isHydrated()) {
                 throw new Exception("That job does not exist.");
             }
             if ($job->get('user_id') != User::$me->id) {
                 throw new Exception("You do not own this job.");
             }
             $file = $job->getFile();
             $queue_id = $job->get('queue_id');
         } else {
             $file = new S3File($this->args('file_id'));
         }
         if (!$file->isHydrated()) {
             throw new Exception("That file does not exist.");
         }
         if ($file->get('user_id') != User::$me->id) {
             throw new Exception("You do not have access to this file.");
         }
         $this->set('file', $file);
         //load up our form.
         $form = $this->_createJobForm($file, $queue_id);
         if (isset($job)) {
             $form->action = "/job/create/job:{$job->id}";
         } else {
             $form->action = "/job/create/file:{$file->id}";
         }
         //handle our form
         if ($form->checkSubmitAndValidate($this->args())) {
             //pull in our quantity
             $quantity = (int) $form->data('quantity');
             $quantity = max(1, $quantity);
             $quantity = min(1000, $quantity);
             //queue error checking.
             $queue = new Queue($form->data('queue_id'));
             if (!$queue->isHydrated()) {
                 throw new Exception("That queue does not exist.");
             }
             if (!$queue->canAdd()) {
                 throw new Exception("You do not have permission to add to that queue.");
             }
             //okay, we good?
             $queue->addGCodeFile($file, $quantity);
             Activity::log("added {$quantity} new " . Utility::pluralizeWord('job', $quantity));
             $this->forwardToUrl($queue->getUrl());
         }
         $this->set('form', $form);
     } catch (Exception $e) {
         $this->set('megaerror', $e->getMessage());
     }
 }