Class: Abid::Session

Inherits:
Object
  • Object
show all
Extended by:
MonitorMixin
Defined in:
lib/abid/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/abid/session.rb', line 10

def initialize(task)
  @task = task
  @state = task.state

  @entered = false
  @locked = false
  @result = nil
  @error = nil
  @ivar = Concurrent::IVar.new

  @on_success = []
  @on_fail = []
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/abid/session.rb', line 8

def error
  @error
end

Instance Method Details

#add_observer(*args, &block) ⇒ Object



43
44
45
# File 'lib/abid/session.rb', line 43

def add_observer(*args, &block)
  @ivar.add_observer(*args, &block)
end

#cancel(error) ⇒ Object



85
86
87
88
89
90
# File 'lib/abid/session.rb', line 85

def cancel(error)
  unlock(error)
  @result = :canceled
  @error = error
  @ivar.fail(error) rescue Concurrent::MultipleAssignmentError
end

#capture_exception(&block) ⇒ Object



37
38
39
40
41
# File 'lib/abid/session.rb', line 37

def capture_exception(&block)
  block.call
rescue Exception => e
  self.fail(e)
end

#enter(&block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/abid/session.rb', line 28

def enter(&block)
  synchronize do
    return @ivar if @entered
    @entered = true
  end
  block.call
  @ivar
end

#fail(error) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/abid/session.rb', line 76

def fail(error)
  @result = :failed
  @error = error
  unlock(error)
  @ivar.fail(error) rescue Concurrent::MultipleAssignmentError
rescue Exception => e
  @ivar.fail(e) rescue Concurrent::MultipleAssignmentError
end

#lockObject



47
48
49
50
51
52
53
54
55
# File 'lib/abid/session.rb', line 47

def lock
  synchronize do
    @state.start unless @locked
    @locked = true
    true
  end
rescue AbidErrorTaskAlreadyRunning
  false
end

#skipObject



70
71
72
73
74
# File 'lib/abid/session.rb', line 70

def skip
  unlock
  @result = :skipped
  @ivar.try_set(false)
end

#successObject



64
65
66
67
68
# File 'lib/abid/session.rb', line 64

def success
  unlock
  @result = :successed
  @ivar.try_set(true)
end

#synchronize(&block) ⇒ Object



24
25
26
# File 'lib/abid/session.rb', line 24

def synchronize(&block)
  self.class.synchronize(&block)
end

#unlock(error = nil) ⇒ Object



57
58
59
60
61
62
# File 'lib/abid/session.rb', line 57

def unlock(error = nil)
  synchronize do
    @state.finish(error) if @locked
    @locked = false
  end
end