sp-job 0.2.3 → 0.3.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/configure +40 -0
  4. data/lib/sp-job.rb +21 -2
  5. data/lib/sp/job/back_burner.rb +350 -68
  6. data/lib/sp/job/broker.rb +18 -16
  7. data/lib/sp/job/broker_http_client.rb +80 -20
  8. data/lib/sp/job/broker_oauth2_client.rb +12 -4
  9. data/lib/sp/job/common.rb +876 -62
  10. data/lib/sp/job/configure/configure.rb +640 -0
  11. data/lib/sp/job/curl_http_client.rb +100 -0
  12. data/lib/sp/job/easy_http_client.rb +94 -0
  13. data/lib/sp/job/http_client.rb +51 -0
  14. data/lib/sp/job/job_db_adapter.rb +38 -36
  15. data/lib/sp/job/jsonapi_error.rb +31 -74
  16. data/lib/sp/job/jwt.rb +55 -5
  17. data/lib/sp/job/mail_queue.rb +9 -2
  18. data/lib/sp/job/manticore_http_client.rb +94 -0
  19. data/lib/sp/job/pg_connection.rb +90 -10
  20. data/lib/sp/job/query_params.rb +45 -0
  21. data/lib/sp/job/rfc822.rb +13 -0
  22. data/lib/sp/job/session.rb +239 -0
  23. data/lib/sp/job/unique_file.rb +37 -1
  24. data/lib/sp/job/uploaded_image_converter.rb +27 -19
  25. data/lib/sp/job/worker.rb +51 -1
  26. data/lib/sp/job/worker_thread.rb +22 -7
  27. data/lib/sp/jsonapi.rb +24 -0
  28. data/lib/sp/jsonapi/adapters/base.rb +177 -0
  29. data/lib/sp/jsonapi/adapters/db.rb +26 -0
  30. data/lib/sp/jsonapi/adapters/raw_db.rb +96 -0
  31. data/lib/sp/jsonapi/exceptions.rb +54 -0
  32. data/lib/sp/jsonapi/model/base.rb +31 -0
  33. data/lib/sp/jsonapi/model/concerns/attributes.rb +91 -0
  34. data/lib/sp/jsonapi/model/concerns/model.rb +39 -0
  35. data/lib/sp/jsonapi/model/concerns/persistence.rb +212 -0
  36. data/lib/sp/jsonapi/model/concerns/serialization.rb +57 -0
  37. data/lib/sp/jsonapi/parameters.rb +54 -0
  38. data/lib/sp/jsonapi/service.rb +96 -0
  39. data/lib/tasks/configure.rake +2 -496
  40. data/sp-job.gemspec +3 -2
  41. metadata +24 -2
@@ -63,10 +63,46 @@ module SP
63
63
  return nil if r != 0
64
64
  return nil if ptr.null?
65
65
 
66
- rv = ptr.read_string
66
+ rv = ptr.read_string.force_encoding('UTF-8')
67
67
 
68
68
  return rv
69
69
  end
70
+
71
+ #
72
+ # Creates a uniquely named file inside the specified folder. The created file is empty
73
+ #
74
+ # @param folder Folder where the file will be created
75
+ # @param name
76
+ # @param extension
77
+ # @return Absolute file path
78
+ #
79
+ def self.create_n (folder:, name:, extension:)
80
+ FileUtils.mkdir_p(folder) if !Dir.exist?(folder)
81
+ if nil != name
82
+ fd = ::SP::Job::Unique::File.mkstemps("#{folder}/#{name}.XXXXXX.#{extension}", extension.length + 1)
83
+ else
84
+ fd = ::SP::Job::Unique::File.mkstemps("#{folder}/XXXXXX.#{extension}", extension.length + 1)
85
+ end
86
+ return nil if fd < 0
87
+
88
+ ptr = FFI::MemoryPointer.new(:char, 8192) # Assumes max path is less that this
89
+ if OS.mac?
90
+ r = ::SP::Job::Unique::File.fcntl(fd, 50, ptr) # 50 is F_GETPATH in OSX
91
+ else
92
+ r = ::SP::Job::Unique::File.readlink("/proc/self/fd/#{fd}", ptr, 8192)
93
+ if r > 0 && r < 8192
94
+ r = 0
95
+ end
96
+ end
97
+ ::SP::Job::Unique::File.close(fd)
98
+ return nil if r != 0
99
+ return nil if ptr.null?
100
+
101
+ rv = ptr.read_string.force_encoding('UTF-8')
102
+
103
+ return rv
104
+ end
105
+
70
106
  end
71
107
  end
72
108
  end
@@ -23,24 +23,23 @@
23
23
  #
24
24
  # How to use this to implement a customized image conversion
25
25
  #
26
- # require 'rmagick'
27
26
  # require 'sp-job'
28
27
  # require 'sp/job/back_burner'
29
28
  # require 'sp/job/uploaded_image_converter'
30
- #
29
+ #
31
30
  # class CLASSNAME < ::SP::Job::UploadedImageConverter
32
- #
31
+ #
33
32
  # def self.perform(job)
34
- #
33
+ #
35
34
  # ... Your code before the image conversion ...
36
35
  #
37
36
  # SP::Job::UploadedImageConverter.perform(job)
38
- #
37
+ #
39
38
  # ... your code after the conversion ...
40
39
  #
41
40
  # end
42
41
  # end
43
- #
42
+ #
44
43
  # Backburner.work
45
44
  #
46
45
 
@@ -51,24 +50,34 @@ module SP
51
50
 
52
51
  def self.perform (job)
53
52
 
54
- raise_error(message: 'i18n_entity_id_must_be_defined') if job[:entity_id].nil? || job[:entity_id].to_i == 0
53
+ raise_error(message: 'i18n_entity_id_must_be_defined') if job[:to_entity_id].nil? || job[:to_entity_id].to_i == 0
55
54
 
56
55
  step = 100 / (job[:copies].size + 1)
57
56
  progress = step
58
- original = File.join(config[:paths][:temporary_uploads], job[:original])
59
- destination = File.join(config[:paths][:uploads_storage], job[:entity], id_to_path(job[:entity_id]), job[:folder])
57
+ original = File.join(config[:scp_config][:temp_uploads], job[:original])
58
+ destination = File.join(config[:scp_config][:path], job[:entity], id_to_path(job[:to_entity_id]), job[:folder])
59
+
60
+ if config[:scp_config][:local]
61
+ ssh = ''
62
+ FileUtils::mkdir_p destination
63
+ else
64
+ ssh = "ssh #{config[:scp_config][:server]} "
65
+ %x[#{ssh}mkdir -p #{destination}]
66
+ unless $?.success?
67
+ raise_error(message: 'i18n_internal_error', info: "unable to create remote directory")
68
+ end
69
+ end
60
70
 
61
71
  #
62
72
  # Check the original image, check format and limits
63
- #
64
- FileUtils::mkdir_p destination
65
- update_progress(progress: progress, message: 'i18n_reading_original_$image', image: job[:original])
66
- img_info = %x[identify #{original}]
73
+ #
74
+ update_progress(progress: progress, message: 'i18n_reading_original_$image', image: job[:original_file_path] || job[:original])
75
+ img_info = %x[#{ssh}identify #{original}]
67
76
  m = %r[.*\.ul\s(\w+)\s(\d+)x(\d+)\s.*].match img_info
68
77
  if $?.success? == false
69
78
  return report_error(message: 'i18n_invalid_image', info: "Image #{original} can't be identified '#{img_info}'")
70
79
  end
71
- if m.nil? || m.size != 4
80
+ if m.nil? || m.size != 4
72
81
  return report_error(message: 'i18n_invalid_image', info: "Image #{original} can't be identified '#{img_info}'")
73
82
  end
74
83
  unless config[:options][:formats].include? m[1]
@@ -87,7 +96,7 @@ module SP
87
96
  # Iterate the copies array
88
97
  #
89
98
  job[:copies].each do |copy|
90
- %x[convert #{original} -geometry #{copy[:geometry]} #{File.join(destination, copy[:name])}]
99
+ %x[#{ssh}convert #{original} -geometry #{copy[:geometry]} #{File.join(destination, copy[:name])}]
91
100
  unless $?.success?
92
101
  raise_error(message: 'i18n_internal_error', info: "convert failed to scale #{original} to #{copy[:geometry]}")
93
102
  end
@@ -97,11 +106,10 @@ module SP
97
106
  barrier = false
98
107
  end
99
108
 
109
+ #
100
110
  # Closing arguments, all done
101
- send_response(message: 'i18n_image_conversion_complete', link: File.join('/',job[:entity], id_to_path(job[:entity_id]), job[:folder], 'logo_template.png'))
102
-
103
- # Remove original file
104
- FileUtils::rm_f(original) if config[:options][:delete_originals]
111
+ #
112
+ return { hostname: config[:urls][:upload_public], path: File.join('/',job[:entity], id_to_path(job[:to_entity_id]), job[:folder])}
105
113
 
106
114
  end
107
115
 
data/lib/sp/job/worker.rb CHANGED
@@ -26,7 +26,15 @@ module SP
26
26
  def start
27
27
  prepare
28
28
  loop do
29
- work_one_job
29
+ begin
30
+ work_one_job(connection)
31
+ rescue Beaneater::NotFoundError => bnfe
32
+ # Do nothing if try to delete the task and it´s not found
33
+ rescue Beaneater::DeadlineSoonError => dse
34
+ # By default there is nothing we can do to speed up
35
+ rescue Backburner::Job::JobTimeout => jte
36
+ # What to do?
37
+ end
30
38
  unless connection.connected?
31
39
  log_error "Connection to beanstalk closed, exiting now"
32
40
  Kernel.exit
@@ -34,6 +42,48 @@ module SP
34
42
  end
35
43
  end
36
44
 
45
+ # Performs a job by reserving a job from beanstalk and processing it
46
+ #
47
+ # @example
48
+ # @worker.work_one_job
49
+ # @raise [Beaneater::NotConnected] If beanstalk fails to connect multiple times.
50
+ def work_one_job(conn = connection)
51
+ begin
52
+ job = reserve_job(conn)
53
+ rescue Beaneater::TimedOutError => e
54
+ return
55
+ end
56
+
57
+ self.log_job_begin(job.name, job.args)
58
+ job.process
59
+ self.log_job_end(job.name)
60
+
61
+ rescue Backburner::Job::JobFormatInvalid => e
62
+ self.log_error self.exception_message(e)
63
+ rescue => e # Error occurred processing job
64
+ self.log_error self.exception_message(e)
65
+
66
+ unless job
67
+ self.log_error "Error occurred before we were able to assign a job. Giving up without retrying!"
68
+ return
69
+ end
70
+
71
+ # NB: There's a slight chance here that the connection to beanstalkd has
72
+ # gone down between the time we reserved / processed the job and here.
73
+ num_retries = job.stats.nil? ? 0 : job.stats.releases
74
+ retry_status = "failed: attempt #{num_retries+1} of #{queue_config.max_job_retries+1}"
75
+ if num_retries < queue_config.max_job_retries # retry again
76
+ delay = queue_config.retry_delay_proc.call(queue_config.retry_delay, num_retries) rescue queue_config.retry_delay
77
+ job.retry(num_retries + 1, delay)
78
+ self.log_job_end(job.name, "#{retry_status}, retrying in #{delay}s") if job_started_at
79
+ else # retries failed, bury
80
+ job.bury
81
+ self.log_job_end(job.name, "#{retry_status}, burying") if job_started_at
82
+ end
83
+
84
+ handle_error(e, job.name, job.args, job)
85
+ end
86
+
37
87
  end # Worker
38
88
  end # Module Job
39
89
  end # Module SP
@@ -33,25 +33,40 @@ module SP
33
33
  #
34
34
  def prepare
35
35
  log_info "Working #{tube_names.size} queues: [ #{tube_names.join(', ')} ]"
36
- $config[:options][:threads].times do
36
+ $config[:options][:threads].times do
37
37
  connection = new_connection.tap{ |conn| conn.tubes.watch!(*tube_names) }
38
38
  connection.on_reconnect = lambda { |conn| conn.tubes.watch!(*tube_names) }
39
39
 
40
40
  $threads << Thread.new {
41
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)
42
+ logger.info "Thread for #{tube_names.join(',')} #{Thread.current}"
43
+ loop do
44
+ begin
45
+ work_one_job(connection)
46
+ rescue Beaneater::NotFoundError => bnfe
47
+ # Do nothing if try to delete the task and it´s not found
48
+ rescue Beaneater::DeadlineSoonError => dse
49
+ # By default there is nothing we can do to speed up
50
+ rescue Backburner::Job::JobTimeout => jte
51
+ # What to do?
52
+ logger.info "Thread #{Thread.current} job timeout".yellow
53
+ Rollbar.warning(jte)
54
+ rescue => e
55
+ Rollbar.error(e)
56
+ end
57
+
45
58
  unless connection.connected?
46
59
  log_error "Connection to beanstalk closed, exiting now"
47
60
  Kernel.exit
48
61
  end
49
- end
62
+ end
63
+ logger.info "Thread #{Thread.current} exiting".yellow
64
+ $threads.delete(Thread.current)
65
+ $thread_data.delete Thread.current
50
66
  }
51
67
  end
52
- #end
53
68
  end
54
69
 
55
70
  end # Worker
56
71
  end # Module Job
57
- end # Module SP
72
+ end # Module SP
data/lib/sp/jsonapi.rb ADDED
@@ -0,0 +1,24 @@
1
+ #
2
+ # Copyright (c) 2011-2017 Cloudware S.A. All rights reserved.
3
+ #
4
+ # This file is part of sp-job.
5
+ #
6
+ # sp-job is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sp-job is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sp-job. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ module SP
21
+ module JSONAPI
22
+ MODULE_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
23
+ end
24
+ end
@@ -0,0 +1,177 @@
1
+ module SP
2
+ module JSONAPI
3
+ module Adapters
4
+
5
+ class Base
6
+
7
+ def service ; @service ; end
8
+
9
+ def initialize(service)
10
+ @service = service
11
+ end
12
+
13
+ def get(path, params = {})
14
+ request('GET', path, params)
15
+ end
16
+ def post(path, params = {})
17
+ request('POST', path, params)
18
+ end
19
+ def patch(path, params = {})
20
+ request('PATCH', path, params)
21
+ end
22
+ def delete(path)
23
+ request('DELETE', path, nil)
24
+ end
25
+
26
+ def get!(path, params = {})
27
+ request!('GET', path, params)
28
+ end
29
+ def post!(path, params = {})
30
+ request!('POST', path, params)
31
+ end
32
+ def patch!(path, params = {})
33
+ request!('PATCH', path, params)
34
+ end
35
+ def delete!(path)
36
+ request!('DELETE', path, nil)
37
+ end
38
+
39
+ def get_explicit!(exp_subentity_schema, exp_subentity_prefix, path, params = {})
40
+ explicit_request!(exp_subentity_schema, exp_subentity_prefix, 'GET', path, params)
41
+ end
42
+ def post_explicit!(exp_subentity_schema, exp_subentity_prefix, path, params = {})
43
+ explicit_request!(exp_subentity_schema, exp_subentity_prefix, 'POST', path, params)
44
+ end
45
+ def patch_explicit!(exp_subentity_schema, exp_subentity_prefix, path, params = {})
46
+ explicit_request!(exp_subentity_schema, exp_subentity_prefix, 'PATCH', path, params)
47
+ end
48
+ def delete_explicit!(exp_subentity_schema, exp_subentity_prefix, path)
49
+ explicit_request!(exp_subentity_schema, exp_subentity_prefix, 'DELETE', path, nil)
50
+ end
51
+
52
+ def get_specific_service!(path, params, service_params)
53
+ specific_service_do_request!('GET', path, params, service_params)
54
+ end
55
+
56
+ alias_method :put, :patch
57
+ alias_method :put!, :patch!
58
+ alias_method :put_explicit!, :patch_explicit!
59
+
60
+ def unwrap_request
61
+ unwrap_response(yield)
62
+ end
63
+
64
+ # do_request MUST be implemented by each specialized adapter, and returns a tuple: the request status and a JSONAPI string or hash with the result
65
+ def do_request(method, path, params) ; ; end
66
+ def explicit_do_request(exp_subentity_schema, exp_subentity_prefix, method, path, params) ; ; end
67
+
68
+ def request(method, path, params)
69
+ # As it is now, this method is EXACTLY the same as request!()
70
+ # And it cannot be reverted without affecting lots of changes already made in the app's controllers.
71
+ # TODO: end it, or end the !() version
72
+ # begin
73
+ unwrap_request do
74
+ do_request(method, path, params)
75
+ end
76
+ # THIS CAN'T BE DONE, because the same method cannot return both a single result (in case there is NOT an error) and a pair (in case there IS an error)
77
+ # rescue SP::JSONAPI::Exceptions::GenericModelError => e
78
+ # [
79
+ # e.status,
80
+ # e.result
81
+ # ]
82
+ # rescue Exception => e
83
+ # [
84
+ # SP::JSONAPI::Status::ERROR,
85
+ # get_error_response(path, e)
86
+ # ]
87
+ # end
88
+ end
89
+
90
+ def request!(method, path, params)
91
+ unwrap_request do
92
+ do_request(method, path, params)
93
+ end
94
+ end
95
+
96
+ def explicit_request!(exp_subentity_schema, exp_subentity_prefix, method, path, params)
97
+ unwrap_request do
98
+ explicit_do_request(exp_subentity_schema, exp_subentity_prefix, method, path, params)
99
+ end
100
+ end
101
+
102
+ def specific_service_do_request!(method, path, params, service_params)
103
+ unwrap_request do
104
+ specific_service_do_request(method, path, params, service_params)
105
+ end
106
+ end
107
+
108
+ protected
109
+
110
+ def url(path) ; File.join(service.url, path) ; end
111
+
112
+ def url_with_params_for_query(path, params)
113
+ query = params_for_query(params)
114
+ query_url = url(path)
115
+ query.blank? ? query_url : query_url + (query_url.include?('?') ? '&' : '?') + query
116
+ end
117
+
118
+ def params_for_query(params)
119
+ query = ""
120
+ if !params.blank?
121
+ case
122
+ when params.is_a?(Array)
123
+ # query = params.join('&')
124
+ query = params.map{ |v| URI.encode(URI.encode(v).gsub("'","''"), "&") }.join('&')
125
+ when params.is_a?(Hash)
126
+ query = params.map do |k,v|
127
+ if v.is_a?(String)
128
+ "#{k}=\"#{URI.encode(URI.encode(v).gsub("'","''"), "&")}\""
129
+ else
130
+ "#{k}=#{v}"
131
+ end
132
+ end.join('&')
133
+ else
134
+ query = params.to_s
135
+ end
136
+ end
137
+ query
138
+ end
139
+
140
+ def params_for_body(params)
141
+ params.blank? ? '' : params.to_json.gsub("'","''")
142
+ end
143
+
144
+ # unwrap_response SHOULD be implemented by each specialized adapter, and returns the request result as a JSONAPI string or hash and raises an exception if there was an error
145
+ def unwrap_response(response)
146
+ # As the method request() is EXACTLY the same as request!(), and it cannot be reverted without affecting lots of changes already made in the app's controllers...
147
+ # Allow for response being both a [ status, result ] pair (as of old) OR a single result (as of now)
148
+ if response.is_a?(Array)
149
+ status = response[0].to_i
150
+ result = response[1]
151
+ result
152
+ else
153
+ response
154
+ end
155
+ end
156
+
157
+ def error_response(path, error)
158
+ {
159
+ errors: [
160
+ {
161
+ status: "#{SP::JSONAPI::Status::ERROR}",
162
+ code: error.message
163
+ }
164
+ ],
165
+ links: { self: url(path) },
166
+ jsonapi: { version: SP::JSONAPI::VERSION }
167
+ }
168
+ end
169
+
170
+ # get_error_response MUST be implemented by each specialized adapter, and returns a JSONAPI error result as a string or hash
171
+ def get_error_response(path, error) ; ; end
172
+
173
+ end
174
+
175
+ end
176
+ end
177
+ end