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'"); } }
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"); }
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"); }
public function testCleanupJobs() { $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser(); $this->scheduler = new Scheduler(false); $this->scheduler->id = create_guid(); $newjob = $this->createJob(array("name" => "Test Func", "status" => SchedulersJob::JOB_STATUS_DONE, "target" => "function::testJobFunction1", "assigned_user_id" => $GLOBALS['current_user']->id, "scheduler_id" => $this->scheduler->id)); $oldjob = $this->createJob(array("name" => "Test Func", "status" => SchedulersJob::JOB_STATUS_DONE, "target" => "function::testJobFunction1", "assigned_user_id" => $GLOBALS['current_user']->id, "scheduler_id" => $this->scheduler->id, 'update_date_modified' => false, 'date_modified' => TimeDate::getInstance()->getNow()->modify("-10 days")->asDB())); $oldestjob = $this->createJob(array("name" => "Test Func", "status" => SchedulersJob::JOB_STATUS_DONE, "target" => "function::testJobFunction1", "assigned_user_id" => $GLOBALS['current_user']->id, "scheduler_id" => $this->scheduler->id, 'update_date_modified' => false, 'date_modified' => TimeDate::getInstance()->getNow()->modify("-100 days")->asDb())); $this->assertNotEmpty($newjob->id); $this->assertNotEmpty($oldjob->id); $this->assertNotEmpty($oldestjob->id); $cleanjob = $this->createJob(array("name" => "Test Func", "status" => SchedulersJob::JOB_STATUS_RUNNING, "target" => "function::cleanJobQueue", "assigned_user_id" => $GLOBALS['current_user']->id, "scheduler_id" => $this->scheduler->id)); $cleanjob->runJob(); // new job should be still there $job = new SchedulersJob(); $job->retrieve($newjob->id); $this->assertEquals($newjob->id, $job->id); // old job should be deleted $job = new SchedulersJob(); $job->retrieve($oldjob->id); $this->assertEmpty($job->id); $job->retrieve($oldjob->id, true, false); $this->assertEquals($oldjob->id, $job->id); // oldest job should be purged $count = $this->db->getOne("SELECT count(*) from {$job->table_name} WHERE id='{$oldestjob->id}'"); $this->assertEquals(0, $count); }
/** * 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; }
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"); }
/** * 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; }
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"); }
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 testJobDelete() { $job = $this->createJob(array("name" => "TestCreate")); $job->status = SchedulersJob::JOB_STATUS_DONE; $job->save(); $this->assertNotEmpty($job->id); $id = $job->id; $job->retrieve($id); $this->assertNotEmpty($job->id); $job->mark_deleted($id); $job = new SchedulersJob(); $job->retrieve($job->id, true, false); $this->assertEmpty($job->id); }
public function testunexpectedExit() { $schedulersJob = new SchedulersJob(); //create conditions to mark job_done as false $schedulersJob->client = 'test'; $schedulersJob->status = SchedulersJob::JOB_STATUS_RUNNING; $schedulersJob->save(); $result = SchedulersJob::runJobId($schedulersJob->id, 'test'); //execute the method $schedulersJob->unexpectedExit(); $schedulersJob->retrieve($schedulersJob->id); $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $schedulersJob->status); $this->assertEquals(SchedulersJob::JOB_FAILURE, $schedulersJob->resolution); $schedulersJob->mark_deleted($schedulersJob->id); }