Skip to content

Commit edea8bb

Browse files
committed
feat(logging): optional log supression flag
1 parent e95f2c3 commit edea8bb

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

Publisher/JobPublisher.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function setContainer(ContainerInterface $container = null)
5959
* @throws MissingTopicException
6060
* @throws UndefinedProducerException
6161
*/
62-
public function publish(Job $job)
62+
public function publish(Job $job, $supressLogging = false)
6363
{
6464
$job->validate();
6565

@@ -82,14 +82,16 @@ public function publish(Job $job)
8282

8383
// log the job as existing
8484
if ($job instanceof ConsoleCommandJob) {
85-
$uuid = Uuid::uuid4()->toString();
86-
$log = new JobLog(trim(sprintf('%s %s', $job->getCommand(), implode(' ', $job->getArguments()))), $uuid, $job->getTopic());
85+
if (!$supressLogging) {
86+
$uuid = Uuid::uuid4()->toString();
87+
$log = new JobLog(trim(sprintf('%s %s', $job->getCommand(), implode(' ', $job->getArguments()))), $uuid, $job->getTopic());
8788

88-
$this->jobLogRepository->add($log);
89+
$this->jobLogRepository->add($log);
8990

90-
// adds the uuid to the published job
91-
// which allows the consumer to specify the Uuid when running the command
92-
$message['uuid'] = $uuid;
91+
// adds the uuid to the published job
92+
// which allows the consumer to specify the Uuid when running the command
93+
$message['uuid'] = $uuid;
94+
}
9395
}
9496

9597
$producer->publish(json_encode($message));

Service/JobManager.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,19 @@ class JobManager
2020
/**
2121
* @var ScheduledJobService
2222
*/
23-
private $scheduledJob;
23+
private $scheduledJobService;
2424

2525
public function __construct(
2626
JobPublisher $publisher,
2727
ScheduledJobService $scheduledJobService
2828
) {
2929
$this->publisher = $publisher;
30-
$this->scheduledJob = $scheduledJobService;
30+
$this->scheduledJobService = $scheduledJobService;
3131
}
3232

33-
/**
34-
* Adds a job to the resque queue
35-
* @param Job $job
36-
*/
37-
public function addJob(Job $job, $dateTime = null)
33+
public function addJob(Job $job, $supressLogging = false)
3834
{
39-
if ($dateTime === null) {
40-
$this->publisher->publish($job);
41-
} elseif ($job instanceof ConsoleCommandJob) {
42-
$this->scheduledJob->addScheduledJob($job, $dateTime);
43-
}
35+
$this->publisher->publish($job, $supressLogging);
4436
}
4537

4638
/**
@@ -51,8 +43,9 @@ public function addJob(Job $job, $dateTime = null)
5143
* @param string $topic The name of a valid topic.
5244
* @param integer $timeout The amount of time to allow the command to run.
5345
* @param integer $idleTimeout The amount of idle time to allow the command to run. Defaults to the same as timeout.
46+
* @param bool $supressLogging Stops the job from being logged by the database
5447
*/
55-
public function addConsoleCommandJob(string $command, array $arguments = [], $topic = 'default', $timeout = 60, $idleTimeout = null)
48+
public function addConsoleCommandJob(string $command, array $arguments = [], $topic = 'default', $timeout = 60, $idleTimeout = null, $supressLogging = false)
5649
{
5750
if (stripos($command, " ") !== false) {
5851
throw new \InvalidArgumentException('Console command is not expected to have spaces within the name');
@@ -64,7 +57,8 @@ public function addConsoleCommandJob(string $command, array $arguments = [], $to
6457
$args['timeout'] = $timeout;
6558
$args['idleTimeout'] = $idleTimeout ?? $timeout;
6659
$job = new ConsoleCommandJob($args, $topic);
67-
$this->addJob($job);
60+
61+
$this->addJob($job, $supressLogging);
6862
}
6963

7064
/**
@@ -95,6 +89,7 @@ public function addScheduledConsoleCommandJob(
9589
$args['timeout'] = $timeout;
9690
$args['idleTimeout'] = $idleTimeout ?? $timeout;
9791
$job = new ConsoleCommandJob($args, $topic);
98-
$this->addJob($job, $dateTime);
92+
93+
$this->scheduledJobService->addScheduledJob($job, $dateTime);
9994
}
10095
}

Tests/Service/JobManagerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public function testCanAddJobWithoutDateTime(): void
5151

5252
public function testCanAddConsoleCommandJobWithDateTime(): void
5353
{
54-
$job = new ConsoleCommandJob();
54+
$job = 'muh:console:jerb';
5555
$scheduledTime = new \DateTime();
56-
$this->jobManager->addJob($job, $scheduledTime);
57-
$this->assertSame([$job], $this->scheduledJobService->getJobs());
56+
$this->jobManager->addScheduledConsoleCommandJob($job, $scheduledTime);
57+
$this->assertCount(1, $this->scheduledJobService->getJobs());
5858
}
5959

6060
public function testCanAddCommandJob(): void
@@ -83,7 +83,7 @@ public function __construct()
8383
$this->initializeJobs();
8484
}
8585

86-
public function publish(Job $job)
86+
public function publish(Job $job, $supressLogging = false)
8787
{
8888
$this->addJob($job);
8989
}

0 commit comments

Comments
 (0)