boxxspring-workers 1.1.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28d891511f5e9885528269e401d43946f8eab6e9
4
- data.tar.gz: 7c4d7828f39a16ccecc075387792ca63eba21879
3
+ metadata.gz: 9bb58d38eac80a419803140a16f795f1d2295c0a
4
+ data.tar.gz: '04797acf3a9e42b0c16c1646fab97f754026658e'
5
5
  SHA512:
6
- metadata.gz: 5e6b1079cc3a9b932f4d44ae19b584ea77e414a2929e193e2e4ff3cb1330dca72bd59eff298222aad07456435da083af7b518a286ee3a728ddbd2ca342e0bc9b
7
- data.tar.gz: 94ba8c40b9ef4bbb5e2f04074acfefecd3f0376f2d8217fd6966f89ab36a6d6eb03fa3ca74b116bbb4c98baf679868e5cf5e34f8fc65eddff4e1129e19903c9c
6
+ metadata.gz: 4273c1fecb32544463181a2e49bdc9b2c2ce81c03a28776de56b8fd1b7548ba6c5bd88186056e31111a733ac9a4e9055bbeae75b5d45465442f53badf116a92f
7
+ data.tar.gz: 9f50f6eb3002870ef1e4268d07f7f010ca0b95d4af253d6b864c260ad3883817b4b9908164c6647fa5d47fb6a10d7f3b1dec7c47bf032710b8656a7fb6a49fe4
@@ -11,6 +11,7 @@ module Boxxspring
11
11
  # modules
12
12
 
13
13
  include Logging
14
+ include Metrics
14
15
 
15
16
  #------------------------------------------------------------------------
16
17
  # class attributes
@@ -62,28 +63,38 @@ module Boxxspring
62
63
  # operations
63
64
 
64
65
  def process
65
- messages = self.receive_messages() || []
66
- messages.each do | message |
67
- if message.present?
68
- payload = self.payload_from_message( message )
69
-
70
- if payload.present?
71
- begin
72
- result = self.process_payload( payload )
73
- # note: if an exception is raised the message will be deleted
74
- self.delete_message( message ) unless result == false
75
- rescue StandardError => error
66
+ metric_defaults dimensions: { worker_name: self.class.name,
67
+ environment: environment } do
68
+
69
+ messages = self.receive_messages() || []
70
+ messages.each do | message |
71
+ if message.present?
72
+ payload = self.payload_from_message( message )
73
+
74
+ if payload.present?
75
+ begin
76
+ metric :messages do
77
+ result = self.process_payload( payload )
78
+
79
+ # note: if an exception is raised the message will be deleted
80
+ self.delete_message( message ) unless result == false
81
+ end
82
+ rescue StandardError => error
83
+ metric :errors
84
+
85
+ self.logger.error(
86
+ "The #{ self.human_name } failed to process the payload."
87
+ )
88
+ self.logger.error( error.message )
89
+ self.logger.info( error.backtrace.join( "\n" ) )
90
+ end
91
+
92
+ else
93
+ self.delete_message( message )
76
94
  self.logger.error(
77
- "The #{ self.human_name } failed to process the payload."
95
+ "The #{ self.human_name } received an invalid payload."
78
96
  )
79
- self.logger.error( error.message )
80
- self.logger.info( error.backtrace.join( "\n" ) )
81
97
  end
82
- else
83
- self.delete_message( message )
84
- self.logger.error(
85
- "The #{ self.human_name } received an invalid payload."
86
- )
87
98
  end
88
99
  end
89
100
  end
@@ -1,30 +1,23 @@
1
1
  module Boxxspring
2
-
3
2
  module Worker
4
-
5
3
  module Logging
6
4
 
7
5
  PWD = Dir.pwd.freeze
8
6
 
9
7
  def logger
10
-
11
8
  @logger ||= begin
12
-
13
9
  workers_env = ENV[ 'WORKERS_ENV' ]
14
10
 
15
11
  if Worker.configuration.include?( 'logger' )
16
-
17
12
  logger = Worker.configuration.logger
18
-
19
13
  else
20
14
 
21
15
  if self.log_local? || workers_env == 'test'
22
-
23
16
  logger = Logger.new( STDOUT )
24
-
25
17
  else
26
18
 
27
19
  group_name = self.log_group_name
20
+
28
21
  raise 'A logging group is required. You may need to set LOG_GROUP.' \
29
22
  unless group_name.present?
30
23
 
@@ -49,15 +42,12 @@ module Boxxspring
49
42
  )
50
43
 
51
44
  end
52
-
53
-
54
45
  end
55
-
56
46
  logger.level = self.log_level
47
+
57
48
  logger
58
49
 
59
50
  end
60
-
61
51
  end
62
52
 
63
53
  protected; def log_group_name
@@ -87,9 +77,7 @@ module Boxxspring
87
77
  log_local = ENV[ 'LOG_LOCAL' ] || 'false'
88
78
  ( log_local.to_s =~ /^true$/i ) == 0
89
79
  end
90
-
80
+
91
81
  end
92
-
93
82
  end
94
-
95
83
  end
@@ -0,0 +1,13 @@
1
+ class CountMetricComputer < MetricComputer
2
+
3
+ def value
4
+ @value
5
+ end
6
+
7
+ def start
8
+ end
9
+
10
+ def stop
11
+ end
12
+
13
+ end
@@ -0,0 +1,42 @@
1
+ class MetricComputer
2
+
3
+ def name
4
+ @name ||= ""
5
+ end
6
+
7
+ def value
8
+ @value ||= 1
9
+ end
10
+
11
+ def unit
12
+ @unit ||= ""
13
+ end
14
+
15
+ def initialize( hash, defaults )
16
+ @name, @value, @unit = hash.values
17
+ @defaults = defaults
18
+ end
19
+
20
+ def to_json
21
+ {
22
+ metric_name: name.to_s.capitalize,
23
+ dimensions: normalize_dimensions_from_defaults( @defaults ),
24
+ value: value.to_i,
25
+ unit: unit.to_s.capitalize
26
+ }
27
+ end
28
+
29
+ private; def normalize_dimensions_from_defaults( defaults )
30
+ return nil if defaults[ :dimensions ].blank?
31
+
32
+ defaults[ :dimensions ].map do | key, value |
33
+ {
34
+ name: key.to_s.camelize,
35
+ value: value
36
+ }
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
@@ -0,0 +1,15 @@
1
+ class MicrosecondsMetricComputer < MetricComputer
2
+
3
+ def value
4
+ @value * 1000000
5
+ end
6
+
7
+ def start
8
+ @start_time = Time.now
9
+ end
10
+
11
+ def stop
12
+ @value = Time.now - @start_time
13
+ end
14
+
15
+ end
@@ -0,0 +1,7 @@
1
+ class MillisecondsMetricComputer < MicrosecondsMetricComputer
2
+
3
+ def value
4
+ super / 1000
5
+ end
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ class SecondsMetricComputer < MicrosecondsMetricComputer
2
+
3
+ def value
4
+ super / 1000000
5
+ end
6
+
7
+ end
@@ -0,0 +1,97 @@
1
+ require 'thread'
2
+
3
+ module Boxxspring
4
+
5
+ module Worker
6
+
7
+ module Metrics
8
+
9
+ METRICS_MUTEX = Mutex.new
10
+ METRICS_CLIENT = Aws::CloudWatch::Client.new
11
+ METRICS_UPLOAD_INTERVAL = 1
12
+
13
+ def initialize( *arguments )
14
+ super
15
+
16
+ @metrics = []
17
+ @metric_defaults = [ {} ]
18
+
19
+ Thread.new do
20
+ upload_metrics
21
+ end
22
+ end
23
+
24
+ def upload_metrics
25
+ loop do
26
+ unless @metrics.empty?
27
+ begin
28
+ metrics_payload = nil
29
+
30
+ METRICS_MUTEX.synchronize do
31
+ metrics_payload = @metrics
32
+ @metrics = []
33
+ end
34
+
35
+ METRICS_CLIENT.put_metric_data( {
36
+ namespace: 'Unimatrix/Worker',
37
+ metric_data: metrics_payload
38
+ } )
39
+
40
+ rescue Error => e
41
+ logger.error(
42
+ "An error has occured when making a request to the AWS " +
43
+ "Cloudwatch endpoint 'put_metric_data'."
44
+ )
45
+ end
46
+
47
+ sleep METRICS_UPLOAD_INTERVAL
48
+ end
49
+ end
50
+ end
51
+
52
+ def metric_defaults( defaults = {} )
53
+ previous_defaults = @metric_defaults.last
54
+ @metric_defaults.push( previous_defaults.merge( defaults ) )
55
+
56
+ yield
57
+ @metric_defaults.pop
58
+
59
+ end
60
+
61
+ def metric ( *arguments )
62
+ arguments = [ arguments ] unless arguments.first.is_a? Array
63
+ computers = arguments.map do | metric |
64
+ parsed_metric = parse_metric( metric )
65
+
66
+ computer_class =
67
+ "#{ parsed_metric[ :unit ].to_s.capitalize }MetricComputer".constantize
68
+ computer_class.new( parsed_metric, @metric_defaults.last )
69
+ end
70
+
71
+ if block_given?
72
+ computers.each( &:start )
73
+ yield
74
+ computers.each( &:stop )
75
+ end
76
+
77
+ METRICS_MUTEX.synchronize do
78
+ @metrics = @metrics.concat(
79
+ computers.map( &:to_json ).delete_if { | json | json.blank? }
80
+ )
81
+ end
82
+
83
+ end
84
+
85
+ private; def parse_metric ( unparsed_metrics )
86
+ name, data, unit = unparsed_metrics.first, 1, :count
87
+
88
+ data = unparsed_metrics[ 1 ] if unparsed_metrics[ 1 ].is_a? Integer
89
+ unit = unparsed_metrics[ 1 ] if unparsed_metrics[ 1 ].is_a? Symbol
90
+ unit = unparsed_metrics[ 2 ] unless unparsed_metrics[ 2 ].nil?
91
+
92
+ { name: name, data: data, unit: unit }
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -1,5 +1,5 @@
1
1
  module Boxxspring
2
2
  module Worker
3
- VERSION = '1.1.5'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -6,6 +6,12 @@ require 'lib/boxxspring/abstract'
6
6
  require 'lib/boxxspring/journal'
7
7
  require 'lib/boxxspring/worker'
8
8
  require 'lib/boxxspring/worker/logging'
9
+ require 'lib/boxxspring/worker/metrics'
10
+ require 'lib/boxxspring/worker/metrics/metric_computer'
11
+ require 'lib/boxxspring/worker/metrics/microseconds_metric_computer'
12
+ require 'lib/boxxspring/worker/metrics/count_metric_computer'
13
+ require 'lib/boxxspring/worker/metrics/seconds_metric_computer'
14
+ require 'lib/boxxspring/worker/metrics/milliseconds_metric_computer'
9
15
  require 'lib/boxxspring/worker/configuration'
10
16
  require 'lib/boxxspring/worker/base'
11
17
  require 'lib/boxxspring/worker/task_base'
@@ -14,4 +20,4 @@ require 'lib/boxxspring/synchronization'
14
20
  require 'lib/boxxspring/synchronization/operations'
15
21
  require 'lib/boxxspring/synchronization/orchestrator'
16
22
  require 'lib/boxxspring/synchronization/mutex'
17
- require 'lib/boxxspring/synchronization/variable'
23
+ require 'lib/boxxspring/synchronization/variable'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxxspring-workers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristoph Cichocki-Romanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cloudwatchlogger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: boxxspring
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,12 @@ files:
115
129
  - lib/boxxspring/worker/base.rb
116
130
  - lib/boxxspring/worker/configuration.rb
117
131
  - lib/boxxspring/worker/logging.rb
132
+ - lib/boxxspring/worker/metrics.rb
133
+ - lib/boxxspring/worker/metrics/count_metric_computer.rb
134
+ - lib/boxxspring/worker/metrics/metric_computer.rb
135
+ - lib/boxxspring/worker/metrics/microseconds_metric_computer.rb
136
+ - lib/boxxspring/worker/metrics/milliseconds_metric_computer.rb
137
+ - lib/boxxspring/worker/metrics/seconds_metric_computer.rb
118
138
  - lib/boxxspring/worker/task_base.rb
119
139
  - lib/tasks/workers.rake
120
140
  homepage: http://bedrocket.com
@@ -137,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
157
  version: '0'
138
158
  requirements: []
139
159
  rubyforge_project:
140
- rubygems_version: 2.4.8
160
+ rubygems_version: 2.6.12
141
161
  signing_key:
142
162
  specification_version: 4
143
163
  summary: Bedrocket Media Ventrures Boxxspring Worker framework.