lib/pgjob/job.rb in pgjob-0.1.0 vs lib/pgjob/job.rb in pgjob-0.1.1

- old
+ new

@@ -22,11 +22,12 @@ @conn.exec <<-SQL CREATE TABLE jobs ( id SERIAL, name character varying(255), params text, - status character varying(20), + mode character varying(20), + status character varying(255), log text ); SQL end end @@ -48,12 +49,12 @@ # Creates new job def create(name, params = {}) assert_symbol_keys!(params) sql = <<-SQL - INSERT INTO jobs(name, params, status) - VALUES('#{@conn.escape(name)}', '#{@conn.escape JSON.dump(params)}', 'wait') + INSERT INTO jobs(name, params, mode, status) + VALUES('#{@conn.escape(name)}', '#{@conn.escape JSON.dump(params)}', 'wait', 'Start') RETURNING id; SQL @conn.exec(sql).to_a.first['id'].to_i end @@ -66,26 +67,30 @@ SQL @conn.exec(sql).to_a.first['name'] end # Sets/Returns status - def status(id, value = nil) - if value - raise "wrong value" unless [:wait, :running, :success, :failed].include?(value.to_sym) + def status(id, mode = nil, value = nil ) + if mode + raise "wrong mode #{mode}" unless [:running, :success, :failed].include?(mode.to_sym) + raise "set value" unless value sql = <<-SQL UPDATE jobs - SET status = '#{@conn.escape value.to_s}' + SET + mode = '#{mode}', + status = '#{@conn.escape value}' WHERE id = #{id}; SQL @conn.exec(sql) + add_log(id, "[mode] #{mode} - #{value}") else sql = <<-SQL SELECT status FROM jobs WHERE id = #{id}; SQL - @conn.exec(sql).to_a.first['status'].to_sym + @conn.exec(sql).to_a.first['status'] end end # Returns params def params(id) @@ -115,10 +120,26 @@ WHERE id = #{id}; SQL @conn.exec(sql) end + def running?(id) + [:wait, :running].include? mode(id) + end + + def finished?(id) + [:success, :failed].include? mode(id) + end + + def success?(id) + mode(id) == :success + end + + def failed?(id) + mode(id) == :failed + end + private def assert_symbol_keys!(hash) hash.each do |key, value| unless key.is_a?(Symbol) raise "#{key} should be symbol" @@ -138,9 +159,18 @@ else out[key.to_sym] = value end end out + end + + def mode(id) + sql = <<-SQL + SELECT mode + FROM jobs + WHERE id = #{id}; + SQL + @conn.exec(sql).to_a.first['mode'].to_sym end def tables sql = <<-SQL SELECT table_name