1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Command; |
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\EntityManager; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use AnimeDb\Bundle\AppBundle\Entity\Task; |
16
|
|
|
use AnimeDb\Bundle\AppBundle\Repository\Task as TaskRepository; |
17
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
18
|
|
|
|
19
|
|
|
class TaskSchedulerCommand extends ContainerAwareCommand |
20
|
|
|
{ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this->setName('animedb:task-scheduler') |
24
|
|
|
->setDescription('Task Scheduler'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param InputInterface $input |
29
|
|
|
* @param OutputInterface $output |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
34
|
|
|
{ |
35
|
|
|
// exit if disabled |
36
|
|
|
if (!$this->getContainer()->getParameter('task_scheduler.enabled')) { |
37
|
|
|
return true; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// path to php executable |
41
|
|
|
$finder = new PhpExecutableFinder(); |
42
|
|
|
$console = $finder->find().' '.$this->getContainer()->getParameter('kernel.root_dir').'/console'; |
43
|
|
|
|
44
|
|
|
/* @var $em EntityManager */ |
45
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
46
|
|
|
/* @var $repository TaskRepository */ |
47
|
|
|
$repository = $em->getRepository('AnimeDbAppBundle:Task'); |
48
|
|
|
|
49
|
|
|
$output->writeln('Task Scheduler'); |
50
|
|
|
|
51
|
|
|
while (true) { |
52
|
|
|
$task = $repository->getNextTask(); |
53
|
|
|
|
54
|
|
|
// task is exists |
55
|
|
|
if ($task instanceof Task) { |
56
|
|
|
$output->writeln(sprintf('Run <info>%s</info>', $task->getCommand())); |
57
|
|
|
|
58
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
59
|
|
|
pclose(popen('start /b '.$console.' '.$task->getCommand().' >nul 2>&1', 'r')); |
60
|
|
|
} else { |
61
|
|
|
exec($console.' '.$task->getCommand().' >/dev/null 2>&1 &'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// update information on starting |
65
|
|
|
$task->executed(); |
66
|
|
|
$em->persist($task); |
67
|
|
|
$em->flush($task); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// standby for the next task |
71
|
|
|
$time = $repository->getWaitingTime(); |
72
|
|
|
if ($time) { |
73
|
|
|
$output->writeln(sprintf('Wait <comment>%s</comment> s.', $time)); |
74
|
|
|
sleep($time); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
unset($task); |
78
|
|
|
gc_collect_cycles(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.