diff --git a/CHANGELOG.md b/CHANGELOG.md index 45bb77e..4db9873 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [1.0.1] - 2024-10-13 [Release] + +### Added + +- Added `WorkerPoolInterface::setPoolContext()` + `WorkerInterface::getPoolContext()` methods. +Each `Worker` can receive a shared application context during startup, +which is set using the `setPoolContext()` method before calling the `WorkerPoolInterface::start()` method. + + ## [1.0.0] - 2024-07-13 [Release] ### Fixed diff --git a/src/Worker/Worker.php b/src/Worker/Worker.php index 7ecc410..ab2482a 100644 --- a/src/Worker/Worker.php +++ b/src/Worker/Worker.php @@ -55,7 +55,12 @@ class Worker implements WorkerInterface /** @var ConcurrentIterator */ protected readonly ConcurrentIterator $iterator; - protected readonly array $context; + /** + * Shared context between all workers. + * + * @var array $poolContext + */ + protected readonly array $poolContext; private LoggerInterface $logger; private WorkersStorageInterface $workersStorage; @@ -83,7 +88,7 @@ public function __construct( private readonly array $groupsScheme, string $workersStorageClass, ?LoggerInterface $logger = null, - array $context = [] + array $poolContext = [] ) { $this->queue = new Queue(); $this->iterator = $this->queue->iterate(); @@ -91,7 +96,7 @@ public function __construct( $this->workerFuture = new DeferredFuture; $this->eventEmitter = new WorkerEventEmitter; - $this->context = $context; + $this->poolContext = $poolContext; if(\class_exists($workersStorageClass) === false) { throw new \RuntimeException('Invalid storage class provided. Expected ' . WorkersStorageInterface::class . ' implementation'); @@ -179,9 +184,9 @@ public function getWorkerType(): WorkerTypeEnum } #[\Override] - public function getWorkerContext(): array + public function getPoolContext(): array { - return $this->context; + return $this->poolContext; } public function getWorkerEventEmitter(): WorkerEventEmitterInterface diff --git a/src/Worker/WorkerInterface.php b/src/Worker/WorkerInterface.php index 37d3afd..a6797e5 100644 --- a/src/Worker/WorkerInterface.php +++ b/src/Worker/WorkerInterface.php @@ -30,7 +30,13 @@ public function getWorkerId(): int; public function getWorkerGroup(): WorkerGroup; public function getWorkerGroupId(): int; public function getWorkerType(): WorkerTypeEnum; - public function getWorkerContext(): array; + + /** + * Returns the context of the worker pool. + * + * @return array + */ + public function getPoolContext(): array; public function getAbortCancellation(): Cancellation;