1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CronBundle\Command; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CronBundle\Annotation\CronJob as CronJobAnno; |
6
|
|
|
use Alpixel\Bundle\CronBundle\Entity\CronJob; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class CronScanCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this->setName('cron:scan') |
18
|
|
|
->setDescription('Scans for any new or deleted cron jobs') |
19
|
|
|
->addOption('keep-deleted', 'k', InputOption::VALUE_NONE, 'If set, deleted cron jobs will not be removed') |
20
|
|
|
->addOption('default-disabled', 'd', InputOption::VALUE_NONE, 'If set, new jobs will be disabled by default'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
$keepDeleted = $input->getOption('keep-deleted'); |
26
|
|
|
$defaultDisabled = $input->getOption('default-disabled'); |
27
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
28
|
|
|
|
29
|
|
|
// Enumerate the known jobs |
30
|
|
|
$jobRepo = $em->getRepository('CronBundle:CronJob'); |
31
|
|
|
$knownJobs = $jobRepo->getKnownJobs(); |
32
|
|
|
$knownJobs = array_fill_keys($knownJobs, true); |
33
|
|
|
|
34
|
|
|
// Enumerate all the jobs currently loaded |
35
|
|
|
$reader = $this->getContainer()->get('annotation_reader'); |
36
|
|
|
|
37
|
|
|
foreach ($this->getApplication()->all() as $command) { |
38
|
|
|
// Check for an @CronJob annotation |
39
|
|
|
$reflClass = new \ReflectionClass($command); |
40
|
|
|
foreach ($reader->getClassAnnotations($reflClass) as $anno) { |
41
|
|
|
if ($anno instanceof CronJobAnno) { |
42
|
|
|
$job = $command->getName(); |
43
|
|
|
if (array_key_exists($job, $knownJobs)) { |
44
|
|
|
// Clear it from the known jobs so that we don't try to delete it |
45
|
|
|
unset($knownJobs[$job]); |
46
|
|
|
|
47
|
|
|
// Update the job if necessary |
48
|
|
|
$currentJob = $jobRepo->findOneByCommand($job); |
49
|
|
|
$currentJob->setDescription($command->getDescription()); |
50
|
|
|
if ($currentJob->getInterval() != $anno->value) { |
51
|
|
|
$newTime = new \DateTime(); |
52
|
|
|
$newTime = $newTime->add(new \DateInterval($anno->value)); |
53
|
|
|
|
54
|
|
|
$currentJob->setInterval($anno->value); |
55
|
|
|
$currentJob->setNextRun($newTime); |
56
|
|
|
$output->writeln("Updated interval for $job to {$anno->value}"); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->newJobFound($output, $command, $anno, $defaultDisabled); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Clear any jobs that weren't found |
66
|
|
|
if (!$keepDeleted) { |
67
|
|
|
foreach (array_keys($knownJobs) as $deletedJob) { |
68
|
|
|
$output->writeln("Deleting job: $deletedJob"); |
69
|
|
|
$jobToDelete = $jobRepo->findOneByCommand($deletedJob); |
70
|
|
|
$em->remove($jobToDelete); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$em->flush(); |
75
|
|
|
$output->writeln('Finished scanning for cron jobs'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function newJobFound(OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false) |
79
|
|
|
{ |
80
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
81
|
|
|
$newJob = new CronJob(); |
82
|
|
|
$newJob->setCommand($command->getName()); |
83
|
|
|
$newJob->setDescription($command->getDescription()); |
84
|
|
|
$newJob->setInterval($anno->value); |
85
|
|
|
|
86
|
|
|
if ($anno->startTime === null) { |
87
|
|
|
$newJob->setNextRun(new \DateTime()); |
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
$newJob->setNextRun(new \DateTime($anno->startTime)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$newJob->setEnabled(!$defaultDisabled); |
93
|
|
|
|
94
|
|
|
$output->writeln('Added the job '.$newJob->getCommand().' with interval '.$newJob->getInterval()); |
95
|
|
|
$em->persist($newJob); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: