kit 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/lib/kit.rb +2 -3
  2. data/lib/kit/bit.rb +21 -4
  3. data/lib/kit/db_sqlite3.rb +14 -0
  4. metadata +3 -3
data/lib/kit.rb CHANGED
@@ -37,7 +37,7 @@ class Kit
37
37
  @@info = config[:info]
38
38
  @@unique = config[:unique]
39
39
  @@actions = config[:actions]
40
- @@status_types = [ :pending, :completed, :queued ]
40
+ @@status_types = [ :pending, :completed, :failed, :running ]
41
41
  @@db = Backend.new config[:db_config]
42
42
 
43
43
  # Run initialization specific to the kit.
@@ -112,7 +112,7 @@ class Kit
112
112
  fail NoAction, "#{action}" unless @@actions.include? action
113
113
  @@db.insert_action action, options
114
114
  rescue NoAction => ex
115
- puts "Could not add task: no such action: '#{ex.message}'."
115
+ puts "Could not add task, no such action: '#{ex.message}'."
116
116
  end
117
117
  end
118
118
 
@@ -133,7 +133,6 @@ class Kit
133
133
  end
134
134
 
135
135
  b.run_all
136
- # TODO try and use blocks here to set status to running and then complete / failed as each task runs and finishes
137
136
  end
138
137
  end
139
138
 
@@ -15,6 +15,14 @@ class Bit < Kit
15
15
  class MissingValues < RuntimeError
16
16
  end
17
17
 
18
+ # Raised when an action runs but fails due to an invalid combination of options.
19
+ class InvalidAction < RuntimeError
20
+ end
21
+
22
+ # Raised when an action runs but encounters a fatal error.
23
+ class FailedAction < RuntimeError
24
+ end
25
+
18
26
  attr_reader :id
19
27
 
20
28
  # Loads all bit info from database, or attempts to add new bit to database.
@@ -135,14 +143,23 @@ class Bit < Kit
135
143
 
136
144
  tasks.each do |t|
137
145
  a = t[:action]
146
+ id = t[:rowid]
147
+ status[a] ||= {}
138
148
  begin
149
+ @@db.update_action_status a, id, { :status => :running }
139
150
  self.send a, t
140
- status[a] = "complete"
141
- rescue
142
- status[a] = "failed"
151
+ stat = :complete
152
+ msg = nil
153
+ rescue InvalidAction, FailedAction => e
154
+ stat = :failed
155
+ msg = "#{e.class}: #{e.message}"
156
+ ensure
157
+ s = { :status => stat, :message => msg }
158
+ status[a].merge! ( { id => s } )
159
+ @@db.update_action_status a, id, s
143
160
  end
144
- status
145
161
  end
162
+ status
146
163
  end
147
164
 
148
165
  # Finds bit ids that match given criteria.
@@ -191,6 +191,20 @@ class Backend < Kit
191
191
  @action_db.last_insert_row_id
192
192
  end
193
193
 
194
+ # Updates the status of a task in an action table.
195
+ # @param [Symbol] table name of the action table
196
+ # @param [Integer] action rowid to update
197
+ # @param [Hash] data key / value pairs to set
198
+ def update_action_status table, id, data
199
+ data.merge! ( { :status => data[:status].to_s, :time => Time.now.to_i } )
200
+ set = []
201
+ data.each do |key, value|
202
+ set << "`#{key}` = '#{value}'"
203
+ end
204
+
205
+ @action_db.execute "UPDATE #{table} SET #{set.join ", "} WHERE `rowid` = '#{id}'"
206
+ end
207
+
194
208
  # def delete_action_by_id action, id
195
209
  # puts "DELETE FROM `#{action}` WHERE `rowid` = #{id}"
196
210
  # end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Evan Boyd Sosenko
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-08-06 00:00:00 -07:00
17
+ date: 2011-08-07 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency