fanforce-workers 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjhhOThmOTJmNDIyYjJkYjk4NDUzODM5Nzc2MTM2M2MzNDU5ZDFhNg==
4
+ YWFiMzEzODAwOTE3MmJiYjI1YjFlNzA2NzI2Mzc3Y2JmNDJhMDI2OQ==
5
5
  data.tar.gz: !binary |-
6
- MmY0YzA0NmQ1MDE5Nzg0OGUzM2VkMjg2Mzg3YmRiMDE3M2IzM2I2YQ==
6
+ YzU4MGJhOWE1ZWYwOGIyZGJjN2Y1NmM5YzBjYTQzYjFhYjAxYTYxNg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MGFkMDdlZmUwZDExNWFjOGQ2ZmViM2Y1YTVhOTA4Njk4NmY0YzIzZmUwOWNi
10
- ZDI3MzI5Y2FhYTlhOTlhNmQ1MGE3Zjg2ZTY2OTBkMjk0MzVkYjQ0N2QwMmI1
11
- ODIxYmJlZGI2MWQzNTc5ZTc4ZTMxZTM2YmJiZWI4OTUyM2VjZTQ=
9
+ NDA2NDkwZTYwOGZjZTQxNzBlZDI3ZjE5MGIwMDgzNDZiMDU4YjRjYjBiNzM2
10
+ YjFmMTQ5OTZjYmZhYzU5OTczNWQ1NTYxMDY1ODFiNTY3YTYxMTMxYjQ0YjM1
11
+ MzYzODQzY2Y5ZGIwMjAzYzJhZWJlZTNhNTMxM2JiNGI2NDRlYzQ=
12
12
  data.tar.gz: !binary |-
13
- NjE3NWFhYWIzYTFiOTFjOTYzYWE0Y2I2ZDMwODAwMjgzOGVlY2NiYTBiNDMz
14
- MDlhNDliNDIxNmQ2NTlkMTkxY2JlYTg1OTA2NWM1YjRmNDA4YTIwMjk2NzMy
15
- ZGQ0YTBkYjIwNTA1NTU0MTE4N2MwY2QxNDdjNzY4MGEwYzljODQ=
13
+ MTZiOTIwOGZjOTAxNDg5MzEwYTg4ODVlY2JhMTZhMDA0YWNiMWNmNDBlNDNk
14
+ ZDY3NjQ5OTg0ZGIzNzI2MWQ0OGY2N2YxMThkMDFkYmRhMGVmMDcwYmI3NzBi
15
+ YTcyMGRiNGVkYTAxMGFhYjZkMDI1MDIzNWFiYTYyMTgyYjliZTQ=
@@ -17,7 +17,9 @@ Gem::Specification.new do |gem|
17
17
  gem.require_paths = ['lib']
18
18
 
19
19
  gem.add_runtime_dependency 'iron_mq', '~> 4'
20
- gem.add_runtime_dependency 'typhoeus'
20
+ gem.add_runtime_dependency 'iron_cache', '~> 1.4'
21
+ gem.add_runtime_dependency 'uuidtools', '~> 2.1'
22
+ gem.add_runtime_dependency 'typhoeus', '~> 0.6'
21
23
  gem.add_runtime_dependency 'activesupport', '~> 3.2'
22
24
  gem.add_runtime_dependency 'fanforce-api', '~> 0.9'
23
25
  end
@@ -1,5 +1,5 @@
1
1
  class Fanforce
2
2
  class Workers
3
- VERSION = '0.5.5'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
@@ -4,38 +4,79 @@ class Fanforce::Workers
4
4
  @opts = opts
5
5
  end
6
6
 
7
- def iron
8
- require_relative 'iron'
9
- @iron ||= Fanforce::Workers::Iron.new(@opts)
7
+ def iron_mq
8
+ require 'iron_mq'
9
+ @iron_mq ||= IronMQ::Client.new(:token => @opts[:token] || ENV['IRON_TOKEN'], :project_id => @opts[:project_id] || ENV['IRON_PROJECT_ID'])
10
10
  end
11
11
 
12
- def enqueue(queue_id, payload)
13
- raise 'Payload being sent to the queue must be a Hash' if !payload.is_a?(Hash)
14
- iron.enqueue queue_id, params: payload, retries: 0
12
+ def iron_cache
13
+ require 'iron_cache'
14
+ @iron_cache ||= IronCache::Client.new(:token => @opts[:token] || ENV['IRON_TOKEN'], :project_id => @opts[:project_id] || ENV['IRON_PROJECT_ID'])
15
15
  end
16
16
 
17
- def retry_error(queue_id, msg_id)
18
- msg = iron.client.queue(queue_id + '-ERRORS').peek(msg_id)
19
- data = MultiJson.load(msg.body, :symbolize_keys => true)
20
- iron.enqueue queue_id, params: data[:params], retries: data[:retries] + 1
21
- msg.delete
17
+ def enqueue(queue_id, params, retries=0)
18
+ raise 'Params being sent to the queue must be a Hash' if !params.is_a?(Hash)
19
+ iron_mq.queue(queue_id).post({params: params, retries: retries}.to_json)
22
20
  end
23
21
 
24
- ##########################################################################################
22
+ def add_error(queue_id, error)
23
+ require 'uuidtools'
24
+ details_id = UUIDTools::UUID.random_create
25
+ iron_cache.queue("#{queue_id}-ERRORS").put(details_id, error.to_json)
26
+ iron_mq.queue("#{queue_id}-ERRORS").post({
27
+ details_id: details_id,
28
+ exception: truncate(error[:exception]),
29
+ message: truncate(error[:message].to_s),
30
+ params: truncate(error[:message].to_json),
31
+ errored_at: error[:errored_at],
32
+ retries: error[:retries],
33
+ env_vars: truncate(error[:env_vars].to_json),
34
+ curl_command: truncate(error[:curl_command].to_s)
35
+ }.to_json)
36
+ end
37
+
38
+ def delete_error(queue_id, msg_id, details_id)
39
+ iron_mq.queue("#{queue_id}-ERRORS").delete(msg_id)
40
+ iron_cache.queue("#{queue_id}-ERRORS").delete(details_id)
41
+ end
42
+
43
+ def error_details(queue_id, details_id)
44
+ cache = iron_cache.queue("#{queue_id}-ERRORS").get(details_id)
45
+ MultiJson.load(cache.body, :symbolize_keys => true)
46
+ end
25
47
 
26
- def self.add_error(queue_id, payload)
27
- self.new.iron.enqueue("#{queue_id}-ERRORS", payload)
48
+ def retry_error(queue_id, msg_id, details_id)
49
+ cache = iron_cache.queue("#{queue_id}-ERRORS").get(details_id)
50
+ cache_data = MultiJson.load(cache.body, :symbolize_keys => true)
51
+ enqueue(queue_id, cache_data[:params], cache_data[:retries] + 1)
52
+
53
+ cache.delete and iron_mq.queue("#{queue_id}-ERRORS").delete(msg_id)
54
+ end
55
+
56
+ def truncate(text, length=130, truncate_string='...')
57
+ if text
58
+ l = length - truncate_string.chars.length
59
+ chars = text.chars
60
+ (chars.length > length ? chars[0...l] + truncate_string : text).to_s
61
+ end
28
62
  end
29
63
 
64
+ ##########################################################################################
65
+
30
66
  def self.enqueue(queue_id, payload)
31
67
  self.new.enqueue(queue_id, payload)
32
68
  end
33
69
 
70
+ def self.add_error(queue_id, error)
71
+ self.new.add_error(queue_id, error)
72
+ end
73
+
34
74
  ##########################################################################################
35
75
 
36
76
  def self.run(worker_data, &code_block)
37
77
  require '.pluginenv'
38
78
  require 'iron_mq'
79
+ require 'iron_cache'
39
80
  require 'fanforce/api'
40
81
 
41
82
  run_validations
@@ -60,17 +101,17 @@ class Fanforce::Workers
60
101
  puts 'MESSAGE IS NIL'
61
102
  return
62
103
  end
63
- task_data[:last_error] = {
64
- exception: e.class.name,
65
- message: e.message,
66
- backtrace: e.backtrace,
67
- raised_at: Time.now
68
- }
69
- task_data[:last_error][:curl_command] = e.curl_command if e.respond_to?(:curl_command)
70
- task_data[:env_vars] = worker_data['env_vars']
71
-
72
- puts "ADDING TO ERROR QUEUE: #{task_data.to_json}"
73
- self.add_error worker_data['worker_id'], task_data
104
+ error = task_data.merge(
105
+ exception: e.class.name,
106
+ message: e.message,
107
+ backtrace: e.backtrace,
108
+ errored_at: Time.now,
109
+ env_vars: worker_data['env_vars']
110
+ )
111
+ error[:curl_command] = e.curl_command if e.respond_to?(:curl_command)
112
+
113
+ puts "ADDING TO ERROR CACHE: #{error.to_json}"
114
+ self.add_error worker_data['worker_id'], error
74
115
  msg.delete
75
116
  puts 'DELETED MESSAGE'
76
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanforce-workers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Clark
@@ -24,20 +24,48 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: iron_cache
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: uuidtools
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: typhoeus
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
- - - ! '>='
59
+ - - ~>
32
60
  - !ruby/object:Gem::Version
33
- version: '0'
61
+ version: '0.6'
34
62
  type: :runtime
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - ! '>='
66
+ - - ~>
39
67
  - !ruby/object:Gem::Version
40
- version: '0'
68
+ version: '0.6'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: activesupport
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -79,7 +107,6 @@ files:
79
107
  - Rakefile
80
108
  - fanforce-workers.gemspec
81
109
  - lib/fanforce/workers.rb
82
- - lib/fanforce/workers/iron.rb
83
110
  - lib/fanforce/workers/version.rb
84
111
  - lib/fanforce/workers/workers.rb
85
112
  homepage: http://github.com/fanforce/fanforce-workers
@@ -1,17 +0,0 @@
1
- require 'iron_mq'
2
-
3
- class Fanforce::Workers::Iron
4
-
5
- def initialize(opts={})
6
- @mq_client = IronMQ::Client.new(:token => opts[:token] || ENV['IRON_TOKEN'], :project_id => opts[:project_id] || ENV['IRON_PROJECT_ID'])
7
- end
8
-
9
- def enqueue(queue_id, payload)
10
- @mq_client.queue(queue_id).post(payload.to_json)
11
- end
12
-
13
- def client
14
- @mq_client
15
- end
16
-
17
- end