Skip to content

Commit 41e91f6

Browse files
committed
fix: refactor job status
1 parent 19d5777 commit 41e91f6

File tree

8 files changed

+48
-84
lines changed

8 files changed

+48
-84
lines changed

Command/AddRecurringConsoleJobToQueueCommand.php

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

33
namespace Markup\JobQueueBundle\Command;
44

5-
use Markup\JobQueueBundle\Factory\JobStatusFactory;
5+
use Markup\JobQueueBundle\Repository\JobStatusRepository;
66
use Markup\JobQueueBundle\Model\RecurringConsoleCommandConfiguration;
77
use Markup\JobQueueBundle\Repository\CronHealthRepository;
88
use Markup\JobQueueBundle\Repository\JobLogRepository;
@@ -48,22 +48,22 @@ class AddRecurringConsoleJobToQueueCommand extends Command
4848
*/
4949
private $environment;
5050

51-
/** @var JobStatusFactory */
52-
private $jobStatusFactory;
51+
/** @var JobStatusRepository */
52+
private $jobStatusRepository;
5353

5454
public function __construct(
5555
RecurringConsoleCommandReader $recurringConsoleCommandReader,
5656
JobManager $jobManager,
5757
CronHealthRepository $cronHealthRepository,
5858
JobLogRepository $jobLogRepository,
59-
JobStatusFactory $jobStatusFactory,
59+
JobStatusRepository $jobStatusRepository,
6060
string $environment
6161
) {
6262
$this->recurringConsoleCommandReader = $recurringConsoleCommandReader;
6363
$this->jobManager = $jobManager;
6464
$this->cronHealthRepository = $cronHealthRepository;
6565
$this->jobLogRepository = $jobLogRepository;
66-
$this->jobStatusFactory = $jobStatusFactory;
66+
$this->jobStatusRepository = $jobStatusRepository;
6767
$this->environment = $environment;
6868

6969
parent::__construct(null);
@@ -133,8 +133,12 @@ private function addRecurringJobs(OutputInterface $output)
133133
}
134134
}
135135

136-
if ($configuration->getUserManaged() && !$this->jobStatusFactory->isUserEnabledJob($configuration->getCommand(), $configuration->getArguments())) {
137-
continue;
136+
if ($configuration->isUserManaged()) {
137+
$arguments = $configuration->getArguments() ? json_encode($configuration->getArguments()) : null;
138+
139+
if (!$this->jobStatusRepository->isStatusEnabled($command, $arguments)) {
140+
continue;
141+
}
138142
}
139143

140144
$this->jobManager->addConsoleCommandJob(

Factory/JobStatusFactory.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

Model/RecurringConsoleCommandConfiguration.php

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

48-
/** @var bool */
48+
/** @var ?bool */
4949
private $userManaged;
5050

5151
/** @var ?bool */
@@ -59,7 +59,7 @@ class RecurringConsoleCommandConfiguration
5959
* @param string|null $description
6060
* @param integer|null $timeout
6161
* @param array|null $envs
62-
* @param bool $userManaged
62+
* @param ?bool $userManaged
6363
* @param ?bool $jobStatusEnabled
6464
*/
6565
public function __construct(
@@ -70,7 +70,7 @@ public function __construct(
7070
$description = null,
7171
$timeout = 60,
7272
$envs = null,
73-
$userManaged = false,
73+
$userManaged = null,
7474
$jobStatusEnabled = null
7575
) {
7676
$this->command = $command;
@@ -110,7 +110,7 @@ public function getArguments(): array
110110
return $this->arguments;
111111
}
112112

113-
public function getUserManaged(): ?bool
113+
public function isUserManaged(): ?bool
114114
{
115115
return $this->userManaged;
116116
}

Repository/JobStatusRepository.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ public function __construct(ManagerRegistry $doctrine)
2020
$this->doctrine = $doctrine;
2121
}
2222

23-
public function isUserEnabled(string $command, array $arguments): bool
23+
public function isStatusEnabled(string $command, ?string $arguments): bool
2424
{
25-
/** @var JobStatus $jobStatus */
26-
$jobStatus = $this->getEntityRepository()->findOneBy([
25+
$jobStatus = $this->findBy([
2726
'command' => $command,
28-
'arguments' => ($arguments) ? json_encode($arguments) : null
27+
'arguments' => $arguments ?? '[]'
2928
]);
3029

31-
if ($jobStatus instanceof JobStatus) {
32-
return $jobStatus->getEnabled();
30+
if (!$jobStatus) {
31+
return true;
3332
}
3433

35-
return true;
34+
return $jobStatus->getEnabled();
3635
}
3736

3837
public function findBy(array $arguments): ?JobStatus
@@ -46,7 +45,17 @@ public function findBy(array $arguments): ?JobStatus
4645
return null;
4746
}
4847

49-
public function store(JobStatus $jobStatus): void
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
5059
{
5160
$this->getEntityManager()->persist($jobStatus);
5261
$this->getEntityManager()->flush($jobStatus);

Resources/config/commands.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +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'
16+
$jobStatusRepository: '@Markup\JobQueueBundle\Repository\JobStatusRepository'
1717
$environment: '%kernel.environment%'
1818
tags:
1919
- { name: console.command, command: 'markup:job_queue:recurring:add' }

Resources/config/doctrine/JobStatus.orm.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Markup\JobQueueBundle\Entity\JobStatus:
1010
indexes:
1111
id:
1212
columns: [ id ]
13-
enabled:
14-
columns: [ enabled ]
1513
fields:
1614
command:
1715
type: string

Resources/config/services.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
class: Markup\JobQueueBundle\Service\RecurringConsoleCommandReader
2424
arguments:
2525
- '%kernel.root_dir%'
26-
- '@Markup\JobQueueBundle\Factory\JobStatusFactory'
26+
- '@Markup\JobQueueBundle\Repository\JobStatusRepository'
2727
markup_job_queue.writer.supervisord_config_file:
2828
class: Markup\JobQueueBundle\Service\SupervisordConfigFileWriter
2929
arguments:
@@ -57,9 +57,7 @@ services:
5757

5858
Markup\JobQueueBundle\Repository\JobStatusRepository:
5959
autowire: true
60-
61-
Markup\JobQueueBundle\Factory\JobStatusFactory:
62-
autowire: true
60+
public: false
6361

6462
markup_job_queue.repository.job_log:
6563
class: Markup\JobQueueBundle\Repository\JobLogRepository

Service/RecurringConsoleCommandReader.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +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;
9+
use Markup\JobQueueBundle\Repository\JobStatusRepository;
1010
use Markup\JobQueueBundle\Model\RecurringConsoleCommandConfiguration;
1111
use Symfony\Component\Finder\Finder;
1212
use Symfony\Component\Finder\SplFileInfo;
@@ -38,15 +38,15 @@ class RecurringConsoleCommandReader
3838
* @param string $kernelPath
3939
*/
4040

41-
/** @var JobStatusFactory */
42-
private $jobStatusFactory;
41+
/** @var JobStatusRepository */
42+
private $jobStatusRepository;
4343

4444
public function __construct(
4545
string $kernelPath,
46-
JobStatusFactory $jobStatusFactory
46+
JobStatusRepository $jobStatusRepository
4747
) {
4848
$this->kernelPath = $kernelPath;
49-
$this->jobStatusFactory = $jobStatusFactory;
49+
$this->jobStatusRepository = $jobStatusRepository;
5050
}
5151

5252
public function setConfigurationFileName($name)
@@ -149,7 +149,7 @@ private function parseConfiguration(array $config)
149149
isset($group['timeout']) ? $group['timeout'] : null,
150150
isset($group['envs']) ? $group['envs'] : null,
151151
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
152+
isset($group['user_managed']) ? $this->jobStatusEnabled($group) : null
153153
);
154154

155155
$configurations->add($recurringConsoleCommandConfiguration);
@@ -158,6 +158,13 @@ private function parseConfiguration(array $config)
158158
return $configurations;
159159
}
160160

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+
161168
/**
162169
* Reads the configuration file using the yml component and returns an array
163170
* @return array

0 commit comments

Comments
 (0)