Sha256: 13097ada231ca06f1a0a42229f87001408ac608c2fde12cd67b0445ebabd0c3d

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

example_description = <<DESC
  Sub Plans Example
  ===================

  This example shows, how to trigger the execution plans from within a
  run method of some action and waing for them to finish.

  This is useful, when doing bulk actions, where having one big
  execution plan would not be effective, or in case all the data are
  not available by the time of original action planning.

DESC

require_relative 'example_helper'
require_relative 'orchestrate_evented'

COUNT = (ARGV[0] || 25).to_i

class Foo < Dynflow::Action
  def plan
    plan_self
  end

  def run(event = nil)
    case event
    when nil
      rng = Random.new
      plan_event(:ping, rng.rand(25) + 1)
      suspend
    when :ping
      # Finish
    end
  end
end

class SubPlansExample < Dynflow::Action
  include Dynflow::Action::V2::WithSubPlans

  def initiate
    limit_concurrency_level! 3
    super
  end

  def create_sub_plans
    current_batch.map { |i| trigger(Foo) }
  end

  def batch_size
    15
  end

  def batch(from, size)
    COUNT.times.drop(from).take(size)
  end

  def total_count
    COUNT
  end
end

if $0 == __FILE__
  ExampleHelper.world.action_logger.level = Logger::DEBUG
  ExampleHelper.world
  t1 = ExampleHelper.world.trigger(SubPlansExample)
  puts example_description
  puts <<-MSG.gsub(/^.*\|/, '')
    |  Execution plans #{t1.id} with sub plans triggered
    |  You can see the details at
    |    #{ExampleHelper::DYNFLOW_URL}/#{t1.id}
  MSG

  ExampleHelper.run_web_console
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynflow-1.9.0 examples/sub_plans_v2.rb
dynflow-1.8.3 examples/sub_plans_v2.rb