funktor 0.2.17 → 0.2.18

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
  SHA256:
3
- metadata.gz: c7b7b575e4abcc04a0b771e3fb9afb912ee82015ed7aa6bcb17570fca0f392fa
4
- data.tar.gz: 98d8caf556c78960b73a1ee40831d4165b01836831d65eb17014239450a5dce7
3
+ metadata.gz: 6e146325cc89da410fb6af4397911f20d66d681eaf14c7bd2919d41c97ab6e62
4
+ data.tar.gz: 4da3a83da0c561ed6ca5e51c84d0fa608063f5fa3d70dedde9a57aed279b91a2
5
5
  SHA512:
6
- metadata.gz: 46b2e852ec64c6f4f5ea23422ec987e95941583f51fe8d76a254df36093c543a4b250c4de5f35abd32df1f9fd8db96df6655b397248697c134a208b8bc60d6f9
7
- data.tar.gz: a7231805102b4a7b05ce8eade0c35c4798779e6ca88171a294137bc20a334e32da47927abdb607d00f83d3585d7dec0d5706c2f6a6baab2e5cbf769b768e4532
6
+ metadata.gz: 4860f967c03a2cf365c211f5d511591a8778c8570492499603bb6e888fb85e7c3ee7def2906fdc09762c9f166b3e1a503571df5fe4175a1cfeb2b153b37a5ba4
7
+ data.tar.gz: 1f32d8f5d968a08c7c0bd36eba892309b8511414bfc0d9ff2cc082dbd38496f8bdfb259ac7046e3d6f06707f994b7a947648eb1a15996c1f481afe112dad9034
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- funktor (0.2.17)
4
+ funktor (0.2.18)
5
5
  activesupport
6
6
  aws-sdk-sqs (~> 1.37)
7
7
  thor
data/lib/funktor.rb CHANGED
@@ -3,16 +3,21 @@ require 'funktor/aws/sqs/event'
3
3
  require 'funktor/aws/sqs/record'
4
4
  require 'funktor/counter'
5
5
  require 'funktor/job'
6
+ require 'funktor/logger'
6
7
  require 'funktor/worker'
7
8
  require 'funktor/middleware_chain'
8
9
  require 'funktor/incoming_job_handler'
9
- require 'funktor/active_job_handler'
10
10
 
11
11
  require 'json'
12
12
 
13
13
  module Funktor
14
14
  class Error < StandardError; end
15
15
 
16
+ DEFAULT_OPTIONS = {
17
+ error_handlers: [],
18
+ log_level: Logger::DEBUG # Set a high log level during early, active development
19
+ }
20
+
16
21
  def self.configure_job_pusher
17
22
  yield self
18
23
  end
@@ -55,9 +60,41 @@ module Funktor
55
60
  def self.dump_json(object)
56
61
  JSON.generate(object)
57
62
  end
63
+
64
+ def self.options
65
+ @options ||= DEFAULT_OPTIONS.dup
66
+ end
67
+
68
+ def self.options=(opts)
69
+ @options = opts
70
+ end
71
+
72
+ # Register a proc to handle any error which occurs within the Funktor active job handler.
73
+ #
74
+ # Funktor.error_handlers << proc {|error, context| ErrorsAsAService.notify(error, context) }
75
+ #
76
+ # The default error handler logs errors to STDOUT
77
+ def self.error_handlers
78
+ options[:error_handlers]
79
+ end
80
+
81
+ def self.logger
82
+ @logger ||= Funktor::Logger.new($stdout, level: options[:log_level])
83
+ end
84
+
85
+ def self.logger=(logger)
86
+ if logger.nil?
87
+ self.logger.level = Logger::FATAL
88
+ return self.logger
89
+ end
90
+
91
+ @logger = logger
92
+ end
58
93
  end
59
94
 
60
95
  # TODO - Should we require this by default or let people opt in?
61
- # Is it a code smell that we need to include it at the bottom, after
96
+ # TODO - Is it a code smell that we need to include these at the bottom, after
62
97
  # the main Funktor module is defined?
63
98
  require 'funktor/middleware/metrics'
99
+ require 'funktor/error_handler'
100
+ require 'funktor/active_job_handler'
@@ -2,6 +2,7 @@ require 'aws-sdk-sqs'
2
2
 
3
3
  module Funktor
4
4
  class ActiveJobHandler
5
+ include Funktor::ErrorHandler
5
6
 
6
7
  def initialize
7
8
  @failed_counter = Funktor::Counter.new('failed')
@@ -28,8 +29,7 @@ module Funktor
28
29
  @processed_counter.incr(job)
29
30
  # rescue Funktor::Job::InvalidJsonError # TODO Make this work
30
31
  rescue Exception => e
31
- puts "Error during processing: #{$!}"
32
- puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
32
+ handle_error(e, job)
33
33
  @failed_counter.incr(job)
34
34
  attempt_retry_or_bail(job)
35
35
  end
@@ -0,0 +1,25 @@
1
+ module Funktor
2
+ module ErrorHandler
3
+ class Logger
4
+ def call(error, context)
5
+ Funktor.logger.warn(Funktor.dump_json(context)) unless context.empty?
6
+ Funktor.logger.warn("#{error.class.name}: #{error.message}")
7
+ Funktor.logger.warn(error.backtrace.join("\n")) unless error.backtrace.nil?
8
+ end
9
+
10
+ Funktor.error_handlers << Funktor::ErrorHandler::Logger.new
11
+ end
12
+
13
+ def handle_error(error, context = {})
14
+ Funktor.error_handlers.each do |handler|
15
+ begin
16
+ handler.call(error, context)
17
+ rescue => new_error
18
+ Funktor.logger.error "!!! ERROR HANDLER THREW AN ERROR !!!"
19
+ Funktor.logger.error new_error
20
+ Funktor.logger.error new_error.backtrace.join("\n") unless new_error.backtrace.nil?
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'logger'
2
+ module Funktor
3
+ class Logger < ::Logger
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Funktor
2
- VERSION = "0.2.17"
2
+ VERSION = "0.2.18"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: funktor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.2.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Green
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-02 00:00:00.000000000 Z
11
+ date: 2021-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs
@@ -137,9 +137,11 @@ files:
137
137
  - lib/funktor/cli/templates/package.json
138
138
  - lib/funktor/cli/templates/serverless.yml
139
139
  - lib/funktor/counter.rb
140
+ - lib/funktor/error_handler.rb
140
141
  - lib/funktor/fake_job_queue.rb
141
142
  - lib/funktor/incoming_job_handler.rb
142
143
  - lib/funktor/job.rb
144
+ - lib/funktor/logger.rb
143
145
  - lib/funktor/middleware/metrics.rb
144
146
  - lib/funktor/middleware_chain.rb
145
147
  - lib/funktor/testing.rb