-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathProcessManagerSampleCommandAdvanced.php
125 lines (104 loc) · 4.39 KB
/
ProcessManagerSampleCommandAdvanced.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Created by valantic CX Austria GmbH
*
*/
namespace App\Command;
use Elements\Bundle\ProcessManagerBundle\Executor\Action;
use Pimcore\Console\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ProcessManagerSampleCommandAdvanced extends AbstractCommand
{
use \Elements\Bundle\ProcessManagerBundle\ExecutionTrait;
protected function configure(): void
{
$this
->setName('process-manager:sample-command-advanced')
->setDescription('Just an example - using the ProcessManager with steps and actions')
->addOption(
'monitoring-item-id',
null,
InputOption::VALUE_REQUIRED,
'Contains the monitoring item if executed via the Pimcore backend'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initProcessManager($input->getOption('monitoring-item-id'), ['autoCreate' => true]);
$classList = new \Pimcore\Model\DataObject\ClassDefinition\Listing();
$classList->setLimit(1);
$classes = $classList->load();
$monitoringItem = $this->getMonitoringItem();
$totalSteps = count($classes) + 1;
$monitoringItem->setTotalSteps($totalSteps)->save();
$data = [];
foreach ($classes as $i => $class) {
/**
* @var $list \Pimcore\Model\DataObject\Listing
* @var $class \Pimcore\Model\DataObject\ClassDefinition
* @var $o \Pimcore\Model\DataObject\AbstractObject
*/
$monitoringItem->setCurrentStep($i + 1)->setMessage('Processing Object of class '.$class->getName())->save();
sleep(5);
$listName = '\Pimcore\Model\DataObject\\'.ucfirst($class->getName()).'\Listing';
$list = new $listName();
$total = $list->getTotalCount();
$perLoop = 50;
for ($k = 0, $kMax = ceil($total / $perLoop); $k < $kMax; $k++) {
$list->setLimit($perLoop);
$offset = $k * $perLoop;
$list->setOffset($offset);
$monitoringItem->setCurrentWorkload(($offset ?: 1))
->setTotalWorkload($total)
->setDefaultProcessMessage($class->getName())
->save();
sleep(2);
$monitoringItem->getLogger()->info($monitoringItem->getMessage());
$objects = $list->load();
foreach ($objects as $o) {
$data[] = [
'ObjectType' => $class->getName(),
'id' => $o->getId(),
'modificationDate' => $o->getModificationDate(),
];
$monitoringItem->getLogger()->info('Processing Object with id: '.$o->getId());
}
}
$monitoringItem->setWorkloadCompleted()->save();
\Pimcore::collectGarbage();
}
$monitoringItem->setCurrentStep($totalSteps)->setCurrentWorkload(0)->setTotalWorkload(0)->setMessage(
'creating csv file'
)->save();
$csvFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . '/process-manager-example.csv';
$file = fopen($csvFile, 'w');
if (!empty($data)) {
array_unshift($data, array_keys($data[0]));
foreach ($data as $row) {
fputcsv($file, $row);
}
}
fclose($file);
$monitoringItem->setCurrentWorkload(1)->setTotalWorkload(1)->setMessage('csv file created')->save();
//adding some actions programmatically
$downloadAction = new Action\Download();
$downloadAction
->setAccessKey('myIcon')
->setLabel('Download Icon')
->setFilePath('/public/bundles/elementsprocessmanager/img/sprite-open-item-action.png')
->setDeleteWithMonitoringItem(false);
$openItemAction = new Action\OpenItem();
$openItemAction
->setLabel('Open document')
->setItemId(1)
->setType('document');
$monitoringItem->setActions([
$downloadAction,
$openItemAction,
]);
$monitoringItem->setMessage('Job finished')->setCompleted();
return self::SUCCESS;
}
}