Beispiel #1
0
 public function testFlushDeadJobs()
 {
     $this->testScheduler->fire();
     $this->testScheduler->status = 'In Progress';
     $this->testScheduler->save();
     $this->assertEquals('In Progress', $this->testScheduler->status, "Assert that the test scheduler instance has status of 'In Progress'");
     $result = $GLOBALS['db']->query("SELECT id FROM schedulers_times WHERE scheduler_id ='{$this->testScheduler->id}'");
     $jobCount = 0;
     while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
         $job = new SchedulersJob();
         $job->retrieve($row['id']);
         $this->assertEquals('completed', $job->status, "Assert that schedulers_times status is set to 'completed'");
         $job->execute_time = $this->testScheduler->date_time_start;
         //Set this to the start time of the scheduler which is in year 2005
         $job->save();
         $jobCount++;
     }
     $this->assertTrue($jobCount > 0, "Assert that we created schedulers_times entries");
     $this->testScheduler->flushDeadJobs();
     $this->testScheduler->retrieve($this->testScheduler->id);
     $this->assertEquals('Active', $this->testScheduler->status, "Assert that the status for scheduler is set to 'Active'");
     $result = $GLOBALS['db']->query("SELECT id FROM schedulers_times WHERE scheduler_id ='{$this->testScheduler->id}'");
     while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
         $job = new SchedulersJob();
         $job->retrieve($row['id']);
         $this->assertEquals('failed', $job->status, "Assert that schedulers_times status is set to 'failed'");
     }
 }
 protected function createJob($data)
 {
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     foreach ($data as $key => $val) {
         $job->{$key} = $val;
     }
     $job->save();
     $this->jobs[] = $job->id;
     return $job;
 }
Beispiel #3
0
 /**
  * Dispatch request.
  * @param SugarBean $seed a bean that fired event
  * @param string $event event name
  * @param array $arguments event arguments 
  * @param string $id web logic hook id
  */
 public function dispatchRequest(SugarBean $seed, $event, $arguments, $id)
 {
     $this->retrieve($id);
     if (empty($this->id)) {
         return;
     }
     $jobData = array('url' => $this->url, 'request_method' => $this->request_method, 'payload' => $this->formatRequestData($seed, $event, $arguments));
     $job = new SchedulersJob();
     $job->assigned_user_id = $this->created_by;
     $job->name = 'Dispatch Web Logic Hook';
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->target = 'class::' . get_class($this);
     $job->data = serialize($jobData);
     $job->save();
 }
Beispiel #4
0
 /**
  * Submit a new job to the queue
  *
  * @param SchedulersJob $job Job object
  *
  * @return string            Job ID
  */
 public function submitJob($job)
 {
     $job->id = create_guid();
     $job->new_with_id = true;
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->resolution = SchedulersJob::JOB_PENDING;
     if (empty($job->execute_time)) {
         $job->execute_time = $GLOBALS['timedate']->nowDb();
     }
     if (empty($job->assigned_user_id)) {
         $job->assigned_user_id = $GLOBALS['current_user']->id;
     }
     $job->save();
     return $job->id;
 }
Beispiel #5
0
 public function testCleanup()
 {
     $job = new SchedulersJob();
     $job->update_date_modified = false;
     $job->status = SchedulersJob::JOB_STATUS_RUNNING;
     $job->scheduler_id = 'unittest';
     $job->execute_time = $GLOBALS['timedate']->nowDb();
     $job->date_entered = $GLOBALS['timedate']->getNow()->modify("-1 day")->asDb();
     $job->date_modified = $GLOBALS['timedate']->getNow()->modify("-10 minutes")->asDb();
     $job->name = "Unit Test Job";
     $job->target = "test::test";
     $job->save();
     $job_id = $job->id;
     $this->jq->cleanup();
     $job = new SchedulersJob();
     $job->retrieve($job_id);
     // Cleanup will always set job resolution to JOB_FAILURE when a job is cleaned
     $this->assertNotEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
 }
Beispiel #6
0
 public function testJobsCleanup()
 {
     // job 1 - oldest, should be executed
     $job = new SchedulersJob();
     $job->update_date_modified = false;
     $job->status = SchedulersJob::JOB_STATUS_RUNNING;
     $job->scheduler_id = 'unittest';
     $job->execute_time = TimeDate::getInstance()->nowDb();
     $job->date_entered = '2010-01-01 12:00:00';
     $job->date_modified = '2010-01-01 12:00:00';
     $job->name = "Unit test Job 1";
     $job->target = "function::CronTest::cronJobFunction";
     $job->assigned_user_id = $GLOBALS['current_user']->id;
     $job->save();
     $jobid1 = $job->id;
     $this->jq->min_interval = 0;
     // disable throttle
     $this->jq->disable_schedulers = true;
     $this->jq->runCycle();
     $this->assertFalse(self::$jobCalled, "Job was called");
     $this->assertFalse($this->jq->runOk(), "Wrong OK flag");
     $job = new SchedulersJob();
     $job->retrieve($jobid1);
     $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
     $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
     $this->assertEmpty(session_id(), "Session not destroyed");
 }
 /**
  * Fetch the next job in the queue and mark it running
  * @param string $clientID ID of the client requesting the job
  * @return SugarJob
  */
 public function nextJob($clientID)
 {
     $now = $this->db->now();
     $queued = SchedulersJob::JOB_STATUS_QUEUED;
     $try = $this->jobTries;
     while ($try--) {
         // TODO: tranaction start?
         $id = $this->db->getOne("SELECT id FROM {$this->job_queue_table} WHERE execute_time <= {$now} AND status = '{$queued}' ORDER BY date_entered ASC");
         if (empty($id)) {
             return null;
         }
         $job = new SchedulersJob();
         $job->retrieve($id);
         if (empty($job->id)) {
             return null;
         }
         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
         $job->client = $clientID;
         $client = $this->db->quote($clientID);
         // using direct query here to be able to fetch affected count
         // if count is 0 this means somebody changed the job status and we have to try again
         $res = $this->db->query("UPDATE {$this->job_queue_table} SET status='{$job->status}', date_modified={$now}, client='{$client}' WHERE id='{$job->id}' AND status='{$queued}'");
         if ($this->db->getAffectedRowCount($res) == 0) {
             // somebody stole our job, try again
             continue;
         } else {
             // to update dates & possible hooks
             $job->save();
             break;
         }
         // TODO: commit/check?
     }
     return $job;
 }
Beispiel #8
0
 public function testServerFailureWithError()
 {
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->scheduler_id = 'unittest';
     $job->execute_time = TimeDate::getInstance()->nowDb();
     $job->name = "Unit test Job";
     $job->target = "function::CronTest::cronJobFunction";
     $job->assigned_user_id = $GLOBALS['current_user']->id;
     $job->save();
     $jobid = $job->id;
     $this->client->return = 'This is not the server you are looking for';
     $this->jq->min_interval = 0;
     // disable throttle
     $this->jq->disable_schedulers = true;
     $this->jq->runCycle();
     $this->assertFalse($this->jq->runOk());
     $job = new SchedulersJob();
     $job->retrieve($jobid);
     $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
     $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
     $this->assertContains('This is not the server you are looking for', $job->message, "Wrong message");
 }
Beispiel #9
0
 /**
  * executes Scheduled job
  */
 function fire()
 {
     if (empty($this->job)) {
         // only execute when valid
         $GLOBALS['log']->fatal('Scheduler tried to fire an empty job!!');
         return false;
     }
     $exJob = explode('::', $this->job);
     if (is_array($exJob)) {
         // instantiate a new SchedulersJob object and prep it
         $trackerManager = TrackerManager::getInstance();
         $trackerManager->pause();
         $job = new SchedulersJob();
         $job->scheduler_id = $this->id;
         $job->scheduler =& $this;
         $job->execute_time = $job->handleDateFormat('now');
         $jobId = $job->save();
         $trackerManager->unPause();
         $job->retrieve($jobId);
         if ($exJob[0] == 'function') {
             $GLOBALS['log']->debug('----->Scheduler found a job of type FUNCTION');
             require_once 'modules/Schedulers/_AddJobsHere.php';
             $job->setJobFlag(1);
             $func = $exJob[1];
             $GLOBALS['log']->debug('----->SchedulersJob firing ' . $func);
             $res = call_user_func($func);
             if ($res) {
                 $job->setJobFlag(2);
                 $job->finishJob();
                 return true;
             } else {
                 $job->setJobFlag(3);
                 return false;
             }
         } elseif ($exJob[0] == 'url') {
             if (function_exists('curl_init')) {
                 $GLOBALS['log']->debug('----->SchedulersJob found a job of type URL');
                 $job->setJobFlag(1);
                 $GLOBALS['log']->debug('----->SchedulersJob firing URL job: ' . $exJob[1]);
                 if ($job->fireUrl($exJob[1])) {
                     $job->setJobFlag(2);
                     $job->finishJob();
                     return true;
                 } else {
                     $job->setJobFlag(3);
                     return false;
                 }
             } else {
                 $job->setJobFlag(4);
                 return false;
             }
         }
     }
     return false;
 }
Beispiel #10
0
 public function testGetNextJob()
 {
     // should get only jobs with status QUEUED, in date_entered order, and mark them as running
     // Clean up the queue
     $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='" . SchedulersJob::JOB_STATUS_QUEUED . "'");
     $job = $this->jq->nextJob("unit test");
     $this->assertNull($job, "Extra job found");
     // older job
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->scheduler_id = 'unittest';
     $job->date_entered = '2010-01-01 12:00:00';
     $job->name = "Old Job";
     $job->target = "test::test";
     $job->save();
     $jobid1 = $job->id;
     // another job, later date
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->scheduler_id = 'unittest';
     $job->date_entered = '2012-01-01 12:00:00';
     $job->name = "Newer Job";
     $job->target = "test::test";
     $job->save();
     $jobid2 = $job->id;
     // job with execute date in the future
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_QUEUED;
     $job->scheduler_id = 'unittest';
     $job->execute_time = $GLOBALS['timedate']->getNow()->modify("+3 days")->asDb();
     $job->date_entered = '2010-01-01 12:00:00';
     $job->name = "Future Job";
     $job->target = "test::test";
     $job->save();
     $jobid3 = $job->id;
     //running job
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_RUNNING;
     $job->scheduler_id = 'unittest';
     $job->date_entered = '2010-01-01 12:00:00';
     $job->name = "Running Job";
     $job->target = "test::test";
     $job->save();
     $jobid4 = $job->id;
     // done job
     $job = new SchedulersJob();
     $job->status = SchedulersJob::JOB_STATUS_DONE;
     $job->scheduler_id = 'unittest';
     $job->date_entered = '2010-01-01 12:00:00';
     $job->name = "Done Job";
     $job->target = "test::test";
     $job->save();
     $jobid5 = $job->id;
     // get the first one
     $job = $this->jq->nextJob("unit test");
     $this->assertEquals($jobid1, $job->id, "Wrong job fetched");
     $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
     $this->assertEquals("unit test", $job->client, "Wrong client");
     // check that DB record matches
     $job = new SchedulersJob();
     $job->retrieve($jobid1);
     $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
     $this->assertEquals("unit test", $job->client, "Wrong client");
     // get the second one
     $job = $this->jq->nextJob("unit test");
     $this->assertEquals($jobid2, $job->id, "Wrong job fetched");
     $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
     $this->assertEquals("unit test", $job->client, "Wrong client");
     // try to get the third one, should get null
     $job = $this->jq->nextJob("unit test");
     $this->assertNull($job, "Extra job found");
 }
Beispiel #11
0
 public function testJobsCleanupReschedule()
 {
     $this->scheduler->job_interval = "*::*::*::*::*";
     $this->scheduler->job = "test::test";
     $this->scheduler->status = "Active";
     $this->scheduler->new_with_id = true;
     $this->scheduler->save();
     $job = new SchedulersJob();
     $job->update_date_modified = false;
     $job->status = SchedulersJob::JOB_STATUS_RUNNING;
     $job->scheduler_id = $this->scheduler->id;
     $job->execute_time = $GLOBALS['timedate']->nowDb();
     $job->date_entered = '2010-01-01 12:00:00';
     $job->date_modified = '2010-01-01 12:00:00';
     $job->name = "Unit test Job 1";
     $job->target = "test::test";
     $job->assigned_user_id = $GLOBALS['current_user']->id;
     $job->save();
     $jobid = $job->id;
     // try queue run with old job stuck
     $queue = new MockSchedulerQueue();
     $this->scheduler->checkPendingJobs($queue);
     $ourjob = null;
     foreach ($queue->jobs as $job) {
         if ($job->scheduler_id == $this->scheduler->id) {
             $ourjob = $job;
             break;
         }
     }
     $this->assertEmpty($ourjob, "Duplicate job found");
     // now cleanup the job
     $queue->cleanup();
     $job = new SchedulersJob();
     $job->retrieve($jobid);
     $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
     $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
     // now try again - should schedule now
     $queue = new MockSchedulerQueue();
     $this->scheduler->checkPendingJobs($queue);
     $ourjob = null;
     foreach ($queue->jobs as $job) {
         if ($job->scheduler_id == $this->scheduler->id) {
             $ourjob = $job;
             break;
         }
     }
     $this->assertNotEmpty($ourjob, "Could not find our job in the queue");
 }
 public function testrunJobId()
 {
     //test with invalid job id
     $result = SchedulersJob::runJobId('1');
     $this->assertEquals('Job 1 not found.', $result);
     //test with valid job id
     $schedulersJob = new SchedulersJob();
     $schedulersJob->status = SchedulersJob::JOB_STATUS_DONE;
     $schedulersJob->save();
     $result = SchedulersJob::runJobId($schedulersJob->id);
     $this->assertEquals('Job ' . $schedulersJob->id . ' is not marked as running.', $result);
     //test with valid job id and status but mismatch client
     $schedulersJob->client = 'client';
     $schedulersJob->status = SchedulersJob::JOB_STATUS_RUNNING;
     $schedulersJob->save();
     $result = SchedulersJob::runJobId($schedulersJob->id, 'test_client');
     $this->assertEquals('Job ' . $schedulersJob->id . ' belongs to another client, can not run as test_client.', $result);
     $schedulersJob->mark_deleted($schedulersJob->id);
 }