diff --git a/src/Manager/JobManager.php b/src/Manager/JobManager.php index 487c9c6..26160e4 100644 --- a/src/Manager/JobManager.php +++ b/src/Manager/JobManager.php @@ -64,14 +64,16 @@ private function escapeArgument(?string $argument): string return '"' . str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument) . '"'; } - public function dispatch(Job $job): void + public function dispatch(string|object $entityOrIdentity, object $payload): Job { - $pendingJobs = $this->jobRepository->getPendingByJob($job); + $pendingJobs = $this->jobRepository->getPending($entityOrIdentity, $payload); if (count($pendingJobs) > 0) { throw new LogicException("Job with identity already queued."); } + $job = $this->create($entityOrIdentity, $payload); + $command = [ ...$this->getPhpBinary(), Path::join($this->projectDirectory, 'bin', 'console'), @@ -95,6 +97,8 @@ public function dispatch(Job $job): void // $process->disableOutput(); $process->start(); + + return $job; } protected function getPhpBinary(): ?array diff --git a/tests/CancelTest.php b/tests/CancelTest.php index 9858eb1..213788f 100644 --- a/tests/CancelTest.php +++ b/tests/CancelTest.php @@ -29,10 +29,8 @@ public function testCancel(): void $entityManager->persist($entity); $entityManager->flush(); - $job = $jobManager->create($entity, new WaitJob(10)); - $now = new \DateTime(); - $jobManager->dispatch($job); + $job = $jobManager->dispatch($entity, new WaitJob(10)); $after = new \DateTime(); $diff = $after->getTimestamp() - $now->getTimestamp(); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index d336e2a..3cb5c0a 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -30,12 +30,9 @@ public function testWaitJobDispatchAndExecution(): void $entityManager->persist($entity); $entityManager->flush(); - $job = $jobManager->create($entity, new WaitJob()); - - self::assertTrue($job->isPending()); - $now = new \DateTime(); - $jobManager->dispatch($job); + $job = $jobManager->dispatch($entity, new WaitJob()); + self::assertTrue($job->isPending()); $after = new \DateTime(); $diff = $after->getTimestamp() - $now->getTimestamp(); @@ -100,10 +97,8 @@ public function testFastJobDispatchExecutionAndResult(): void $entityManager->persist($entity); $entityManager->flush(); - $job = $jobManager->create($entity, new FastJob("yeet")); - $now = new \DateTime(); - $jobManager->dispatch($job); + $job = $jobManager->dispatch($entity, new FastJob("yeet")); $after = new \DateTime(); $diff = $after->getTimestamp() - $now->getTimestamp();