Example #1
0
/**
 * The workflow engine provides access to the communication layer of the processing system.
 * Communication is based on ProophServiceBus. We use the idea of CQRS to decouple the
 * workflow processor from workflow message handlers which are responsible for processing
 * single tasks. A workflow message handler is normally the glue component which connects
 * Prooph\Processing with an external system. It receives commands from the processor like collect data or
 * process data and send events back to tell the processor what's happened.
 *
 * Each target (external system) gets a command bus and an event bus assigned. These are the communication
 * channels between the processor and the target.
 * You can use the full power of ProophServiceBus so a channel can be a local bus, a link to a messaging infrastructure,
 * a link to a worker queue, or a http remote interface.
 *
 * @return \Prooph\Processing\Processor\RegistryWorkflowEngine
 */
function _set_up_workflow_engine()
{
    $commandBus = new \Prooph\ServiceBus\CommandBus();
    $eventBus = new \Prooph\ServiceBus\EventBus();
    $commandRouter = new \Prooph\ServiceBus\Router\CommandRouter();
    //For our scenario it is enough to use a closure as workflow message handler
    //In a production system this should be a class loadable by Zend\ServiceManager
    //See the more complex scenarios to get an idea how such a set up can be look like.
    $commandRouter->route(\Prooph\Processing\Message\MessageNameUtils::getProcessDataCommandName('Prooph\\ProcessingExample\\Type\\SourceUser'))->to(function (\Prooph\Processing\Message\WorkflowMessage $message) use($eventBus) {
        $dataAsJsonString = json_encode($message->payload());
        $answer = $message->answerWithDataProcessingCompleted();
        try {
            \Zend\Stdlib\ErrorHandler::start();
            if (!file_put_contents('data/target-data.txt', $dataAsJsonString)) {
                \Zend\Stdlib\ErrorHandler::stop(true);
            }
        } catch (\Exception $ex) {
            $answer = \Prooph\Processing\Message\LogMessage::logException($ex, $message);
        }
        $eventBus->dispatch($answer);
    });
    $commandBus->utilize($commandRouter);
    $commandBus->utilize(new \Prooph\ServiceBus\InvokeStrategy\CallbackStrategy());
    $workflowEngine = new \Prooph\Processing\Processor\RegistryWorkflowEngine();
    $workflowEngine->registerCommandBus($commandBus, ['target-file-writer']);
    $workflowEngine->registerEventBus($eventBus, [\Prooph\Processing\Processor\Definition::SERVICE_WORKFLOW_PROCESSOR]);
    return $workflowEngine;
}
Example #2
0
        if ($recordedEvent instanceof \Prooph\Done\Story\Event\ChapterWasStarted) {
            echo sprintf("%s started. Chapter Number: %s\n", $recordedEvent->chapterStatus()->chapterName()->toString(), $recordedEvent->chapterStatus()->chapterNumber()->number());
            continue;
        }
        if ($recordedEvent instanceof \Prooph\Done\Story\Event\ChapterWasDone) {
            echo sprintf("%s DONE!. Chapter number: %s\n", $recordedEvent->chapterStatus()->chapterName()->toString(), $recordedEvent->chapterStatus()->chapterNumber()->number());
            continue;
        }
        $messageInfo = $recordedEvent instanceof \Prooph\Done\Story\Event\ChapterMessageWasReceived ? $recordedEvent->payload()['causation_message']['message_name'] : $recordedEvent->messageName();
        echo sprintf("Event %s recorded.\n", $messageInfo);
    }
});
//Set up infrastructure required by the story teller
$storyLog = new \Prooph\Done\Infrastructure\EventStoreStoryLog($eventStore);
$commandRouter = new \Prooph\ServiceBus\Plugin\Router\CommandRouter();
$commandBus = new \Prooph\ServiceBus\CommandBus();
$commandBus->utilize($commandRouter);
//Enable transaction handling based on command dispatch
$transactionManager = new \Prooph\EventStoreBusBridge\TransactionManager($eventStore);
$commandBus->utilize($transactionManager);
$delayedCommandBus = new \Prooph\Done\Infrastructure\DelayedCommandBus($commandBus);
//The delayed command bus queues commands until next EventStore.commit.post event occurs
$delayedCommandBus->setUp($eventStore);
//As a Story can handle and dispatch any message implementing Prooph\Common\Messaging\Message
//it requires a message factory and message converter to be able to create and log  messages correctly
$messageFactory = new \Prooph\Common\Messaging\FQCNMessageFactory();
$messageConverter = new \Prooph\Common\Messaging\NoOpMessageConverter();
//Set up the story
$storyName = \Prooph\Done\Shared\StoryName::fromString('Json to CSV Conversion');
//Set up the StoryTeller - basically a StoryTeller is a generic message handler but specifically
//designed to handle a single Story configured with so called ChapterTemplates
<?php

/*
 * This file is part of the prooph/php-service-bus.
 * (c) Alexander Miertsch <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 * 
 * Date: 16.03.14 - 23:46
 */
chdir(__DIR__);
require_once '../../vendor/autoload.php';
include 'classes.php';
//Attach php-resque worker to a queue via environment variable
putenv('QUEUE=resque-sample');
//Set up command bus to dispatch incoming message to it's handler
$commandBus = new \Prooph\ServiceBus\CommandBus();
//In the example we will only receive one type of command so we only define one route
$commandBus->utilize(new \Prooph\ServiceBus\Router\CommandRouter(['Prooph\\ServiceBus\\Example\\Resque\\WriteLine' => new \Prooph\ServiceBus\Example\Resque\FileWriter(__DIR__ . '/dump.txt')]));
//The php-resque message consumer will receive a @see \Prooph\ServiceBus\Message\StandardMessage
//Before the FileWriter can handle the message it needs to be translated back to a command
$commandBus->utilize(new \Prooph\ServiceBus\Message\FromRemoteMessageTranslator());
//The FileWriter provides a handleWriteLine method so we can use the HandleCommandStrategy to invoke it
$commandBus->utilize(new \Prooph\ServiceBus\InvokeStrategy\HandleCommandStrategy());
//The CommandBus needs to be globally available so that the message consumer can forward the incoming messages to it
\Prooph\ServiceBus\StaticBusRegistry::setCommandBus($commandBus);
//The StaticBusRegistry requires both bus types to be set. The EventBus won't be used in our example so we just
//register a default EventBus
\Prooph\ServiceBus\StaticBusRegistry::setEventBus(new \Prooph\ServiceBus\EventBus());
include '../../vendor/chrisboulton/php-resque/resque.php';