|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Action; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class RunCronsCommand extends ContainerAwareCommand |
|
12
|
|
|
{ |
|
13
|
1 |
|
protected function configure() |
|
14
|
|
|
{ |
|
15
|
|
|
$this |
|
16
|
1 |
|
->setName('home:run-crons') |
|
17
|
1 |
|
->setDescription('Runs cronned actions for devices') |
|
18
|
1 |
|
->setHelp("This command takes all actions, that are marked as crons and runs it."); |
|
19
|
1 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
$doctrine = $this->getContainer()->get('doctrine'); |
|
25
|
|
|
|
|
26
|
|
|
$crons = $doctrine->getManager()->getRepository('AppBundle:Action')->findBy(['type'=>'cron']); |
|
27
|
|
|
|
|
28
|
|
|
/** @var Action $cronAction */ |
|
29
|
|
|
foreach ($crons as $cronAction) { |
|
30
|
|
|
$args = json_decode($cronAction->getArguments(), true); |
|
31
|
|
|
|
|
32
|
|
|
if (isset($args['disabled'])) { |
|
33
|
|
|
continue; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (time()-$args['last'] > $args['every']) { |
|
37
|
|
|
$output->writeln('Running '.$cronAction->getExecutor().' for "'. |
|
38
|
|
|
$cronAction->getDevice()->getName().'" device'); |
|
39
|
|
|
$this-> |
|
40
|
|
|
getContainer()-> |
|
41
|
|
|
get('actions')-> |
|
42
|
|
|
executeReal( |
|
43
|
|
|
$cronAction, |
|
44
|
|
|
'cron', |
|
45
|
|
|
['container'=>$this->getContainer()] |
|
46
|
|
|
); |
|
47
|
|
|
$args['last'] = time(); |
|
48
|
|
|
$cronAction->setArguments(json_encode($args)); |
|
49
|
|
|
$doctrine->getManager()->persist($cronAction); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$doctrine->getManager()->flush(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|