Class: PGJob::Job
- Inherits:
-
Object
- Object
- PGJob::Job
- Defined in:
- lib/pgjob/job.rb
Instance Method Summary collapse
-
#add_log(id, msg) ⇒ Object
Adds log message.
-
#create(name, params = {}) ⇒ Object
Creates new job.
- #failed?(id) ⇒ Boolean
-
#finish! ⇒ Object
Closes connection if possible.
- #finished?(id) ⇒ Boolean
-
#initialize(*args, &block) ⇒ Job
constructor
Initialize with AR::connection or attributes for PG.connect.
-
#log(id) ⇒ Object
Returns full log.
-
#migrate! ⇒ Object
Creates jobs table.
-
#name(id) ⇒ Object
Returns name.
-
#params(id) ⇒ Object
Returns params.
-
#rollback! ⇒ Object
Destroys jobs table.
- #running?(id) ⇒ Boolean
-
#status(id, mode = nil, value = nil) ⇒ Object
Sets/Returns status.
- #success?(id) ⇒ Boolean
Constructor Details
#initialize(*args, &block) ⇒ Job
Initialize with AR::connection or attributes for PG.connect
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/pgjob/job.rb', line 4 def initialize(*args, &block) if args.first.respond_to?(:exec) @conn = args.first @can_finish = false else @conn = PG.connect(*args) @can_finish = true end if block_given? block.call(self) finish! end end |
Instance Method Details
#add_log(id, msg) ⇒ Object
Adds log message
116 117 118 119 120 121 122 123 |
# File 'lib/pgjob/job.rb', line 116 def add_log(id, msg) sql = " UPDATE jobs\n SET log = '\#{@conn.escape [log(id), msg].compact.join(\"\\n\")}'\n WHERE id = \#{id};\n SQL\n @conn.exec(sql)\nend\n" |
#create(name, params = {}) ⇒ Object
Creates new job
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pgjob/job.rb', line 50 def create(name, params = {}) assert_symbol_keys!(params) sql = " INSERT INTO jobs(name, params, mode, status)\n VALUES('\#{@conn.escape(name)}', '\#{@conn.escape JSON.dump(params)}', 'wait', 'Start')\n RETURNING id;\n SQL\n @conn.exec(sql).to_a.first['id'].to_i\nend\n" |
#failed?(id) ⇒ Boolean
137 138 139 |
# File 'lib/pgjob/job.rb', line 137 def failed?(id) mode(id) == :failed end |
#finish! ⇒ Object
Closes connection if possible
43 44 45 46 47 |
# File 'lib/pgjob/job.rb', line 43 def finish! if @can_finish @conn.finish end end |
#finished?(id) ⇒ Boolean
129 130 131 |
# File 'lib/pgjob/job.rb', line 129 def finished?(id) [:success, :failed].include? mode(id) end |
#log(id) ⇒ Object
Returns full log
106 107 108 109 110 111 112 113 |
# File 'lib/pgjob/job.rb', line 106 def log(id) sql = " SELECT log\n FROM jobs\n WHERE id = \#{id};\n SQL\n @conn.exec(sql).to_a.first['log']\nend\n" |
#migrate! ⇒ Object
Creates jobs table
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pgjob/job.rb', line 20 def migrate! unless tables.include?('jobs') @conn.exec " CREATE TABLE jobs (\n id SERIAL,\n name character varying(255),\n params text,\n mode character varying(20),\n status character varying(255),\n log text\n );\n SQL\n end\nend\n" |
#name(id) ⇒ Object
Returns name
62 63 64 65 66 67 68 69 |
# File 'lib/pgjob/job.rb', line 62 def name(id) sql = " SELECT name\n FROM jobs\n WHERE id = \#{id};\n SQL\n @conn.exec(sql).to_a.first['name']\nend\n" |
#params(id) ⇒ Object
Returns params
96 97 98 99 100 101 102 103 |
# File 'lib/pgjob/job.rb', line 96 def params(id) sql = " SELECT params\n FROM jobs\n WHERE id = \#{id};\n SQL\n symbolize_keys JSON.load(@conn.exec(sql).to_a.first['params'])\nend\n" |
#rollback! ⇒ Object
Destroys jobs table
36 37 38 39 40 |
# File 'lib/pgjob/job.rb', line 36 def rollback! @conn.exec " DROP TABLE IF EXISTS jobs;\n SQL\nend\n" |
#running?(id) ⇒ Boolean
125 126 127 |
# File 'lib/pgjob/job.rb', line 125 def running?(id) [:wait, :running].include? mode(id) end |
#status(id, mode = nil, value = nil) ⇒ Object
Sets/Returns status
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pgjob/job.rb', line 72 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 = " UPDATE jobs\n SET\n mode = '\#{mode}',\n status = '\#{@conn.escape value}'\n WHERE id = \#{id};\n SQL\n @conn.exec(sql)\n add_log(id, \"[mode] \#{mode} - \#{value}\") \n else\n sql = <<-SQL\n SELECT status\n FROM jobs\n WHERE id = \#{id};\n SQL\n @conn.exec(sql).to_a.first['status']\n end\nend\n" |
#success?(id) ⇒ Boolean
133 134 135 |
# File 'lib/pgjob/job.rb', line 133 def success?(id) mode(id) == :success end |