Sha256: 079c0b868986e20d0e3ecb561f74eab52c02f4c024831cad08ecc47977f3d43d

Contents?: true

Size: 736 Bytes

Versions: 4

Compression:

Stored size: 736 Bytes

Contents

require 'delegate'
require 'thread'

# Controls the state of the Worker object, and wraps it's state transitions
# with a Mutex.
class Sqewer::StateLock < SimpleDelegator
  def initialize
    @m = Mutex.new
    m = VeryTinyStateMachine.new(:stopped)
    m.permit_state :starting, :running, :stopping, :stopped, :failed
    m.permit_transition :stopped => :starting, :starting => :running
    m.permit_transition :running => :stopping, :stopping => :stopped
    m.permit_transition :starting => :failed # Failed to start
    __setobj__(m)
  end
  
  def in_state?(some_state)
    @m.synchronize { __getobj__.in_state?(some_state) }
  end
    
  def transition!(to_state)
    @m.synchronize { __getobj__.transition!(to_state) }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sqewer-5.0.0 lib/sqewer/state_lock.rb
sqewer-4.2.0 lib/sqewer/state_lock.rb
sqewer-4.1.0 lib/sqewer/state_lock.rb
sqewer-4.0.1 lib/sqewer/state_lock.rb