ProcessManager icon indicating copy to clipboard operation
ProcessManager copied to clipboard

Add MonitoringTrait to ease use of steps and workload

Open kjkooistra-youwe opened this issue 2 years ago • 0 comments

Proposal to add additional methods to a new MonitoringTrait to ease the use of steps and workload. Also add a method to add the monitoring-item-id option. Suggestions for improvement are welcome.

Example usage:


use Elements\Bundle\ProcessManagerBundle\ExecutionTrait;
use Elements\Bundle\ProcessManagerBundle\MonitoringTrait;
use Monolog\Logger;
use Pimcore\Console\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ItemCommand extends AbstractCommand
{
    use ExecutionTrait;
    use MonitoringTrait;
    
    protected function configure(): void
    {
        // Command configuration
        $this->addMonitoringItemIdOption();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->initProcessManagerByInputOption($input);

        try {
            $this->processSteps();
        } catch (\Exception $exception) {
            $this->monitorAndLog($exception->getMessage(), Logger::CRITICAL)->setCompleted();
            return 1;
        }
        return 0;
    }

    protected function processSteps(): void
    {
        // Step 1: retrieve data
        $this->startSteps('Retrieving items', 3);
        $items = ['some', 'data']

        // Step 2: do something with the data
        $this->updateStep('Processing items');
        $this->processItems($items);

        // Step 3: save items
        $this->updateStep('Saving items');
        $this->saveItems($items);

        $this->getMonitoringItemInstance()
            ->setMessage('Finished updating items')
            ->setCompleted();
    }

    protected function processItems(array $items): void
    {
        $this->startWorkload('Processing items', count($items));

        foreach ($items as $item) {
            $this->updateWorkload('Processing item ' . $item);
            // Process item
        }

        $this->completeWorkload();
    }

    protected function saveItems(array $items): void
    {
        $this->startWorkload('Saving items', count($items));

        foreach ($items as $item) {
            $this->updateWorkload('Saving item ' . $item);
            // Save item
        }

        $this->completeWorkload();
    }
}

kjkooistra-youwe avatar Jan 16 '23 11:01 kjkooistra-youwe