Class: DopCommon::StateStore
- Inherits:
-
YAML::Store
- Object
- YAML::Store
- DopCommon::StateStore
- Defined in:
- lib/dop_common/state_store.rb
Instance Method Summary collapse
-
#initialize(state_file, plan_name, plan_cache) ⇒ StateStore
constructor
A new instance of StateStore.
-
#on_change ⇒ Object
This method will take a block which will be executet every time the state file changes.
- #pending_updates? ⇒ Boolean
-
#transaction(read_only = false, &block) ⇒ Object
This is a wrapper around transaction to make sure we have a run lock.
-
#update ⇒ Object
update the state file.
- #version ⇒ Object
Constructor Details
#initialize(state_file, plan_name, plan_cache) ⇒ StateStore
Returns a new instance of StateStore.
14 15 16 17 18 19 20 |
# File 'lib/dop_common/state_store.rb', line 14 def initialize(state_file, plan_name, plan_cache) @plan_name = plan_name @plan_cache = plan_cache @state_file = state_file @write_mutex = Mutex.new super(@state_file) end |
Instance Method Details
#on_change ⇒ Object
This method will take a block which will be executet every time the state file changes.
76 77 78 79 80 81 82 |
# File 'lib/dop_common/state_store.rb', line 76 def on_change notifier = INotify::Notifier.new notifier.watch(@state_file, :modify) do yield end notifier.run end |
#pending_updates? ⇒ Boolean
48 49 50 51 52 53 |
# File 'lib/dop_common/state_store.rb', line 48 def pending_updates? case version when :new, latest_version then false else true end end |
#transaction(read_only = false, &block) ⇒ Object
This is a wrapper around transaction to make sure we have a run lock. This will ensure that only ever one instance can write to this store.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dop_common/state_store.rb', line 24 def transaction(read_only = false, &block) if read_only super(read_only, &block) else @write_mutex.synchronize do if @plan_cache.run_lock?(@plan_name) # save the version on first write super do self[:version] = latest_version if self[:version].nil? end super(&block) else raise StandardError, "Not possible to write to #{@state_file} because we have no run lock" end end end end |
#update ⇒ Object
update the state file. This takes a block which will receive a hash diff from the state version to the newest plan version. If the plan is new or already on the latest version the block will not be executed.
The block is already inside a transaction. The version will be bumped to the latest version only if the transaction is successful.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dop_common/state_store.rb', line 62 def update ver = version return if ver == latest_version return if ver == :new raise UnknownVersionError.new(ver) unless version_exists?(ver) DopCommon.log.info("Updating plan #{@plan_name} from version #{ver} to #{latest_version}") transaction do yield(@plan_cache.get_plan_hash_diff(@plan_name, ver, latest_version)) self[:version] = latest_version end end |
#version ⇒ Object
42 43 44 45 46 |
# File 'lib/dop_common/state_store.rb', line 42 def version transaction(true) do self[:version] || :new end end |