Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MonitoringTrait to ease use of steps and workload #162

Open
kjkooistra-youwe opened this issue Jan 16, 2023 · 0 comments
Open

Add MonitoringTrait to ease use of steps and workload #162

kjkooistra-youwe opened this issue Jan 16, 2023 · 0 comments

Comments

@kjkooistra-youwe
Copy link

kjkooistra-youwe commented Jan 16, 2023

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 added a commit to kjkooistra-youwe/ProcessManager that referenced this issue Feb 7, 2023
Introduce a MonitoringTrait, providing methods to
- Add the monitoring item (parent) id options
- Start and update steps
- Start, update and complete workloads
Add initProcessManagerByInputOption method to ExecutionTrait.
Update documentation example commands and clean them up a bit.
Relates to elements-at#162
@kjkooistra-youwe kjkooistra-youwe changed the title Expand ExecutionTrait to ease use of steps and workload Add MonitoringTrait to ease use of steps and workload Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant