rethinkdb_job 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ class RethinkDBJob
2
+ module Setup
3
+ def rql_db_string(rql_db)
4
+ rql_db.instance_variable_get("@body")[1].first
5
+ end
6
+
7
+ def database
8
+ # ugly hack until @rdb.default_db works properly
9
+ db = rdb.instance_variable_get("@default_opts")[:db]
10
+ if db
11
+ rql_db_string(db)
12
+ else
13
+ @rdb_config[:db] || "test"
14
+ end
15
+ end
16
+
17
+ def done_setup?
18
+ @done_setup
19
+ end
20
+
21
+ def ensure_setup
22
+ unless done_setup?
23
+ setup
24
+ @done_setup = true
25
+ end
26
+ end
27
+
28
+ def setup
29
+ begin
30
+ rdb
31
+ rescue => e
32
+ msg = "cannot connect to RethinkDB #{@rdb_config.inspect}"
33
+ raise e.class, e.message + ", #{msg}", caller
34
+ end
35
+
36
+ begin
37
+ r.db_create(database).run(rdb)
38
+ rescue RethinkDB::RqlRuntimeError => err
39
+ # already exists, cool
40
+ rescue => e
41
+ msg = "cannot create db #{database.inspect}"
42
+ raise e.class, e.message + ", #{msg}", caller
43
+ end
44
+
45
+ begin
46
+ r.db(database).table_create(table).run(rdb)
47
+ rescue RethinkDB::RqlRuntimeError => err
48
+ # already exists, cool
49
+ rescue => e
50
+ msg = "cannot create table #{table.inspect} on database #{database.inspect}"
51
+ raise e.class, e.message + ", #{msg}", caller
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module RethinkdbJob
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'rethinkdb'
2
+ require 'rethinkdb_job/setup'
3
+
4
+ class RethinkDBJob
5
+ attr_reader :table, :r
6
+
7
+ include RethinkDBJob::Setup
8
+
9
+ def initialize(rdb_config={})
10
+ @rdb_config = rdb_config
11
+ @table = @rdb_config.delete(:table) || "jobs"
12
+ @done_setup = false
13
+
14
+ @r = RethinkDB::RQL.new
15
+ end
16
+
17
+ def rdb
18
+ @rdb ||= @r.connect(@rdb_config)
19
+ end
20
+
21
+ def create
22
+ ensure_setup
23
+ result = @r.table(@table).insert({}, :return_vals => true).run(@rdb)
24
+ if result["inserted"] == 1
25
+ result["new_val"]["id"]
26
+ else
27
+ raise "Unable to create new job record"
28
+ end
29
+ end
30
+
31
+ def find(job_id)
32
+ @r.table(@table).get(job_id).run(@rdb)
33
+ end
34
+
35
+ TIMESTAMP_COLUMNS = [:job_start, :job_finish]
36
+ def set_timestamp(job_id, column, time=Time.now)
37
+ ensure_setup
38
+ if TIMESTAMP_COLUMNS.include?(column.to_sym)
39
+ @r.table(@table).get(job_id).update(column => time).run(@rdb)
40
+ time
41
+ else
42
+ raise "expected column to be one of #{timestamp_columns.inspect} (#{column.inspect})"
43
+ end
44
+ end
45
+
46
+ def logdir
47
+ @rdb_config[:logdir]
48
+ end
49
+
50
+ def logfile(job_id)
51
+ File.join(logdir, "job_#{job_id}.log")
52
+ end
53
+
54
+ def tail(job_id, lines=10)
55
+ `tail -#{Integer(lines).to_s} #{logfile(job_id)} 2>&1`
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rethinkdb_job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Duane Johnson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rethinkdb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.13.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: 1.13.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: debugger
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: guard
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
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Stores job queue data in rethinkdb
79
+ email:
80
+ - duane.johnson@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/rethinkdb_job.rb
86
+ - lib/rethinkdb_job/setup.rb
87
+ - lib/rethinkdb_job/version.rb
88
+ homepage: https://github.com//rethinkdb_job
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project: ! '[none]'
108
+ rubygems_version: 1.8.23
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Stores job queue data in rethinkdb
112
+ test_files: []
113
+ has_rdoc: