devcreel/command-lockable-trait

Symfony console command lockable trait

v1.0.1 2017-10-15 13:31 UTC

This package is not auto-updated.

Last update: 2024-05-26 01:51:59 UTC


README

Symfony console command lockable trait (very simple emulation of multithreading)

Installation

Use

Add this to composer.json

{
    "require": {
        "devcreel/command-lockable-trait": "1.0.*-dev"
    }
}

Usage

<?php

namespace TestBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use DevCreel\Command\LockableTrait;

class TestCommand extends ContainerAwareCommand
{
    use LockableTrait;
    
    //count of threads
    private $threadsCount = 5;
    
    protected function configure()
    {
        $this->setName('test:run');
    }
    
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        //check for free thread
        if (!$this->lock()) {
            $output->writeln('[' . $this->getName() . '] is already running in another process.');
            return 0;
        }
        
        //your code...
        
        //release thread
        $this->release();
    }
    
}

License

CommandLockableTrait is licensed under the MIT License