Skip to content

Commit 36ca482

Browse files
authored
Merge pull request #106 from usemarkup/feat/enable-recurring-jobs
feat: option to enable or disable jobs
2 parents a9a0977 + 41e91f6 commit 36ca482

File tree

8 files changed

+241
-5
lines changed

8 files changed

+241
-5
lines changed

Command/AddRecurringConsoleJobToQueueCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Markup\JobQueueBundle\Command;
44

5+
use Markup\JobQueueBundle\Repository\JobStatusRepository;
56
use Markup\JobQueueBundle\Model\RecurringConsoleCommandConfiguration;
67
use Markup\JobQueueBundle\Repository\CronHealthRepository;
78
use Markup\JobQueueBundle\Repository\JobLogRepository;
@@ -47,17 +48,22 @@ class AddRecurringConsoleJobToQueueCommand extends Command
4748
*/
4849
private $environment;
4950

51+
/** @var JobStatusRepository */
52+
private $jobStatusRepository;
53+
5054
public function __construct(
5155
RecurringConsoleCommandReader $recurringConsoleCommandReader,
5256
JobManager $jobManager,
5357
CronHealthRepository $cronHealthRepository,
5458
JobLogRepository $jobLogRepository,
59+
JobStatusRepository $jobStatusRepository,
5560
string $environment
5661
) {
5762
$this->recurringConsoleCommandReader = $recurringConsoleCommandReader;
5863
$this->jobManager = $jobManager;
5964
$this->cronHealthRepository = $cronHealthRepository;
6065
$this->jobLogRepository = $jobLogRepository;
66+
$this->jobStatusRepository = $jobStatusRepository;
6167
$this->environment = $environment;
6268

6369
parent::__construct(null);
@@ -127,6 +133,14 @@ private function addRecurringJobs(OutputInterface $output)
127133
}
128134
}
129135

136+
if ($configuration->isUserManaged()) {
137+
$arguments = $configuration->getArguments() ? json_encode($configuration->getArguments()) : null;
138+
139+
if (!$this->jobStatusRepository->isStatusEnabled($command, $arguments)) {
140+
continue;
141+
}
142+
}
143+
130144
$this->jobManager->addConsoleCommandJob(
131145
$command,
132146
$configuration->getArguments(),

Entity/JobStatus.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Markup\JobQueueBundle\Entity;
5+
6+
class JobStatus
7+
{
8+
/** @var ?int */
9+
private $id;
10+
11+
/** @var string */
12+
private $command;
13+
14+
/** @var string */
15+
private $arguments;
16+
17+
/** @var bool */
18+
private $enabled;
19+
20+
public function __construct(
21+
?int $id,
22+
string $command,
23+
string $arguments,
24+
bool $enabled
25+
) {
26+
$this->id = $id;
27+
$this->command = $command;
28+
$this->arguments = $arguments;
29+
$this->enabled = $enabled;
30+
}
31+
32+
public function getCommand(): string
33+
{
34+
return $this->command;
35+
}
36+
37+
public function getArguments(): string
38+
{
39+
return $this->arguments;
40+
}
41+
42+
public function getEnabled(): bool
43+
{
44+
return $this->enabled;
45+
}
46+
47+
public function setCommand(string $command): void
48+
{
49+
$this->command = $command;
50+
}
51+
52+
public function setArguments(string $arguments): void
53+
{
54+
$this->arguments = $arguments;
55+
}
56+
57+
public function setEnabled(bool $enabled): void
58+
{
59+
$this->enabled = $enabled;
60+
}
61+
}

Model/RecurringConsoleCommandConfiguration.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class RecurringConsoleCommandConfiguration
4545
*/
4646
private $arguments;
4747

48+
/** @var ?bool */
49+
private $userManaged;
50+
51+
/** @var ?bool */
52+
private $jobStatusEnabled;
53+
4854
/**
4955
* @param string $command
5056
* @param array $arguments
@@ -53,16 +59,29 @@ class RecurringConsoleCommandConfiguration
5359
* @param string|null $description
5460
* @param integer|null $timeout
5561
* @param array|null $envs
56-
*/
57-
public function __construct($command, array $arguments, $topic, $schedule, $description = null, $timeout = 60, $envs = null)
58-
{
62+
* @param ?bool $userManaged
63+
* @param ?bool $jobStatusEnabled
64+
*/
65+
public function __construct(
66+
$command,
67+
array $arguments,
68+
$topic,
69+
$schedule,
70+
$description = null,
71+
$timeout = 60,
72+
$envs = null,
73+
$userManaged = null,
74+
$jobStatusEnabled = null
75+
) {
5976
$this->command = $command;
6077
$this->arguments = $arguments;
6178
$this->schedule = $schedule;
6279
$this->topic = str_replace('-', '_', $topic);
6380
$this->timeout = $timeout;
6481
$this->description = $description;
6582
$this->envs = $envs;
83+
$this->userManaged = $userManaged;
84+
$this->jobStatusEnabled = $jobStatusEnabled;
6685
}
6786

6887
/**
@@ -91,6 +110,16 @@ public function getArguments(): array
91110
return $this->arguments;
92111
}
93112

113+
public function isUserManaged(): ?bool
114+
{
115+
return $this->userManaged;
116+
}
117+
118+
public function getJobStatusEnabled(): ?bool
119+
{
120+
return $this->jobStatusEnabled;
121+
}
122+
94123
/**
95124
* @return string
96125
*/

Repository/JobStatusRepository.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Markup\JobQueueBundle\Repository;
5+
6+
use Doctrine\Common\Persistence\ManagerRegistry;
7+
use Doctrine\ORM\EntityManager;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Doctrine\ORM\EntityRepository;
10+
use Doctrine\ORM\Query;
11+
use Markup\JobQueueBundle\Entity\JobStatus;
12+
13+
class JobStatusRepository
14+
{
15+
/** @var ManagerRegistry */
16+
private $doctrine;
17+
18+
public function __construct(ManagerRegistry $doctrine)
19+
{
20+
$this->doctrine = $doctrine;
21+
}
22+
23+
public function isStatusEnabled(string $command, ?string $arguments): bool
24+
{
25+
$jobStatus = $this->findBy([
26+
'command' => $command,
27+
'arguments' => $arguments ?? '[]'
28+
]);
29+
30+
if (!$jobStatus) {
31+
return true;
32+
}
33+
34+
return $jobStatus->getEnabled();
35+
}
36+
37+
public function findBy(array $arguments): ?JobStatus
38+
{
39+
$jobStatus = $this->getEntityRepository()->findOneBy($arguments);
40+
41+
if ($jobStatus instanceof JobStatus) {
42+
return $jobStatus;
43+
}
44+
45+
return null;
46+
}
47+
48+
public function fetchOrCreateJobStatus(string $command, string $arguments): JobStatus
49+
{
50+
$jobStatus = $this->findBy([
51+
'command' => $command,
52+
'arguments' => ($arguments) ?? null
53+
]);
54+
55+
return $jobStatus ?? new JobStatus(null, $command, $arguments, true);
56+
}
57+
58+
public function save(JobStatus $jobStatus): void
59+
{
60+
$this->getEntityManager()->persist($jobStatus);
61+
$this->getEntityManager()->flush($jobStatus);
62+
}
63+
64+
private function getEntityRepository(): EntityRepository
65+
{
66+
$repository = $this->getEntityManager()->getRepository(JobStatus::class);
67+
68+
if ($repository instanceof EntityRepository) {
69+
return $repository;
70+
}
71+
72+
throw new \RuntimeException(sprintf('Doctrine returned an invalid repository for entity JobStatus'));
73+
}
74+
75+
private function getEntityManager(): EntityManager
76+
{
77+
$manager = $this->doctrine->getManager();
78+
79+
if ($manager instanceof EntityManagerInterface && !$manager->isOpen()) {
80+
$manager = $this->doctrine->resetManager();
81+
}
82+
83+
if ($manager instanceof EntityManager) {
84+
return $manager;
85+
}
86+
87+
throw new \RuntimeException(sprintf('Doctrine returned an invalid type for entity manager'));
88+
}
89+
}

Resources/config/commands.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
$jobManager: '@jobby'
1414
$cronHealthRepository: '@markup_job_queue.repository.cron_health'
1515
$jobLogRepository: '@markup_job_queue.repository.job_log'
16+
$jobStatusRepository: '@Markup\JobQueueBundle\Repository\JobStatusRepository'
1617
$environment: '%kernel.environment%'
1718
tags:
1819
- { name: console.command, command: 'markup:job_queue:recurring:add' }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Markup\JobQueueBundle\Entity\JobStatus:
2+
type: entity
3+
table: job_status
4+
id:
5+
id:
6+
type: guid
7+
unique: true
8+
generator:
9+
strategy: AUTO
10+
indexes:
11+
id:
12+
columns: [ id ]
13+
fields:
14+
command:
15+
type: string
16+
length: 60
17+
arguments:
18+
type: text
19+
enabled:
20+
type: boolean
21+
defaultValue: '1'

Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ services:
2323
class: Markup\JobQueueBundle\Service\RecurringConsoleCommandReader
2424
arguments:
2525
- '%kernel.root_dir%'
26+
- '@Markup\JobQueueBundle\Repository\JobStatusRepository'
2627
markup_job_queue.writer.supervisord_config_file:
2728
class: Markup\JobQueueBundle\Service\SupervisordConfigFileWriter
2829
arguments:
@@ -54,6 +55,10 @@ services:
5455

5556
Markup\JobQueueBundle\Repository\JobLogRepository: '@markup_job_queue.repository.job_log'
5657

58+
Markup\JobQueueBundle\Repository\JobStatusRepository:
59+
autowire: true
60+
public: false
61+
5762
markup_job_queue.repository.job_log:
5863
class: Markup\JobQueueBundle\Repository\JobLogRepository
5964
arguments:

Service/RecurringConsoleCommandReader.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Markup\JobQueueBundle\Exception\InvalidConfigurationException;
77
use Markup\JobQueueBundle\Exception\MissingScheduleException;
88
use Markup\JobQueueBundle\Exception\MissingConfigurationException;
9+
use Markup\JobQueueBundle\Repository\JobStatusRepository;
910
use Markup\JobQueueBundle\Model\RecurringConsoleCommandConfiguration;
1011
use Symfony\Component\Finder\Finder;
1112
use Symfony\Component\Finder\SplFileInfo;
@@ -36,10 +37,16 @@ class RecurringConsoleCommandReader
3637
/**
3738
* @param string $kernelPath
3839
*/
40+
41+
/** @var JobStatusRepository */
42+
private $jobStatusRepository;
43+
3944
public function __construct(
40-
$kernelPath
45+
string $kernelPath,
46+
JobStatusRepository $jobStatusRepository
4147
) {
4248
$this->kernelPath = $kernelPath;
49+
$this->jobStatusRepository = $jobStatusRepository;
4350
}
4451

4552
public function setConfigurationFileName($name)
@@ -140,7 +147,9 @@ private function parseConfiguration(array $config)
140147
$group['schedule'],
141148
isset($group['description']) ? $group['description'] : null,
142149
isset($group['timeout']) ? $group['timeout'] : null,
143-
isset($group['envs']) ? $group['envs'] : null
150+
isset($group['envs']) ? $group['envs'] : null,
151+
isset($group['user_managed']) ? $group['user_managed'] : null,
152+
isset($group['user_managed']) ? $this->jobStatusEnabled($group) : null
144153
);
145154

146155
$configurations->add($recurringConsoleCommandConfiguration);
@@ -149,6 +158,13 @@ private function parseConfiguration(array $config)
149158
return $configurations;
150159
}
151160

161+
private function jobStatusEnabled(array $group): bool
162+
{
163+
$arguments = isset($group['arguments']) ? json_encode($group['arguments']) : null;
164+
165+
return $this->jobStatusRepository->isStatusEnabled($group['command'], $arguments);
166+
}
167+
152168
/**
153169
* Reads the configuration file using the yml component and returns an array
154170
* @return array

0 commit comments

Comments
 (0)