Skip to content

[symfony] Async commands#403

Merged
makasim merged 13 commits into
masterfrom
pr-250
Aug 14, 2018
Merged

[symfony] Async commands#403
makasim merged 13 commits into
masterfrom
pr-250

Conversation

@makasim

@makasim makasim commented Mar 10, 2018

Copy link
Copy Markdown
Member

Async commands

The enqueue/async-command package adds ability to run Symfony command async with ability to get output.

Installation

$ composer require enqueue/async-command:^0.9.x-dev

Configuration

# config/packages/enqueue_async_commands.yaml

enqueue:
   async_commands: true

Usage

<?php

use Enqueue\Client\ProducerInterface;
use Enqueue\AsyncCommand\Commands;
use Enqueue\AsyncCommand\RunCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;

/** @var $container ContainerInterface */

/** @var ProducerInterface $producer */
$producer = $container->get(ProducerInterface::class);

$producer->sendCommand(Commands::RUN_COMMAND, new RunCommand('debug:container'));

optionally you can get a command execution result:

<?php

use Enqueue\Client\ProducerInterface;
use Enqueue\AsyncCommand\RunCommandResult;
use Enqueue\AsyncCommand\Commands;
use Enqueue\AsyncCommand\RunCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;

/** @var $container ContainerInterface */

/** @var ProducerInterface $producer */
$producer = $container->get(ProducerInterface::class);

$promise = $producer->sendCommand(Commands::RUN_COMMAND, new RunCommand('debug:container'), true);

// do other stuff.

if ($replyMessage = $promise->receive(5000)) { 
    $result = RunCommandResult::jsonUnserialize($replyMessage->getBody());
    
    echo $result->getOutput();
}

back to index

@wesleylancel

Copy link
Copy Markdown

@makasim Hi, any word on this being merged? We would love to use this :)

@makasim

makasim commented Mar 26, 2018

Copy link
Copy Markdown
Member Author

@wesleylancel It needs some testing, not sure I can find time to finish this in near future.

@temp

temp commented Apr 16, 2018

Copy link
Copy Markdown

Has enqueue/async-command been removed? The link in the first post produces a 404...
Would love to test this!

@stephanvierkant

stephanvierkant commented Apr 17, 2018

Copy link
Copy Markdown

Same here! See schmittjoh/JMSJobQueueBundle#208 btw, where some users are not satisfied with JMSJobQueueBundle right now, and maybe switch to enqueue-dev when a async function is available.

@makasim

makasim commented Apr 17, 2018

Copy link
Copy Markdown
Member Author

I've created a repo enqueue/async-command and published it to packagist https://packagist.org/packages/enqueue/async-command

you can require it in your composer json like this

{
  "require": {
    "enqueue/async-command": "dev-pr-250"
  }
}

That way you can test it.

@temp

temp commented Apr 17, 2018

Copy link
Copy Markdown

Thanks! Is there something like an example for a minimal setup/configuration for this? I'm new to enqueue, and just want to check wether or not to replace JmsJobQueue by this.

@makasim

makasim commented Apr 17, 2018

Copy link
Copy Markdown
Member Author

@makasim

makasim commented Apr 17, 2018

Copy link
Copy Markdown
Member Author

and in PR's description.

@temp

temp commented Apr 17, 2018

Copy link
Copy Markdown

@makasim Hmm there is no *Bundle class, am I missing something?

}

if ($config['async_commands']['enabled']) {
if (false == class_exists(AsyncCommandExtension::class)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't the class constant lookup fail when the class is not available?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wont

@temp

temp commented Apr 17, 2018

Copy link
Copy Markdown

Ok, got it. Had to manually edit the Configuration and the EnqueueExtension. Runs fine now, will report.

@rolandbrecht

Copy link
Copy Markdown

Maybe a small improvement we did:
We extended Enqueue\Client\Message, to have access to all message functions

<?php

namespace App\Async;

use Enqueue\Client\Message;

/**
 * Class RunCommand.
 */
final class RunCommand extends Message
{

    public function __construct(string $commandName, array $commandArguments = [], array $commandOptions = [], array $messageProperties = [], array $messageHeaders = [])
    {
        $argumentString = $this->createString($commandArguments);
        $optionsString = $this->createString($commandOptions);
        
        $message = sprintf('%s %s %s', $commandName, $argumentString, $optionsString);

        parent::__construct($message, $messageProperties, $messageHeaders);
    }

    private function createString(array $values): string
    {
        return implode(' ', $values);
    }
}
$command = new RunCommand('app:import-products', [$categoryId], ['--force']);
$command->setPriority(MessagePriority::HIGH);
$command->setDelay(60);
$this->producer->sendCommand(Commands::CONSOLE_COMMAND, $command);

@hslavich

Copy link
Copy Markdown

Any plan to merge this?

@makasim

makasim commented Aug 14, 2018

Copy link
Copy Markdown
Member Author

@hslavich today (:

@makasim makasim merged commit e50da16 into master Aug 14, 2018
@makasim makasim deleted the pr-250 branch August 14, 2018 08:46
ASKozienko pushed a commit that referenced this pull request Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants