Skip to content

Anonymous workflows

Andrei Horak edited this page Sep 2, 2019 · 2 revisions

Sometimes, the structure of workflows is dynamic and thus cannot be defined by subclassing Pallets::Workflow. For this, Pallets employs anonymous workflows (think of anonymous functions), which are nameless workflows ("disposable", unbound to any constant) that can be defined during runtime:

workflow = Pallets::Workflow.build do
  task 'Anonymous'
end

Running anonymous workflows is the same as running the regular ones:

workflow.new.run

Anonymous workflows open up a handful of scenarios for using Pallets in a more dynamic way. Say, for example, you're using Pallets to analyse parts and stitch them together after a multipart file upload. The workflow differs based on the number of uploaded parts (obviously), which is only known during runtime. This scenario could be modelled like this:

number_of_parts = 123

Pallets::Workflow.build do
  number_of_parts.times do |i|
    task 'Analyse', as: "AnalysePart#{i}"
  end
  task 'Stitch' => number_of_parts.times.map { |i| "AnalysePart#{i}" }
end

In order to leverage dynamic usage of workflows, Pallets provides task aliases, using task 'ClassName', as: 'Alias'. This way, a task can be defined multiple times inside a workflow, and can also be depended upon by other tasks.

As you can see, anonymous workflows allow plenty of dynamic scenarios to be modelled using Pallets!

Clone this wiki locally