-
Notifications
You must be signed in to change notification settings - Fork 38
/
ExampleCommand.php
86 lines (72 loc) · 2.71 KB
/
ExampleCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Acme\DemoBundle\Command;
use Wrep\Daemonizable\Command\EndlessCommand;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
class ExampleCommand extends EndlessCommand
{
// This is just a normal Command::configure() method
protected function configure()
{
$this->setName('acme:examplecommand')
->setDescription('An EndlessContainerAwareCommand implementation example')
->setTimeout(1.5); // Set the timeout in seconds between two calls to the "execute" method
}
// This is a normal Command::initialize() method and it's called exactly once before the first execute call
protected function initialize(InputInterface $input, OutputInterface $output)
{
// Do one time initialization here
}
// Execute will be called in a endless loop
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Tell the user what we're going to do.
// This will be silenced by Symfony if the user doesn't want any output at all,
// so you don't have to write any checks, just always write to the output.
$output->write('Updating average score... ');
// After a long operation, but before doing irreversable things call throwExceptionOnShutdown
// this will throw an exception if the OS or something else wants us to shutdown. Finalize is
// still called and the command will exit normally.
$score = $this->calculateAvgScore();
$this->throwExceptionOnShutdown();
if ( false === file_put_contents('/tmp/acme-avg-score.txt', $score) )
{
// Set the return code to non-zero if there are any errors
$this->setReturnCode(1);
// After this execute method returns we want the command exit
$this->shutdown();
// Tell the user we're done
$output->writeln('failed!');
}
else
{
// Tell the user we're done
$output->writeln('done');
}
return self::SUCCESS;
}
/**
* Called after each iteration
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function finishIteration(InputInterface $input, OutputInterface $output): void
{
// Do some cleanup/memory management here, don't forget to call the parent implementation!
parent::finishIteration($input, $output);
}
// Called once on shutdown after the last iteration finished
protected function finalize(InputInterface $input, OutputInterface $output): void
{
// Do some cleanup here, don't forget to call the parent implementation!
parent::finalize($input, $output);
// Keep it short! We may need to exit because the OS wants to shutdown
// and we can get killed if it takes to long!
}
// Long operation to calculate the avarage score
private function calculateAvgScore()
{
sleep(5);
return rand(1, 10);
}
}