Skip to content

Add MonitoringTrait to ease use of steps and workload #162

Description

@kjkooistra-youwe

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();
    }
}

Activity

changed the title [-]Expand ExecutionTrait to ease use of steps and workload[/-] [+]Add MonitoringTrait to ease use of steps and workload[/+] on Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions