Class: Funktor::WorkQueueHandler

Inherits:
Object
  • Object
show all
Includes:
ErrorHandler
Defined in:
lib/funktor/work_queue_handler.rb

Instance Method Summary collapse

Methods included from ErrorHandler

#handle_error

Constructor Details

#initializeWorkQueueHandler

Returns a new instance of WorkQueueHandler.



8
9
10
11
12
# File 'lib/funktor/work_queue_handler.rb', line 8

def initialize
  @failed_counter = Funktor::Counter.new('failed')
  @processed_counter = Funktor::Counter.new('processed')
  @tracker = Funktor::ActivityTracker.new
end

Instance Method Details

#call(event:, context:) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/funktor/work_queue_handler.rb', line 14

def call(event:, context:)
  event = Funktor::Aws::Sqs::Event.new(event)
  Funktor.logger.debug "event.jobs.count = #{event.jobs.count}"
  event.jobs.each do |job|
    dispatch(job)
  end
end

#delayed_job_tableObject



80
81
82
# File 'lib/funktor/work_queue_handler.rb', line 80

def delayed_job_table
  ENV['FUNKTOR_JOBS_TABLE']
end

#delete_job_from_dynamodb(job) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/funktor/work_queue_handler.rb', line 102

def delete_job_from_dynamodb(job)
  puts "starting delete_job_from_dynamodb"
  dynamodb_client.delete_item({
    key: {
      "jobShard" => job.shard,
      "jobId" => job.job_id
    },
    table_name: delayed_job_table,
    return_values: "ALL_OLD"
  })
  puts "ending delete_job_from_dynamodb"
end

#dispatch(job) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/funktor/work_queue_handler.rb', line 30

def dispatch(job)
  begin
    @tracker.track(:processingStarted, job)
    if Funktor.enable_work_queue_visibility
      update_job_category(job, "processing")
    end
    Funktor.work_queue_handler_middleware.invoke(job) do
      job.execute
    end
    @processed_counter.incr(job)
    @tracker.track(:processingComplete, job)

    if Funktor.enable_work_queue_visibility
      delete_job_from_dynamodb(job)
    end
  # rescue Funktor::Job::InvalidJsonError # TODO Make this work
  rescue Exception => e
    handle_error(e, job)
    @failed_counter.incr(job)
    job.error = e
    if job.can_retry
      @tracker.track(:retrying, job)

      if Funktor.enable_work_queue_visibility
        update_job_category(job, "retry")
      end
      trigger_retry(job)
    else
      @tracker.track(:bailingOut, job)

      if Funktor.enable_work_queue_visibility
        update_job_category(job, "dead")
      end
      Funktor.logger.error "We retried max times. We're bailing on this one."
      Funktor.logger.error job.to_json
    end
    @tracker.track(:processingFailed, job)
  end
end

#dynamodb_clientObject



22
23
24
# File 'lib/funktor/work_queue_handler.rb', line 22

def dynamodb_client
  Funktor.dynamodb_client
end

#sqs_clientObject



26
27
28
# File 'lib/funktor/work_queue_handler.rb', line 26

def sqs_client
  Funktor.sqs_client
end

#trigger_retry(job) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/funktor/work_queue_handler.rb', line 70

def trigger_retry(job)
  job.increment_retries
  Funktor.logger.error "scheduling retry # #{job.retries} with delay of #{job.delay}"
  Funktor.logger.error job.to_json
  sqs_client.send_message({
    queue_url: job.retry_queue_url,
    message_body: job.to_json
  })
end

#update_job_category(job, category) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/funktor/work_queue_handler.rb', line 84

def update_job_category(job, category)
  puts "starting update_job_category #{category}"
  dynamodb_client.update_item({
    key: {
      "jobShard" => job.shard,
      "jobId" => job.job_id
    },
    table_name: delayed_job_table,
    update_expression: "SET category = :category, queueable = :queueable",
    expression_attribute_values: {
      ":queueable" => "false",
      ":category" => category
    },
    return_values: "ALL_OLD"
  })
  puts "ending update_job_category #{category}"
end