Skip to content

semaz/CommandLockableTrait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CommandLockableTrait version

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages