diff --git a/lib/funktor.rb b/lib/funktor.rb index 532f99a..bd5384d 100644 --- a/lib/funktor.rb +++ b/lib/funktor.rb @@ -1,6 +1,7 @@ require "funktor/version" require 'funktor/aws/sqs/event' require 'funktor/aws/sqs/record' +require 'funktor/counter' require 'funktor/job' require 'funktor/worker' require 'funktor/middleware_chain' diff --git a/lib/funktor/active_job_handler.rb b/lib/funktor/active_job_handler.rb index 6256c7f..4aee50e 100644 --- a/lib/funktor/active_job_handler.rb +++ b/lib/funktor/active_job_handler.rb @@ -3,6 +3,11 @@ module Funktor class ActiveJobHandler + def initialize + @failed_counter = Funktor::Counter.new('failed') + @processed_counter = Funktor::Counter.new('processed') + end + def call(event:, context:) event = Funktor::Aws::Sqs::Event.new(event) puts "event.jobs.count = #{event.jobs.count}" @@ -20,10 +25,12 @@ def dispatch(job) Funktor.active_job_handler_middleware.invoke(job) do job.execute end + @processed_counter.incr(job) # rescue Funktor::Job::InvalidJsonError # TODO Make this work rescue Exception => e puts "Error during processing: #{$!}" puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}" + @failed_counter.incr(job) attempt_retry_or_bail(job) end end diff --git a/lib/funktor/counter.rb b/lib/funktor/counter.rb new file mode 100644 index 0000000..5d13cce --- /dev/null +++ b/lib/funktor/counter.rb @@ -0,0 +1,42 @@ +module Funktor + class Counter + attr_accessor :dimension + + def initialize(dimension) + @dimension = dimension + end + + def incr(job) + put_metric_to_stdout(job) + end + + def put_metric_to_stdout(job) + puts Funktor.dump_json(metric_hash(job)) + end + + def metric_hash(job) + { + "_aws": { + "Timestamp": Time.now.strftime('%s%3N').to_i, + "CloudWatchMetrics": [ + { + "Namespace": ENV['FUNKTOR_APP_NAME'], + "Dimensions": [["WorkerClassName"]], + "Metrics": [ # CPU, Memory, Duration, etc... + { + "Name": dimension, + "Unit": "Count" + } + ] + } + ] + }, + "WorkerClassName": job.worker_class_name, + "#{dimension}": 1 + #"count": value, + #"requestId": "989ffbf8-9ace-4817-a57c-e4dd734019ee" + } + #data[key] = value + end + end +end