/** * @param JobInterface $job * @return string jobId */ public function create(JobInterface $job) { $job->validate(); $jobData = ['index' => $this->index->getIndexNameCurrent(), 'type' => 'jobs', 'id' => $job->getId(), 'body' => $this->fillEmptyKeys($job->getData())]; $response = null; $i = 0; while ($i < 5) { try { $response = $this->client->index($jobData); break; } catch (ServerErrorResponseException $e) { // ES server error, try again $this->log('error', 'Elastic server error response', ['attemptNo' => $i, 'jobId' => $job->getId(), 'exception' => $e]); } sleep(1 + intval(pow(2, $i) / 2)); $i++; } if (!isset($response['created'])) { throw new ApplicationException("Unable to index job", null, ['job' => $jobData, 'elasticResponse' => $response]); } //@todo: remove sleep in next (major) release sleep(1); $i = 0; while ($i < 5) { $resJob = $this->get($job->getId()); if ($resJob != null) { return $response['_id']; } sleep(1 + intval(pow(2, $i) / 2)); $i++; } throw new ApplicationException("Unable to find job in index after creation", null, ['job' => $job->getData(), 'elasticResponse' => $response]); }
public function testCreateJob() { $job = self::$jobFactory->create(uniqid()); $id = self::$jobMapper->create($job); $res = self::$client->get(['index' => self::$index->getIndexNameCurrent(), 'type' => 'jobs', 'id' => $id]); $resJob = $res['_source']; $job = self::$jobMapper->get($id); $this->assertJob($job, $resJob); $this->assertEquals($job->getVersion(), $res['_version']); }