-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathsingletons.rb
executable file
·55 lines (44 loc) · 1.69 KB
/
singletons.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env ruby
# frozen_string_literal: true
example_description = <<DESC
Sub Plans Example
===================
This example shows, how singleton actions can be used for making sure
there is only one instance of the action running at a time.
Singleton actions try to obtain a lock at the beggining of their plan
phase and fail if they can't do so. In run phase they check if they
have the lock and try to acquire it again if they don't. These actions
release the lock at the end of their finalize phase.
DESC
require_relative 'example_helper'
class SingletonExample < Dynflow::Action
include Dynflow::Action::Singleton
def run
sleep 10
end
end
class SingletonExampleA < SingletonExample; end
class SingletonExampleB < SingletonExample; end
if $0 == __FILE__
ExampleHelper.world.action_logger.level = Logger::INFO
ExampleHelper.world
t1 = ExampleHelper.world.trigger(SingletonExampleA)
t2 = ExampleHelper.world.trigger(SingletonExampleA)
ExampleHelper.world.trigger(SingletonExampleA) unless SingletonExampleA.singleton_locked?(ExampleHelper.world)
t3 = ExampleHelper.world.trigger(SingletonExampleB)
db = ExampleHelper.world.persistence.adapter.db
puts example_description
puts <<-MSG.gsub(/^.*\|/, '')
| 3 execution plans were triggered:
| #{t1.id} should finish successfully
| #{t3.id} should finish successfully because it is a singleton of different class
| #{t2.id} should fail because #{t1.id} holds the lock
|
| You can see the details at
| #{ExampleHelper::DYNFLOW_URL}/#{t1.id}
| #{ExampleHelper::DYNFLOW_URL}/#{t2.id}
| #{ExampleHelper::DYNFLOW_URL}/#{t3.id}
|
MSG
ExampleHelper.run_web_console
end