mrhash/taskmachine

具有验证状态机完整性的模块化微服务任务管道与编排。

dev-master 2019-11-17 22:27 UTC

This package is auto-updated.

Last update: 2024-09-18 09:33:38 UTC


README

具有验证状态机完整性的模块化微服务任务管道与编排。

定义微服务任务,并通过简单的表达式API将它们安排到状态机管理的流程中。您可以创建构建工具链、处理管道、实用服务,甚至服务网页...

面向大众的状态机!

示例

简单管道

我们定义了两个简单的内联任务,它们是独立的。机器按顺序执行这两个任务,然后完成。

$tmb = new TaskMachineBuilder;

// Define some tasks
$tmb->task('hello', function () {
  echo 'Hello World';
});

$tmb->task('goodbye', function () {
  echo 'Goodbye World';
});

// Define and build a machine
$tm = $tmb->machine('greetings')
  // specify an initial task and transition
  ->hello([
    'initial' => true,
    'transition' => 'goodbye'
  ])
   // specify a final task
  ->goodbye(['final' => true])
  ->build();

// Run the machine.
$tm->run('greetings');

具有依赖注入的管道

现在我们引入了一些具有依赖注入的更多任务。任务根据定义是隔离的,并且可以选择有预期的输入和输出。

// Bootstrap your own Auryn injector and throw it in
$tmb = new TaskMachineBuilder(new TaskFactory($myInjector));

// Define some tasks
$tmb->task(
  'translate',
  function (InputInterface $input, MyTranslationInterface $translator) {
    // Auryn injects fully constructed dependencies. Run your things.
    $translation = $translator->translate($input->get('text'));
    return ['text' => $translation];
  }
);

// Input from previous task is injectable and immutable
$tmb->task('echo', function (InputInterface $input) {
  echo $input->get('text');
});

$tmb->task('goodbye', function () {
  return ['closing' => 'Goodbye World'];
});

 // Define and build machine
$tm = $tmb->machine('translator')
  ->translate([
    'initial' => true,
    'transition' => 'echo'
  ])
  ->echo(['transition' => 'goodbye'])
  ->goodbye(['final' => true])
  ->build();

// Run with input and then echo the output from the last task
$output = $tm->run('translator', ['text' => 'Hello World']);
echo $output->get('closing');

您机器配置中的任何错误都可能导致构建错误!任务必须正确链接,并且具有有效且明确的转换。

条件分支

机器可以根据在Symfony表达式语言中编写的条件分支到不同的任务。

$tmb = new TaskMachineBuilder(new TaskFactory($myInjector));

// Define some tasks
$tmb->task('process', function () {
  // This outputs a random true or false result
  $result = (bool)random_int(0,1);
  return ['success' => $result];
});

// Task with your instantiated object which implements TaskHandlerInterface
$tmb->task('finish', new Finisher($myService));

// Task with your handler which implements TaskHandlerInterface
// Your dependencies are injected
$tmb->task('fail', MyCustomServiceInterface::class);

// Define and build a machine with different final outcomes
$tm = $tmb->machine('switcher')
  ->process([
    'initial' => true,
    // Specify switch conditions to subsequent tasks
    'transition' => [
      'output.success' => 'finish',
      '!output.success' => 'fail'
    ]
  ])
  ->finish(['final' => true])
  ->fail(['final' => true])
  ->build();
  
// Run it.
$tm->run('switcher');