Skip to content

Commit 19d5777

Browse files
committed
feat: option enable or disable jobs
1 parent a9a0977 commit 19d5777

File tree

9 files changed

+277
-5
lines changed

9 files changed

+277
-5
lines changed

Command/AddRecurringConsoleJobToQueueCommand.php

Lines changed: 10 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\Factory\JobStatusFactory;
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 JobStatusFactory */
52+
private $jobStatusFactory;
53+
5054
public function __construct(
5155
RecurringConsoleCommandReader $recurringConsoleCommandReader,
5256
JobManager $jobManager,
5357
CronHealthRepository $cronHealthRepository,
5458
JobLogRepository $jobLogRepository,
59+
JobStatusFactory $jobStatusFactory,
5560
string $environment
5661
) {
5762
$this->recurringConsoleCommandReader = $recurringConsoleCommandReader;
5863
$this->jobManager = $jobManager;
5964
$this->cronHealthRepository = $cronHealthRepository;
6065
$this->jobLogRepository = $jobLogRepository;
66+
$this->jobStatusFactory = $jobStatusFactory;
6167
$this->environment = $environment;
6268

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

136+
if ($configuration->getUserManaged() && !$this->jobStatusFactory->isUserEnabledJob($configuration->getCommand(), $configuration->getArguments())) {
137+
continue;
138+
}
139+
130140
$this->jobManager->addConsoleCommandJob(
131141
$command,
132142
$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+
}

Factory/JobStatusFactory.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Markup\JobQueueBundle\Factory;
5+
6+
use Markup\JobQueueBundle\Entity\JobStatus;
7+
use Markup\JobQueueBundle\Repository\JobStatusRepository;
8+
9+
class JobStatusFactory
10+
{
11+
/** @var JobStatusRepository */
12+
private $jobStatusRepository;
13+
14+
public function __construct(JobStatusRepository $jobStatusRepository)
15+
{
16+
$this->jobStatusRepository = $jobStatusRepository;
17+
}
18+
19+
public function isStatusEnabled(string $command, ?string $arguments): bool
20+
{
21+
$jobStatus = $this->jobStatusRepository->findBy([
22+
'command' => $command,
23+
'arguments' => $arguments ?? '[]'
24+
]);
25+
26+
if (!$jobStatus) {
27+
return true;
28+
}
29+
30+
return $jobStatus->getEnabled();
31+
}
32+
33+
public function isUserEnabledJob(string $command, array $arguments): bool
34+
{
35+
return $this->jobStatusRepository->isUserEnabled($command, $arguments);
36+
}
37+
38+
public function fetchOrCreateJobStatus(string $command, string $arguments): JobStatus
39+
{
40+
$jobStatus = $this->jobStatusRepository->findBy([
41+
'command' => $command,
42+
'arguments' => ($arguments) ?? null
43+
]);
44+
45+
return $jobStatus ?? new JobStatus(null, $command, $arguments, true);
46+
}
47+
48+
public function saveJobStatus(JobStatus $jobStatus): void
49+
{
50+
$this->jobStatusRepository->store($jobStatus);
51+
}
52+
}

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 = false,
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 getUserManaged(): ?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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 isUserEnabled(string $command, array $arguments): bool
24+
{
25+
/** @var JobStatus $jobStatus */
26+
$jobStatus = $this->getEntityRepository()->findOneBy([
27+
'command' => $command,
28+
'arguments' => ($arguments) ? json_encode($arguments) : null
29+
]);
30+
31+
if ($jobStatus instanceof JobStatus) {
32+
return $jobStatus->getEnabled();
33+
}
34+
35+
return true;
36+
}
37+
38+
public function findBy(array $arguments): ?JobStatus
39+
{
40+
$jobStatus = $this->getEntityRepository()->findOneBy($arguments);
41+
42+
if ($jobStatus instanceof JobStatus) {
43+
return $jobStatus;
44+
}
45+
46+
return null;
47+
}
48+
49+
public function store(JobStatus $jobStatus): void
50+
{
51+
$this->getEntityManager()->persist($jobStatus);
52+
$this->getEntityManager()->flush($jobStatus);
53+
}
54+
55+
private function getEntityRepository(): EntityRepository
56+
{
57+
$repository = $this->getEntityManager()->getRepository(JobStatus::class);
58+
59+
if ($repository instanceof EntityRepository) {
60+
return $repository;
61+
}
62+
63+
throw new \RuntimeException(sprintf('Doctrine returned an invalid repository for entity JobStatus'));
64+
}
65+
66+
private function getEntityManager(): EntityManager
67+
{
68+
$manager = $this->doctrine->getManager();
69+
70+
if ($manager instanceof EntityManagerInterface && !$manager->isOpen()) {
71+
$manager = $this->doctrine->resetManager();
72+
}
73+
74+
if ($manager instanceof EntityManager) {
75+
return $manager;
76+
}
77+
78+
throw new \RuntimeException(sprintf('Doctrine returned an invalid type for entity manager'));
79+
}
80+
}

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+
$jobStatusFactory: '@Markup\JobQueueBundle\Factory\JobStatusFactory'
1617
$environment: '%kernel.environment%'
1718
tags:
1819
- { name: console.command, command: 'markup:job_queue:recurring:add' }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
enabled:
14+
columns: [ enabled ]
15+
fields:
16+
command:
17+
type: string
18+
length: 60
19+
arguments:
20+
type: text
21+
enabled:
22+
type: boolean
23+
defaultValue: '1'

Resources/config/services.yml

Lines changed: 7 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\Factory\JobStatusFactory'
2627
markup_job_queue.writer.supervisord_config_file:
2728
class: Markup\JobQueueBundle\Service\SupervisordConfigFileWriter
2829
arguments:
@@ -54,6 +55,12 @@ services:
5455

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

58+
Markup\JobQueueBundle\Repository\JobStatusRepository:
59+
autowire: true
60+
61+
Markup\JobQueueBundle\Factory\JobStatusFactory:
62+
autowire: true
63+
5764
markup_job_queue.repository.job_log:
5865
class: Markup\JobQueueBundle\Repository\JobLogRepository
5966
arguments:

Service/RecurringConsoleCommandReader.php

Lines changed: 11 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\Factory\JobStatusFactory;
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 JobStatusFactory */
42+
private $jobStatusFactory;
43+
3944
public function __construct(
40-
$kernelPath
45+
string $kernelPath,
46+
JobStatusFactory $jobStatusFactory
4147
) {
4248
$this->kernelPath = $kernelPath;
49+
$this->jobStatusFactory = $jobStatusFactory;
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->jobStatusFactory->isStatusEnabled($group['command'], isset($group['arguments']) ? json_encode($group['arguments']) : null) : null
144153
);
145154

146155
$configurations->add($recurringConsoleCommandConfiguration);

0 commit comments

Comments
 (0)