|
2 | 2 |
|
3 | 3 | namespace Markup\JobQueueBundle\Command;
|
4 | 4 |
|
5 |
| -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 5 | +use Markup\JobQueueBundle\Service\JobManager; |
| 6 | +use Markup\JobQueueBundle\Service\ScheduledJobService; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use Symfony\Component\Console\Command\Command; |
6 | 9 | use Symfony\Component\Console\Input\InputInterface;
|
7 | 10 | use Symfony\Component\Console\Output\OutputInterface;
|
8 | 11 |
|
9 | 12 | /**
|
10 | 13 | * This command adds scheduled jobs to the job-queue
|
11 | 14 | *
|
12 | 15 | */
|
13 |
| -class AddScheduleJobToQueueCommand extends ContainerAwareCommand |
| 16 | +class AddScheduleJobToQueueCommand extends Command |
14 | 17 | {
|
| 18 | + protected static $defaultName = 'markup:scheduled_job:add'; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var JobManager |
| 22 | + */ |
| 23 | + private $jobManager; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var LoggerInterface|null |
| 27 | + */ |
| 28 | + private $logger; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ScheduledJobService |
| 32 | + */ |
| 33 | + private $scheduledJobService; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + JobManager $jobManager, |
| 37 | + ScheduledJobService $scheduledJobService, |
| 38 | + ?LoggerInterface $logger = null |
| 39 | + ) { |
| 40 | + $this->jobManager = $jobManager; |
| 41 | + $this->logger = $logger; |
| 42 | + $this->scheduledJobService = $scheduledJobService; |
| 43 | + |
| 44 | + parent::__construct(null); |
| 45 | + } |
| 46 | + |
15 | 47 | /**
|
16 | 48 | * @see Command
|
17 | 49 | */
|
18 | 50 | protected function configure()
|
19 | 51 | {
|
20 | 52 | $this
|
21 |
| - ->setName('markup:scheduled_job:add') |
22 | 53 | ->setDescription('Adds scheduled jobs to the job-queue');
|
23 | 54 | }
|
24 | 55 |
|
25 | 56 | protected function execute(InputInterface $input, OutputInterface $output)
|
26 | 57 | {
|
27 |
| - $scheduledJobService = $this->getContainer()->get('markup_job_queue.scheduled'); |
28 |
| - $logger = $this->getContainer()->get('logger'); |
29 |
| - |
30 |
| - if ($jobs = $scheduledJobService->getUnqueuedJobs()) { |
| 58 | + if ($jobs = $this->scheduledJobService->getUnqueuedJobs()) { |
31 | 59 | foreach ($jobs as $job) {
|
32 | 60 | try {
|
33 |
| - $this->getContainer()->get('jobby')->addCommandJob( |
| 61 | + $this->jobManager->addConsoleCommandJob( |
34 | 62 | $job->getJob(),
|
| 63 | + [], |
35 | 64 | $job->getTopic(),
|
36 | 65 | 3600,
|
37 | 66 | 3600
|
38 | 67 | );
|
39 | 68 | $job->setQueued(true);
|
40 |
| - $scheduledJobService->save($job, $flush = true); |
41 |
| - } catch(\Exception $e) { |
42 |
| - $logger->error(sprintf('There was an error adding the job "%s" to the job-queue, error: %s', $job->getJob(), $e->getMessage())); |
| 69 | + |
| 70 | + $this->scheduledJobService->save($job, $flush = true); |
| 71 | + } catch (\Exception $e) { |
| 72 | + $this->logger->error( |
| 73 | + sprintf( |
| 74 | + 'There was an error adding the job "%s" to the job-queue, error: %s', |
| 75 | + $job->getJob(), |
| 76 | + $e->getMessage() |
| 77 | + ) |
| 78 | + ); |
43 | 79 | }
|
44 | 80 | }
|
45 | 81 | }
|
|
0 commit comments