/** * 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 testGetAffectedRowCount() { if (!$this->_db->supports("affected_rows")) { $this->markTestSkipped('Skipping, backend doesn\'t support affected rows'); } $beanIds = $this->_createRecords(1); $result = $this->_db->query("DELETE From contacts where id = '{$beanIds[0]}'"); $this->assertEquals(1, $this->_db->getAffectedRowCount($result)); }