Issues (62)

Command/CleanupCommand.php (2 issues)

1
<?php
2
3
namespace Kaliop\eZWorkflowEngineBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Kaliop\eZMigrationBundle\API\Value\Migration;
9
10
/**
11
 * Command to clean up workflows.
12
 */
13
class CleanupCommand extends AbstractCommand
14
{
15
    protected function configure()
16
    {
17
        $this->setName('kaliop:workflows:cleanup')
18
            ->addOption('older-than', 'o', InputOption::VALUE_REQUIRED, "Only remove workflows which have finished since N minutes", 86400)
19
            ->addOption('failed', 'f',  InputOption::VALUE_NONE, "Remove failed instead of finished workflows")
20
            ->addOption('dry-run', 'd', InputOption::VALUE_NONE, "Only list workflows to remove, without actually doing it")
21
            ->setDescription('Removes old workflows from the list of executed ones')
22
        ;
23
    }
24
25
    public function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $minAge = $input->getOption('older-than');
28
29
        $maxAge = time() - ($minAge * 60);
30
        $offset = 0;
31
        $limit = 1000;
32
33
        $workflowService = $this->getWorkflowService();
34
        $toRemove = array();
35
        $total = 0;
36
        do {
37
            $status = Migration::STATUS_DONE;
38
            $label = 'executed';
39
            if ($input->getOption('failed')) {
40
                $status = $status = Migration::STATUS_FAILED;
0 ignored issues
show
The assignment to $status is dead and can be removed.
Loading history...
41
                $label = 'failed';
42
            }
43
44
            $workflows = $workflowService->getWorkflowsByStatus($status, $limit, $offset);
0 ignored issues
show
The method getWorkflowsByStatus() does not exist on Kaliop\eZWorkflowEngineB...e\WorkflowServiceFacade. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            /** @scrutinizer ignore-call */ 
45
            $workflows = $workflowService->getWorkflowsByStatus($status, $limit, $offset);
Loading history...
45
46
            if (!count($workflows)) {
47
                break;
48
            }
49
50
            foreach($workflows as $workflow) {
51
                if ($workflow->executionDate < $maxAge) {
52
                    $toRemove[] = $workflow;
53
                }
54
            }
55
56
            $total += count($workflows);
57
            $offset += $limit;
58
        } while(true);
59
60
        if ($input->getOption('dry-run')) {
61
            $action = "To remove: ";
62
63
        } else {
64
            $action = "Removed ";
65
            foreach ($toRemove as $workflow) {
66
                $workflowService->deleteMigration($workflow);
67
            }
68
        }
69
70
        $output->writeln($action . count($toRemove) . " workflows out of $total $label");
71
72
        return 0;
73
    }
74
}
75