sp-job 0.1.17 → 0.2.2

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.
@@ -18,29 +18,22 @@
18
18
  #
19
19
  # encoding: utf-8
20
20
  #
21
-
22
21
  module SP
23
22
  module Job
24
23
 
25
24
  class Worker < Backburner::Workers::Simple
26
25
 
27
- def initialize (tube_names=nil)
28
- super(tube_names)
29
- end
30
-
31
26
  def start
32
27
  prepare
33
28
  loop do
34
29
  work_one_job
35
30
  unless connection.connected?
36
31
  log_error "Connection to beanstalk closed, exiting now"
37
- exit
32
+ Kernel.exit
38
33
  end
39
34
  end
40
35
  end
41
36
 
42
- private
43
-
44
37
  end # Worker
45
38
  end # Module Job
46
39
  end # Module SP
@@ -0,0 +1,57 @@
1
+ #
2
+ #
3
+ # Copyright (c) 2011-2017 Cloudware S.A. All rights reserved.
4
+ #
5
+ # This file is part of sp-job.
6
+ #
7
+ # sp-job is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Affero General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # sp-job is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Affero General Public License
18
+ # along with sp-job. If not, see <http://www.gnu.org/licenses/>.
19
+ #
20
+ # encoding: utf-8
21
+ #
22
+ module SP
23
+ module Job
24
+
25
+ class WorkerThread < Backburner::Workers::Simple
26
+
27
+ # Used to prepare job queues before processing jobs.
28
+ # Setup beanstalk tube_names and watch all specified tubes for jobs.
29
+ #
30
+ # @raise [Beaneater::NotConnected] If beanstalk fails to connect.
31
+ # @example
32
+ # @worker.prepare
33
+ #
34
+ def prepare
35
+ log_info "Working #{tube_names.size} queues: [ #{tube_names.join(', ')} ]"
36
+ $config[:options][:threads].times do
37
+ connection = new_connection.tap{ |conn| conn.tubes.watch!(*tube_names) }
38
+ connection.on_reconnect = lambda { |conn| conn.tubes.watch!(*tube_names) }
39
+
40
+ $threads << Thread.new {
41
+ $thread_data[Thread.current] = ::SP::Job::ThreadData.new
42
+ logger.debug "Thread for #{tube_names.join(',')} #{Thread.current}"
43
+ loop do
44
+ work_one_job(connection)
45
+ unless connection.connected?
46
+ log_error "Connection to beanstalk closed, exiting now"
47
+ Kernel.exit
48
+ end
49
+ end
50
+ }
51
+ end
52
+ #end
53
+ end
54
+
55
+ end # Worker
56
+ end # Module Job
57
+ end # Module SP
@@ -5,8 +5,38 @@ require 'ostruct'
5
5
  require 'awesome_print'
6
6
  require 'os'
7
7
  require 'fileutils'
8
+ require 'tempfile'
8
9
  require 'etc'
9
10
 
11
+ class SpDataStruct < OpenStruct
12
+
13
+ def self.to_hash_sp (object)
14
+ hash = {}
15
+ object.each_pair do |key, value|
16
+ if value.is_a?(SpDataStruct)
17
+ hash[key] = SpDataStruct::to_hash_sp(value)
18
+ elsif value.is_a?(Array)
19
+ hash[key] = []
20
+ value.each do |member|
21
+ if member.is_a?(SpDataStruct)
22
+ hash[key] << SpDataStruct::to_hash_sp(member)
23
+ else
24
+ hash[key] << member
25
+ end
26
+ end
27
+ else
28
+ hash[key] = value
29
+ end
30
+ end
31
+ hash
32
+ end
33
+
34
+ def to_json
35
+ SpDataStruct::to_hash_sp(self).to_json
36
+ end
37
+
38
+ end
39
+
10
40
  def safesudo(cmd)
11
41
  unless true == system(cmd)
12
42
  system("sudo #{cmd}")
@@ -15,7 +45,7 @@ end
15
45
 
16
46
  def create_directory (path)
17
47
 
18
- if ! Dir.exists?(path)
48
+ if ! Dir.exist?(path)
19
49
  if OS.mac?
20
50
  if path.match("^/usr/local/")
21
51
  info = Etc.getpwnam(Etc.getlogin)
@@ -113,8 +143,26 @@ def diff_and_write (contents:, path:, diff: true, dry_run: false)
113
143
  FileUtils.rm(tmp_file)
114
144
  end
115
145
 
116
- def get_config
146
+ def pg_conn_string (db)
147
+ "host=#{db.host} port=#{db.port} dbname=#{db.dbname} user=#{db.user}#{db.password.size != 0 ? ' password='+ db.password : '' }"
148
+ end
117
149
 
150
+ def expand_template (template, pretty_json: false)
151
+ begin
152
+ contents = ERB.new(File.read(template), nil, '-').result()
153
+ if pretty_json
154
+ JSON.pretty_generate(JSON.parse(contents))
155
+ else
156
+ contents
157
+ end
158
+ rescue Exception => e
159
+ puts "Expansion of #{template} failed".yellow
160
+ puts e.message.red
161
+ exit
162
+ end
163
+ end
164
+
165
+ def get_config
118
166
  hostname = %x[hostname -s].strip
119
167
  @project = Dir.pwd
120
168
  @user_home = File.expand_path('~')
@@ -165,13 +213,16 @@ def get_config
165
213
  end
166
214
 
167
215
  #
168
- # Pre-cook the connection string
216
+ # Pre-cook the connection string # TODO remove this after Hydra goes live
169
217
  #
170
- dbname = conf['db']['dbname']
171
- dbuser = conf['db']['user']
172
- dbhost = conf['db']['host']
173
- dbpass = conf['db']['password'] || ''
174
- conf['db']['connection_string'] = "host=#{dbhost} dbname=#{dbname} user=#{dbuser}#{dbpass.size != 0 ? ' password='+ dbpass : '' }"
218
+ if conf['db']
219
+ dbname = conf['db']['dbname']
220
+ dbuser = conf['db']['user']
221
+ dbhost = conf['db']['host']
222
+ dbport = conf['db']['port'] || 5432
223
+ dbpass = conf['db']['password'] || ''
224
+ conf['db']['connection_string'] = "host=#{dbhost} port=#{dbport} dbname=#{dbname} user=#{dbuser}#{dbpass.size != 0 ? ' password='+ dbpass : '' }"
225
+ end
175
226
 
176
227
  #
177
228
  # Resolve project and user relative paths
@@ -187,7 +238,7 @@ def get_config
187
238
  conf.clean_keys!
188
239
 
189
240
  ap conf
190
- return JSON.parse(conf.to_json, object_class: OpenStruct), conf
241
+ return JSON.parse(conf.to_json, object_class: SpDataStruct), conf
191
242
  end
192
243
 
193
244
  desc 'Update project configuration: action=overwrite => update system,user,project; action => hotfix update project only; other no change (dryrun)'
@@ -338,7 +389,7 @@ task :configure, [ :action ] do |task, args|
338
389
  if m && m.size == 3
339
390
  key_l1 = m[1].gsub('-', '_')
340
391
  if conf[key_l1].nil? or conf[key_l1][m[2]].nil?
341
- puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}".yellow
392
+ puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}"
342
393
  next
343
394
  end
344
395
  end
@@ -347,7 +398,7 @@ task :configure, [ :action ] do |task, args|
347
398
  if m && m.size == 3
348
399
  key_l1 = m[1].gsub('-', '_')
349
400
  if conf[key_l1].nil?
350
- puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}".yellow
401
+ puts "Filtered #{m[1]} - #{m[2]} - #{dst_file}"
351
402
  next
352
403
  end
353
404
  end
@@ -361,9 +412,8 @@ task :configure, [ :action ] do |task, args|
361
412
  end
362
413
  end
363
414
 
364
- # puts "Expanding #{template}".red
365
415
  # Now expand the template
366
- file_contents = ERB.new(File.read(template), nil, '-').result()
416
+ file_contents = expand_template(template)
367
417
 
368
418
  if /.*(nginx-broker|nginx-epaper)\/conf\.d\/(.*)\.conf$/.match(dst_file)
369
419
  includes = file_contents.scan(/^\s*include\s+conf\.d\/(.*)\.location\;/)
@@ -393,7 +443,7 @@ task :configure, [ :action ] do |task, args|
393
443
  if used_locations.include? m[1]
394
444
  # Write text expanded configuration file
395
445
  create_directory(File.dirname dst_file)
396
- diff_and_write(contents: ERB.new(File.read(template), nil, '-').result(),
446
+ diff_and_write(contents: expand_template(template),
397
447
  path: dst_file,
398
448
  diff: diff_before_copy,
399
449
  dry_run: dry_run
@@ -427,7 +477,7 @@ task :configure, [ :action ] do |task, args|
427
477
  end
428
478
  create_directory "#{@config.prefix}/etc/#{@job_name}"
429
479
  create_directory "#{@config.prefix}/var/log/#{@job_name}"
430
- diff_and_write(contents: JSON.pretty_generate(JSON.parse(ERB.new(File.read(template), nil, '-').result())),
480
+ diff_and_write(contents: expand_template(template, pretty_json: true),
431
481
  path: "#{@config.prefix}/etc/#{@job_name}/conf.json",
432
482
  diff: diff_before_copy,
433
483
  dry_run: dry_run
@@ -442,7 +492,7 @@ task :configure, [ :action ] do |task, args|
442
492
  throw "Missing service file for #{@job_name}"
443
493
  end
444
494
 
445
- diff_and_write(contents: ERB.new(File.read(template)).result(),
495
+ diff_and_write(contents: expand_template(template),
446
496
  path: "#{@config.prefix}/lib/systemd/system/#{@job_name}@.service",
447
497
  diff: diff_before_copy,
448
498
  dry_run: dry_run
@@ -26,24 +26,26 @@ Gem::Specification.new do |spec|
26
26
  f.match(%r{^(test|spec|features)/})
27
27
  end
28
28
  spec.bindir = 'bin'
29
- spec.executables = 'queue-job'
29
+ spec.executables = ['queue-job', 'unique-file']
30
30
  spec.require_paths = ['lib']
31
- spec.add_dependency 'concurrent-ruby'
32
31
  spec.add_dependency 'os'
33
- spec.add_dependency 'sp-duh'
32
+ spec.add_dependency 'ffi'
34
33
  spec.add_dependency 'redis'
35
34
  spec.add_dependency 'backburner'
36
- spec.add_dependency 'pg'
37
- spec.add_dependency 'oauth2'
38
- spec.add_dependency 'oauth2-client'
39
- spec.add_dependency 'curb'
35
+ spec.add_dependency 'pg' unless RUBY_ENGINE == 'jruby'
36
+ spec.add_dependency 'pg_jruby' if RUBY_ENGINE == 'jruby'
37
+ spec.add_dependency 'curb' unless RUBY_ENGINE == 'jruby'
38
+ spec.add_dependency 'oauth2' unless RUBY_ENGINE == 'jruby'
39
+ spec.add_dependency 'oauth2-client' unless RUBY_ENGINE == 'jruby'
40
40
  spec.add_dependency 'mail'
41
- spec.add_dependency 'rails'
42
41
  spec.add_dependency 'json'
42
+ spec.add_dependency 'jwt'
43
43
  spec.add_dependency 'awesome_print'
44
44
  spec.add_dependency 'rollbar'
45
45
  spec.add_dependency 'roadie'
46
46
 
47
+ spec.add_development_dependency 'ruby-debug' if RUBY_ENGINE == 'jruby'
48
+ spec.add_development_dependency 'byebug' unless RUBY_ENGINE == 'jruby'
47
49
  spec.add_development_dependency 'bundler', '~> 1.14'
48
50
  spec.add_development_dependency 'rake', '~> 10.0'
49
51
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sp-job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eurico Inocencio, Vitor Pinho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-06 00:00:00.000000000 Z
11
+ date: 2018-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: concurrent-ruby
14
+ name: os
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: os
28
+ name: ffi
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sp-duh
42
+ name: redis
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: redis
56
+ name: backburner
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: backburner
70
+ name: pg
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: pg
84
+ name: curb
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: curb
126
+ name: mail
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -137,7 +137,7 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: mail
140
+ name: json
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -151,7 +151,7 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: rails
154
+ name: jwt
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
@@ -165,7 +165,7 @@ dependencies:
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
- name: json
168
+ name: awesome_print
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - ">="
@@ -179,7 +179,7 @@ dependencies:
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: awesome_print
182
+ name: rollbar
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -193,7 +193,7 @@ dependencies:
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  - !ruby/object:Gem::Dependency
196
- name: rollbar
196
+ name: roadie
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - ">="
@@ -207,13 +207,13 @@ dependencies:
207
207
  - !ruby/object:Gem::Version
208
208
  version: '0'
209
209
  - !ruby/object:Gem::Dependency
210
- name: roadie
210
+ name: byebug
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
213
  - - ">="
214
214
  - !ruby/object:Gem::Version
215
215
  version: '0'
216
- type: :runtime
216
+ type: :development
217
217
  prerelease: false
218
218
  version_requirements: !ruby/object:Gem::Requirement
219
219
  requirements:
@@ -267,6 +267,7 @@ email:
267
267
  - eurico.inocencio@gmail.com, vitor.pinho@servicepartner.pt
268
268
  executables:
269
269
  - queue-job
270
+ - unique-file
270
271
  extensions: []
271
272
  extra_rdoc_files: []
272
273
  files:
@@ -281,6 +282,7 @@ files:
281
282
  - bin/console
282
283
  - bin/queue-job
283
284
  - bin/setup
285
+ - bin/unique-file
284
286
  - lib/sp-job.rb
285
287
  - lib/sp/job.rb
286
288
  - lib/sp/job/back_burner.rb
@@ -288,12 +290,16 @@ files:
288
290
  - lib/sp/job/broker_http_client.rb
289
291
  - lib/sp/job/broker_oauth2_client.rb
290
292
  - lib/sp/job/common.rb
291
- - lib/sp/job/engine.rb
293
+ - lib/sp/job/job_db_adapter.rb
292
294
  - lib/sp/job/jsonapi_error.rb
295
+ - lib/sp/job/jwt.rb
296
+ - lib/sp/job/mail_queue.rb
293
297
  - lib/sp/job/pg_connection.rb
298
+ - lib/sp/job/unique_file.rb
294
299
  - lib/sp/job/uploaded_image_converter.rb
295
300
  - lib/sp/job/version.rb
296
301
  - lib/sp/job/worker.rb
302
+ - lib/sp/job/worker_thread.rb
297
303
  - lib/tasks/configure.rake
298
304
  - sp-job.gemspec
299
305
  homepage: https://github.com/vpfpinho/sp-job