16 code examples of PHP Oro\Component\MessageQueue\Job\Job extracted from open source projects
private function updateRootJob(Job $rootJob): void
{
$rootJob->setLastActiveAt(new \DateTime());
$statusAndProgressCalculator = $this->statusCalculatorResolver->getCalculatorForRootJob($rootJob);
$rootJobStatus = $statusAndProgressCalculator->calculateRootJobStatus();
$rootJob->setStatus($rootJobStatus);
if ($this->jobStatusChecker->isJobStopped($rootJob)) {
$rootJob->setStoppedAt(new \DateTime());
}
$progress = $statusAndProgressCalculator->calculateRootJobProgress();
if ($rootJob->getJobProgress() !== $progress) {
$rootJob->setJobProgress($progress);
}
$statusAndProgressCalculator->clean();
if ($this->jobStatusChecker->isJobStopped($rootJob)) {
$message = new Message(['jobId' => $rootJob->getId()], MessagePriority::HIGH);
$this->messageProducer->send(RootJobStoppedTopic::getName(), $message);
}
}
private function updateRootJob(Job $rootJob): void
{
$rootJob->setLastActiveAt(new \DateTime());
$statusAndProgressCalculator = $this->statusCalculatorResolver->getCalculatorForRootJob($rootJob);
$rootJobStatus = $statusAndProgressCalculator->calculateRootJobStatus();
$rootJob->setStatus($rootJobStatus);
if ($this->jobStatusChecker->isJobStopped($rootJob)) {
$rootJob->setStoppedAt(new \DateTime());
}
$progress = $statusAndProgressCalculator->calculateRootJobProgress();
if ($rootJob->getJobProgress() !== $progress) {
$rootJob->setJobProgress($progress);
}
$statusAndProgressCalculator->clean();
if ($this->jobStatusChecker->isJobStopped($rootJob)) {
$message = new Message(['jobId' => $rootJob->getId()], MessagePriority::HIGH);
$this->messageProducer->send(RootJobStoppedTopic::getName(), $message);
}
}
private function hasNotStartedChild(Job $job): bool
{
foreach ($job->getChildJobs() as $childJob) {
if (Job::STATUS_NEW === $childJob->getStatus()) {
return true;
}
}
return false;
}
public function failAndRedeliveryChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t fail root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_FAILED_REDELIVERED);
$this->jobManager->saveJob($job);
}
public function failChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t fail root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_FAILED);
$job->setStoppedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function failChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t fail root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_FAILED);
$job->setStoppedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function successChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t success root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_SUCCESS);
$job->setJobProgress(1);
$job->setStoppedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function successChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t success root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_SUCCESS);
$job->setJobProgress(1);
$job->setStoppedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function successChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t success root jobs. id: "%s"', $job->getId()));
}
$job->setStatus(Job::STATUS_SUCCESS);
$job->setJobProgress(1);
$job->setStoppedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function startChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t start root jobs. id: "%s"', $job->getId()));
}
if (!in_array($job->getStatus(), $this->getNotStartedJobStatuses(), true)) {
throw new JobCannotBeStartedException($job);
}
$job->setStatus(Job::STATUS_RUNNING);
$job->setStartedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
public function startChildJob(Job $job): void
{
if ($job->isRoot()) {
throw new \LogicException(sprintf('Can\'t start root jobs. id: "%s"', $job->getId()));
}
if (!in_array($job->getStatus(), $this->getNotStartedJobStatuses(), true)) {
throw new JobCannotBeStartedException($job);
}
$job->setStatus(Job::STATUS_RUNNING);
$job->setStartedAt(new \DateTime());
$this->jobManager->saveJob($job);
}
/**
* @return Job
*/
public function findOrCreateChildJob(string $jobName, Job $rootJob): ?Job
{
if (!$jobName) {
throw new \LogicException('Job name must not be empty');
}
$job = $this->getJobRepository()->findChildJobByName($jobName, $rootJob);
if ($job) {
return $job;
}
$job = $this->getJobRepository()->createJob();
$job->setStatus(Job::STATUS_NEW);
$job->setName($jobName);
$job->setCreatedAt(new \DateTime());
$job->setRootJob($rootJob);
$rootJob->addChildJob($job);
$job->setJobProgress(0);
$this->jobManager->saveJob($job);
return $job;
}
private function insertJob(Job $job, EntityManager $em): void
{
$tableName = $em->getClassMetadata(JobEntity::class)->getTableName();
$connection = $em->getConnection();
$qb = $connection->createQueryBuilder();
$qb
->insert($tableName)
->values([
'owner_id' => ':ownerId',
'name' => ':name',
'status' => ':status',
'interrupted' => ':interrupted',
'created_at' => ':createdAt',
'started_at' => ':startedAt',
'stopped_at' => ':stoppedAt',
'last_active_at' => ':lastActiveAt',
'root_job_id' => ':rootJob',
'data' => ':data',
'job_progress' => ':jobProgress',
]);
if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
$qb->setValue('`unique`', ':unique');
} else {
$qb->setValue('"unique"', ':unique');
}
$qb
->setParameters([
'ownerId' => $job->getOwnerId(),
'name' => $job->getName(),
'status' => $job->getStatus(),
'unique' => (bool) $job->isUnique(),
'interrupted' => (bool) $job->isInterrupted(),
'createdAt' => $job->getCreatedAt(),
'startedAt' => $job->getStartedAt(),
'stoppedAt' => $job->getStoppedAt(),
'lastActiveAt' => $job->getLastActiveAt(),
'rootJob' => $job->getRootJob() ? $job->getRootJob()->getId() : null,
'data' => $job->getData(),
'jobProgress' => $job->getJobProgress(),
], [
'ownerId' => Type::STRING,
'name' => Type::STRING,
'status' => Type::STRING,
'unique' => Type::BOOLEAN,
'interrupted' => Type::BOOLEAN,
'createdAt' => Type::DATETIME,
'startedAt' => Type::DATETIME,
'stoppedAt' => Type::DATETIME,
'lastActiveAt' => Type::DATETIME,
'rootJob' => Type::INTEGER,
'data' => Type::JSON_ARRAY,
'jobProgress' => Type::FLOAT,
]);
$qb->execute();
$job->setId($connection->lastInsertId());
}
protected function saveJobResult(Job $job, array $data)
{
if (!empty($data['errors'])) {
$errorLogFile = $this->saveToStorageErrorLog($data['errors']);
if ($errorLogFile) {
$data['errorLogFile'] = $errorLogFile;
}
}
$job->setData($data);
}
/**
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function processJob(
JobRunner $jobRunner,
Job $job,
array $body,
float $startTimestamp,
bool &$deleteChunkFile
): bool {
$deleteChunkFile = true;
$chunkFile = new ChunkFile(
$body['fileName'],
$body['fileIndex'],
$body['firstRecordOffset'],
$body['sectionName']
);
$requestType = new RequestType($body['requestType']);
$requestType->add(ApiAction::UPDATE_LIST);
$request = new BatchUpdateRequest(
$body['version'],
$requestType,
$body['operationId'],
[$body['entityClass']],
$chunkFile,
$this->fileManager
);
$response = $this->handler->handle($request);
$jobData = $job->getData();
if ($body['extra_chunk'] ?? false) {
$jobData['extra_chunk'] = true;
}
$previousAggregateTime = $jobData['summary']['aggregateTime'] ?? 0;
$jobData['summary'] = [
'aggregateTime' => $this->processingHelper->calculateAggregateTime(
$startTimestamp,
$previousAggregateTime
),
'readCount' => $response->getSummary()->getReadCount(),
'writeCount' => $response->getSummary()->getWriteCount(),
'errorCount' => $response->getSummary()->getErrorCount(),
'createCount' => $response->getSummary()->getCreateCount(),
'updateCount' => $response->getSummary()->getUpdateCount(),
];
$job->setData($jobData);
if ($response->isRetryAgain()) {
$this->logger->info(sprintf('The retry requested. Reason: %s', $response->getRetryReason()));
$retryResult = $this->retryChunk($jobRunner, $job, $body, $chunkFile);
if ($retryResult) {
$deleteChunkFile = false;
}
return $retryResult;
}
if ($response->hasUnexpectedErrors()) {
return false;
}
$rawItems = $response->getData();
if (!$rawItems) {
// failed to load data
return false;
}
$processedItemStatuses = $response->getProcessedItemStatuses();
if (!$processedItemStatuses) {
// some unexpected errors occurred before processing of loaded data
return false;
}
$result = true;
if ($this->retryHelper->hasItemsToRetry($rawItems, $processedItemStatuses)) {
$chunksToRetry = $this->retryHelper->getChunksToRetry($rawItems, $processedItemStatuses);
if ($chunksToRetry && !$this->processChunksToRetry($jobRunner, $job, $body, $chunkFile, $chunksToRetry)) {
$result = false;
}
}
$jobData['summary']['aggregateTime'] = $this->processingHelper->calculateAggregateTime(
$startTimestamp,
$previousAggregateTime
);
$job->setData($jobData);
return $result;
}
private function saveOperationIdToJob(int $operationId, Job $rootJob): void
{
$data = $rootJob->getData();
$data['api_operation_id'] = $operationId;
$rootJob->setData($data);
$this->jobManager->saveJob($rootJob);
}