Class: Abid::State

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

Constant Summary collapse

RUNNING =
1
SUCCESSED =
2
FAILED =
3
STATES =
constants.map { |c| [const_get(c), c] }.to_h

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ State

Returns a new instance of State.



61
62
63
64
65
66
67
# File 'lib/abid/state.rb', line 61

def initialize(task)
  @task = task
  @record = nil
  @ivar = Concurrent::IVar.new
  @already_invoked = false
  reload
end

Instance Attribute Details

#ivarObject (readonly)

Returns the value of attribute ivar.



59
60
61
# File 'lib/abid/state.rb', line 59

def ivar
  @ivar
end

Class Method Details

.deserialize(bytes) ⇒ Object



53
54
55
# File 'lib/abid/state.rb', line 53

def deserialize(bytes)
  YAML.load(bytes)
end

.find(task) ⇒ Object



16
17
18
# File 'lib/abid/state.rb', line 16

def find(task)
  synchronize { @cache[task.object_id] ||= new(task) }
end

.list(pattern: nil, started_before: nil, started_after: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abid/state.rb', line 20

def list(pattern: nil, started_before: nil, started_after: nil)
  dataset = Rake.application.database[:states]

  dataset = dataset.where { start_time < started_before } if started_before
  dataset = dataset.where { start_time > started_after } if started_after
  dataset = dataset.order(:start_time)

  dataset.map do |record|
    next if pattern && record[:name] !~ pattern
    {
      id: record[:id],
      name: record[:name],
      params: deserialize(record[:params]),
      state: STATES[record[:state]],
      start_time: record[:start_time],
      end_time: record[:end_time]
    }
  end.compact
end

.revoke(id) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/abid/state.rb', line 40

def revoke(id)
  db = Rake.application.database
  db.transaction do
    running = db[:states].where(id: id, state: RUNNING).count > 0
    fail 'task is now running' if running
    db[:states].where(id: id).delete
  end
end

.serialize(params) ⇒ Object



49
50
51
# File 'lib/abid/state.rb', line 49

def serialize(params)
  YAML.dump(params)
end

Instance Method Details

#assumeObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/abid/state.rb', line 130

def assume
  fail 'cannot revoke volatile task' if disabled?

  database.transaction do
    reload
    fail 'task is now running' if running?

    new_state = {
      state: SUCCESSED,
      start_time: Time.now,
      end_time: Time.now
    }

    if @record
      dataset.where(id: @record[:id]).update(new_state)
      @record = @record.merge(new_state)
    else
      id = dataset.insert(
        digest: digest,
        name: @task.name,
        params: serialize(@task.params),
        **new_state
      )
      @record = { id: id, **new_state }
    end
  end
end

#close_session(error = nil) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/abid/state.rb', line 196

def close_session(error = nil)
  return if disabled? || preview?
  return unless @record
  state = error ? FAILED : SUCCESSED
  dataset.where(id: @record[:id]).update(state: state, end_time: Time.now)
  reload
end

#databaseObject



77
78
79
# File 'lib/abid/state.rb', line 77

def database
  Rake.application.database
end

#datasetObject



81
82
83
# File 'lib/abid/state.rb', line 81

def dataset
  database[:states]
end

#digestObject



204
205
206
# File 'lib/abid/state.rb', line 204

def digest
  Digest::MD5.hexdigest(@task.name + "\n" + serialize(@task.params))
end

#disabled?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/abid/state.rb', line 85

def disabled?
  @task.volatile? || Rake.application.options.disable_state
end

#failed?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/abid/state.rb', line 122

def failed?
  state == FAILED
end

#idObject



106
107
108
# File 'lib/abid/state.rb', line 106

def id
  @record[:id] if @record
end

#not_found?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/abid/state.rb', line 126

def not_found?
  !disabled? && @record.nil?
end

#only_once(&block) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/abid/state.rb', line 69

def only_once(&block)
  self.class.synchronize do
    return if @already_invoked
    @already_invoked = true
  end
  block.call
end

#preview?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/abid/state.rb', line 89

def preview?
  Rake.application.options.dryrun || Rake.application.options.preview
end

#reloadObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/abid/state.rb', line 93

def reload
  return if disabled?

  if @record
    id = @record[:id]
    @record = dataset.where(id: id).first
  else
    @record = dataset.where(digest: digest).to_a.find do |r|
      [@task.name, @task.params].eql? [r[:name], deserialize(r[:params])]
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/abid/state.rb', line 114

def running?
  state == RUNNING
end

#session(&block) ⇒ Object



158
159
160
161
162
163
# File 'lib/abid/state.rb', line 158

def session(&block)
  started = start_session
  block.call
ensure
  close_session($ERROR_INFO) if started
end

#start_sessionObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/abid/state.rb', line 165

def start_session
  return true if disabled? || preview?

  database.transaction do
    reload

    fail AbidErrorTaskAlreadyRunning if running?

    new_state = {
      state: RUNNING,
      start_time: Time.now,
      end_time: nil
    }

    if @record
      dataset.where(id: @record[:id]).update(new_state)
      @record = @record.merge(new_state)
    else
      id = dataset.insert(
        digest: digest,
        name: @task.name,
        params: serialize(@task.params),
        **new_state
      )
      @record = { id: id, **new_state }
    end

    true
  end
end

#stateObject



110
111
112
# File 'lib/abid/state.rb', line 110

def state
  @record[:state] if @record
end

#successed?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/abid/state.rb', line 118

def successed?
  state == SUCCESSED
end