Skip to content

Commit 47aa343

Browse files
Douglas Greenshieldsgsdevme
authored andcommitted
feat: change default idle timeout so follows same as timeout
1 parent b11139a commit 47aa343

File tree

4 files changed

+111
-45
lines changed

4 files changed

+111
-45
lines changed

Publisher/JobPublisher.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Markup\JobQueueBundle\Repository\JobLogRepository;
1111
use PhpAmqpLib\Exception\AMQPRuntimeException;
1212
use Psr\Log\LoggerInterface;
13+
use Psr\Log\NullLogger;
1314
use Ramsey\Uuid\Uuid;
1415
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -34,16 +35,12 @@ class JobPublisher implements ContainerAwareInterface
3435
*/
3536
private $logger;
3637

37-
/**
38-
* @param JobLogRepository $jobLogRepository
39-
* @param LoggerInterface $logger
40-
*/
4138
public function __construct(
4239
JobLogRepository $jobLogRepository,
43-
LoggerInterface $logger
40+
LoggerInterface $logger = null
4441
) {
4542
$this->jobLogRepository = $jobLogRepository;
46-
$this->logger = $logger;
43+
$this->logger = $logger ?? new NullLogger();
4744
}
4845

4946
/**

Service/JobManager.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,44 @@ public function addJob(Job $job, $dateTime = null)
4343
} else {
4444
$this->scheduledJob->addScheduledJob($job, $dateTime);
4545
}
46-
47-
return;
4846
}
4947

5048
/**
5149
* Adds a named command to the job queue
52-
* @param string $command A valid command for this application
53-
* @param string $topic The name of a valid topic
54-
* @param integer $timeout The amount of time to allow the command to run
55-
* @param integer $idleTimeout The amount of idle time to allow the command to run
50+
* @param string $command A valid command for this application.
51+
* @param string $topic The name of a valid topic.
52+
* @param integer $timeout The amount of time to allow the command to run.
53+
* @param integer $idleTimeout The amount of idle time to allow the command to run. Defaults to the same as timeout.
5654
*/
57-
public function addCommandJob($command, $topic = 'default', $timeout = 60, $idleTimeout = 60)
55+
public function addCommandJob($command, $topic = 'default', $timeout = 60, $idleTimeout = null)
5856
{
5957
$args = [];
6058
$args['command'] = $command;
6159
$args['timeout'] = $timeout;
62-
$args['idleTimeout'] = $idleTimeout;
60+
$args['idleTimeout'] = $idleTimeout ?? $timeout;
6361
$job = new ConsoleCommandJob($args, $topic);
6462
$this->addJob($job);
6563
}
6664

6765
/**
6866
* Adds a named command to the job queue at a specific datetime
69-
* @param string $command A valid command for this application
70-
* @param string $dateTime The DateTime to execute the command
71-
* @param string $topic The name of a valid topic
72-
* @param integer $timeout The amount of time to allow the command to run
73-
* @param integer $idleTimeout The amount of idle time to allow the command to run
67+
* @param string $command A valid command for this application.
68+
* @param string $dateTime The DateTime to execute the command.
69+
* @param string $topic The name of a valid topic.
70+
* @param integer $timeout The amount of time to allow the command to run.
71+
* @param integer $idleTimeout The amount of idle time to allow the command to run. Default to the same as timeout.
7472
*/
7573
public function addScheduledCommandJob(
7674
$command,
7775
\DateTime $dateTime,
7876
$topic = 'default',
7977
$timeout = 60,
80-
$idleTimeout = 60
78+
$idleTimeout = null
8179
) {
8280
$args = [];
8381
$args['command'] = $command;
8482
$args['timeout'] = $timeout;
85-
$args['idleTimeout'] = $idleTimeout;
83+
$args['idleTimeout'] = $idleTimeout ?? $timeout;
8684
$job = new ConsoleCommandJob($args, $topic);
8785
$this->addJob($job, $dateTime);
8886
}

Service/RecurringConsoleCommandReader.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public function __construct(
4040
$kernelPath
4141
) {
4242
$this->kernelPath = $kernelPath;
43-
$this->configurationFileName = null;
44-
$this->configurations = null;
4543
}
4644

4745
public function setConfigurationFileName($name)

Tests/Service/JobManagerTest.php

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Markup\Bundle\JobQueueBundle\Tests\Service;
44

5+
use Doctrine\Common\Persistence\ManagerRegistry;
56
use Markup\JobQueueBundle\Job\SleepJob;
7+
use Markup\JobQueueBundle\Model\Job;
68
use Markup\JobQueueBundle\Publisher\JobPublisher;
9+
use Markup\JobQueueBundle\Repository\JobLogRepository;
710
use Markup\JobQueueBundle\Service\JobManager;
811
use Markup\JobQueueBundle\Service\ScheduledJobService;
912
use Mockery as m;
@@ -12,41 +15,111 @@
1215

1316
class JobManagerTest extends MockeryTestCase
1417
{
18+
/**
19+
* @var JobPublisher
20+
*/
21+
private $jobPublisher;
22+
23+
/**
24+
* @var ScheduledJobService
25+
*/
26+
private $scheduledJobService;
27+
28+
/**
29+
* @var JobManager
30+
*/
31+
private $jobManager;
32+
1533
protected function setUp()
1634
{
17-
$jobPublisher = m::mock(JobPublisher::class);
18-
$scheduledJob = m::mock(ScheduledJobService::class);
19-
$jobPublisher->shouldReceive('publish')->andReturn(null);
20-
$scheduledJob->shouldReceive('addScheduledJob')->andReturn(null);
21-
$this->jobManager = new JobManager($jobPublisher, $scheduledJob);
35+
$this->jobPublisher = $this->createStoringJobPublisher();
36+
$this->scheduledJobService = $this->createStoringJobScheduler();
37+
$this->jobManager = new JobManager(
38+
$this->jobPublisher,
39+
$this->scheduledJobService
40+
);
2241
}
2342

2443
public function testCanAddJob()
2544
{
2645
$job = new SleepJob();
2746
$scheduledTime = new \DateTime();
28-
$this->assertNull($this->jobManager->addJob($job));
29-
$this->assertNull($this->jobManager->addJob($job, $scheduledTime));
47+
$this->jobManager->addJob($job);
48+
$this->jobManager->addJob($job, $scheduledTime);
49+
$this->assertSame([$job], $this->jobPublisher->getJobs());
50+
$this->assertSame([$job], $this->scheduledJobService->getJobs());
3051
}
3152

32-
public function testDoesNotAcceptBadJobs()
53+
public function testCanAddCommandJob()
3354
{
34-
$badjob = 'console:herp:derp';
35-
$exceptionThrown = false;
36-
try {
37-
$this->jobManager->addJob($badjob);
38-
} catch (Error $e) {
39-
$exceptionThrown = true;
40-
} catch (\TypeError $e) {
41-
$exceptionThrown = true;
42-
}
43-
if (!$exceptionThrown) {
44-
$this->fail();
45-
}
55+
$this->jobManager->addCommandJob('console:herp:derp', 'system', 60, 60);
56+
$this->assertCount(1, $this->jobPublisher->getJobs());
4657
}
4758

48-
public function testCanAddCommandJob()
59+
public function testIdleTimeoutDefaultsToTimeout()
60+
{
61+
$timeout = 720;
62+
$this->jobManager->addCommandJob('command', 'topic', $timeout);
63+
/** @var Job $job */
64+
$job = $this->jobPublisher->getJobs()[0];
65+
$this->assertEquals($timeout, $job->getArgs()['idleTimeout']);
66+
}
67+
68+
private function createStoringJobPublisher(): JobPublisher
69+
{
70+
return new class () extends JobPublisher {
71+
use JobStore;
72+
73+
public function __construct()
74+
{
75+
parent::__construct(m::mock(JobLogRepository::class));
76+
$this->initializeJobs();
77+
}
78+
79+
public function publish(Job $job)
80+
{
81+
$this->addJob($job);
82+
}
83+
};
84+
}
85+
86+
private function createStoringJobScheduler()
87+
{
88+
return new class () extends ScheduledJobService {
89+
use JobStore;
90+
91+
public function __construct()
92+
{
93+
parent::__construct(m::mock(ManagerRegistry::class));
94+
$this->initializeJobs();
95+
}
96+
97+
public function addScheduledJob(Job $job, $scheduledTime)
98+
{
99+
$this->addJob($job);
100+
}
101+
};
102+
}
103+
}
104+
105+
trait JobStore {
106+
/**
107+
* @var array<Job>
108+
*/
109+
private $jobs;
110+
111+
private function initializeJobs(): void
112+
{
113+
$this->jobs = [];
114+
}
115+
116+
private function addJob(Job $job): void
117+
{
118+
$this->jobs[] = $job;
119+
}
120+
121+
public function getJobs(): array
49122
{
50-
$this->assertNull($this->jobManager->addCommandJob('console:herp:derp', 'system', 60, 60));
123+
return $this->jobs;
51124
}
52125
}

0 commit comments

Comments
 (0)