pgjob 0.1.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script:
5
+ - bundle exec rake db:prepare && bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # ChangeLog
2
+
3
+ ## PGJob 0.1.0 (July 8, 2012)
4
+
5
+ * First public release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pgjob.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexey Vakhov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # PGJob
2
+
3
+ Custom delayed jobs on pure PosgreSQL.
4
+
5
+ [<img src="https://secure.travis-ci.org/avakhov/pgjob.png"/>](http://travis-ci.org/avakhov/pgjob)
6
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ def exec(cmd); puts "[exec] #{cmd}"; system cmd; end
5
+
6
+ namespace :db do
7
+ desc "prepare db for specs"
8
+ task "prepare" do
9
+ exec "createdb pgjob_test"
10
+ end
11
+ end
data/lib/pgjob/job.rb ADDED
@@ -0,0 +1,155 @@
1
+ module PGJob
2
+ class Job
3
+ # Initialize with AR::connection or attributes for PG.connect
4
+ def initialize(*args, &block)
5
+ if args.first.respond_to?(:exec)
6
+ @conn = args.first
7
+ @can_finish = false
8
+ else
9
+ @conn = PG.connect(*args)
10
+ @can_finish = true
11
+ end
12
+
13
+ if block_given?
14
+ block.call(self)
15
+ finish!
16
+ end
17
+ end
18
+
19
+ # Creates jobs table
20
+ def migrate!
21
+ unless tables.include?('jobs')
22
+ @conn.exec <<-SQL
23
+ CREATE TABLE jobs (
24
+ id SERIAL,
25
+ name character varying(255),
26
+ params text,
27
+ status character varying(20),
28
+ log text
29
+ );
30
+ SQL
31
+ end
32
+ end
33
+
34
+ # Destroys jobs table
35
+ def rollback!
36
+ @conn.exec <<-SQL
37
+ DROP TABLE IF EXISTS jobs;
38
+ SQL
39
+ end
40
+
41
+ # Closes connection if possible
42
+ def finish!
43
+ if @can_finish
44
+ @conn.finish
45
+ end
46
+ end
47
+
48
+ # Creates new job
49
+ def create(name, params = {})
50
+ assert_symbol_keys!(params)
51
+
52
+ sql = <<-SQL
53
+ INSERT INTO jobs(name, params, status)
54
+ VALUES('#{@conn.escape(name)}', '#{@conn.escape JSON.dump(params)}', 'wait')
55
+ RETURNING id;
56
+ SQL
57
+ @conn.exec(sql).to_a.first['id'].to_i
58
+ end
59
+
60
+ # Returns name
61
+ def name(id)
62
+ sql = <<-SQL
63
+ SELECT name
64
+ FROM jobs
65
+ WHERE id = #{id};
66
+ SQL
67
+ @conn.exec(sql).to_a.first['name']
68
+ end
69
+
70
+ # Sets/Returns status
71
+ def status(id, value = nil)
72
+ if value
73
+ raise "wrong value" unless [:wait, :running, :success, :failed].include?(value.to_sym)
74
+ sql = <<-SQL
75
+ UPDATE jobs
76
+ SET status = '#{@conn.escape value.to_s}'
77
+ WHERE id = #{id};
78
+ SQL
79
+ @conn.exec(sql)
80
+ else
81
+ sql = <<-SQL
82
+ SELECT status
83
+ FROM jobs
84
+ WHERE id = #{id};
85
+ SQL
86
+ @conn.exec(sql).to_a.first['status'].to_sym
87
+ end
88
+ end
89
+
90
+ # Returns params
91
+ def params(id)
92
+ sql = <<-SQL
93
+ SELECT params
94
+ FROM jobs
95
+ WHERE id = #{id};
96
+ SQL
97
+ symbolize_keys JSON.load(@conn.exec(sql).to_a.first['params'])
98
+ end
99
+
100
+ # Returns full log
101
+ def log(id)
102
+ sql = <<-SQL
103
+ SELECT log
104
+ FROM jobs
105
+ WHERE id = #{id};
106
+ SQL
107
+ @conn.exec(sql).to_a.first['log']
108
+ end
109
+
110
+ # Adds log message
111
+ def add_log(id, msg)
112
+ sql = <<-SQL
113
+ UPDATE jobs
114
+ SET log = '#{@conn.escape [log(id), msg].compact.join("\n")}'
115
+ WHERE id = #{id};
116
+ SQL
117
+ @conn.exec(sql)
118
+ end
119
+
120
+ private
121
+ def assert_symbol_keys!(hash)
122
+ hash.each do |key, value|
123
+ unless key.is_a?(Symbol)
124
+ raise "#{key} should be symbol"
125
+ end
126
+
127
+ if value.is_a?(Hash)
128
+ assert_symbol_keys!(value)
129
+ end
130
+ end
131
+ end
132
+
133
+ def symbolize_keys(hash)
134
+ out = {}
135
+ hash.each do |key, value|
136
+ if value.is_a?(Hash)
137
+ out[key.to_sym] = symbolize_keys(value)
138
+ else
139
+ out[key.to_sym] = value
140
+ end
141
+ end
142
+ out
143
+ end
144
+
145
+ def tables
146
+ sql = <<-SQL
147
+ SELECT table_name
148
+ FROM information_schema.tables
149
+ WHERE table_schema = 'public';
150
+ SQL
151
+
152
+ @conn.exec(sql).map { |r| r['table_name'] }
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module PGJob
2
+ VERSION = "0.1.0"
3
+ end
data/lib/pgjob.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'pgjob/version'
2
+ require 'pg'
3
+ require 'json'
4
+ require 'pgjob/job'
data/pgjob.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/pgjob/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alexey Vakhov"]
6
+ gem.email = ["vakhov@gmail.com"]
7
+ gem.description = %q{PgJob}
8
+ gem.summary = %q{Pure PostgreSQL Job}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "pgjob"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = PGJob::VERSION
17
+
18
+ gem.add_dependency 'pg'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
21
+ end
data/spec/job_spec.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe PGJob::Job do
4
+ before do
5
+ @job = PGJob::Job.new(dbname: 'pgjob_test')
6
+ end
7
+
8
+ after do
9
+ @job.finish!
10
+ end
11
+
12
+ describe '#create' do
13
+ it 'success' do
14
+ id = @job.create("test", key: 'value')
15
+ id.should > 0
16
+ id.should be_kind_of(Fixnum)
17
+ end
18
+
19
+ it 'accepts only symbol keys' do
20
+ -> {
21
+ @job.create("test", 'key' => 'value')
22
+ }.should raise_error
23
+
24
+ -> {
25
+ @job.create("test", :hash => {'key' => 'value'})
26
+ }.should raise_error
27
+ end
28
+ end
29
+
30
+ describe '#status' do
31
+ it 'returns' do
32
+ id = @job.create("test", key: 'value')
33
+ @job.status(id).should == :wait
34
+ end
35
+
36
+ it 'sets' do
37
+ id = @job.create("test", key: 'value')
38
+ @job.status(id, :running)
39
+ @job.status(id).should == :running
40
+ end
41
+ end
42
+
43
+ it '#name' do
44
+ id = @job.create("test", key: 'value')
45
+ @job.name(id).should == 'test'
46
+ end
47
+
48
+ describe '#params' do
49
+ it 'returns' do
50
+ id = @job.create("test", key: 'value')
51
+ @job.params(id).should == {key: 'value'}
52
+ end
53
+
54
+ it 'returns integer values' do
55
+ id = @job.create("test", key: 1054)
56
+ @job.params(id).should == {key: 1054}
57
+ end
58
+
59
+ it 'symbolizes all keys' do
60
+ id = @job.create("test", hash: {key: 1054})
61
+ @job.params(id).should == {hash: {key: 1054}}
62
+ end
63
+ end
64
+
65
+ describe '#log' do
66
+ it "success" do
67
+ id = @job.create("test")
68
+ @job.add_log(id, 'msg1')
69
+ @job.add_log(id, 'msg2')
70
+ @job.log(id).should == "msg1\nmsg2"
71
+ end
72
+
73
+ it 'escape' do
74
+ id = @job.create("test")
75
+ hard = ['"', "'", '#', '@', '!', "\n"].join
76
+ @job.add_log(id, hard)
77
+ @job.log(id).should == hard
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path("../../lib/pgjob", __FILE__)
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:all) do
5
+ PGJob::Job.new(dbname: 'pgjob_test') { |job| job.migrate! }
6
+ end
7
+
8
+ config.after(:all) do
9
+ PGJob::Job.new(dbname: 'pgjob_test') { |job| job.rollback! }
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pgjob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Vakhov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pg
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: PgJob
63
+ email:
64
+ - vakhov@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - CHANGELOG.md
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - lib/pgjob.rb
78
+ - lib/pgjob/job.rb
79
+ - lib/pgjob/version.rb
80
+ - pgjob.gemspec
81
+ - spec/job_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: ''
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Pure PostgreSQL Job
107
+ test_files:
108
+ - spec/job_spec.rb
109
+ - spec/spec_helper.rb