Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Expose more about job using metadata #158

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ return array(
)
);
```

### Getting slm/queue-doctrine specific data

The `DoctrineQueue` will populate all jobs with some additional metadata about the job. This metadata is stored under
a specific key name.

```php
$job->getMetadata(DoctrineQueue::METADATA_SCHEDULED_KEY); // Returns `\DateTimeImmutable` date for which job is scheduled.
$job->getMetadata(DoctrineQueue::METADATA_PRIORITY_KEY); // Returns integer priority of the job.
$job->getMetadata(DoctrineQueue::METADATA_ID_KEY); // Returns integer ID of the job in the database.
```

Provided Worker Strategies
--------------------------
Expand Down
27 changes: 23 additions & 4 deletions src/Queue/DoctrineQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class DoctrineQueue extends AbstractQueue implements DoctrineQueueInterface

public const DEFAULT_PRIORITY = 1024;

// A metadata field is populated with a date (`\DateTimeImmutable`) under this key.
public const METADATA_SCHEDULED_KEY = '__scheduled__';

// A metadata field is populated with the priority (`int`) under this key.
public const METADATA_PRIORITY_KEY = '__priority__';

// A metadata field is populated with the ID of this job in the database (`int`) under this key.
public const METADATA_ID_KEY = '__id__';

/**
* Options for this queue
*
Expand Down Expand Up @@ -193,8 +202,7 @@ public function pop(array $options = []): ?JobInterface
return null;
}

// Add job ID to meta data
return $this->unserializeJob($row['data'], ['__id__' => $row['id']]);
return $this->unserializeJobFromRow($row);
}

/**
Expand Down Expand Up @@ -313,8 +321,7 @@ public function peek(int $id): JobInterface
throw new JobNotFoundException(sprintf("Job with id '%s' does not exists.", $id));
}

// Add job ID to meta data
return $this->unserializeJob($row['data'], ['__id__' => $row['id']]);
return $this->unserializeJobFromRow($row);
}

/**
Expand Down Expand Up @@ -456,4 +463,16 @@ protected function purge(): void
);
}
}

private function unserializeJobFromRow(array $row): JobInterface
{
return $this->unserializeJob(
$row['data'],
[
self::METADATA_ID_KEY => $row['id'],
self::METADATA_SCHEDULED_KEY => new \DateTimeImmutable($row['scheduled']),
self::METADATA_PRIORITY_KEY => $row['priority'],
]
);
}
}