Class: Funktor::IncomingJobHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/funktor/incoming_job_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeIncomingJobHandler

Returns a new instance of IncomingJobHandler.



8
9
10
# File 'lib/funktor/incoming_job_handler.rb', line 8

def initialize
  @tracker = Funktor::ActivityTracker.new
end

Instance Method Details

#call(event:, context:) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/funktor/incoming_job_handler.rb', line 12

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



68
69
70
# File 'lib/funktor/incoming_job_handler.rb', line 68

def delayed_job_table
  ENV['FUNKTOR_JOBS_TABLE']
end

#dispatch(job) ⇒ Object



28
29
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
# File 'lib/funktor/incoming_job_handler.rb', line 28

def dispatch(job)
  Funktor.incoming_job_handler_middleware.invoke(job) do
    # TODO : This number should be configurable via ENV var
    if job.delay < 60 # for now we're testing with just one minute * 5 # 5 minutes
      Funktor.logger.debug "pushing to work queue for delay = #{job.delay}"
      if Funktor.enable_work_queue_visibility
        # We push to the jobs table first becauase the work queue handler will expect to be able
        # to update the stats of a record that's already in the table.
        # TODO : For time sensitive jobs this is probably less than optimal. Can we update the
        # work queue handler to be ok with a job that's not yet in the table?
        push_to_jobs_table(job, "queued")
      end
      push_to_work_queue(job)
      if job.is_retry?
        @tracker.track(:retryActivated, job)
      else
        @tracker.track(:queued, job)
      end
    else
      Funktor.logger.debug "pushing to jobs table for delay = #{job.delay}"
      push_to_jobs_table(job, nil)
      if job.is_retry?
        # do nothing for tracking
      else
        @tracker.track(:scheduled, job)
      end
    end
    @tracker.track(:incoming, job)
  end
end

#dynamodb_clientObject



24
25
26
# File 'lib/funktor/incoming_job_handler.rb', line 24

def dynamodb_client
  Funktor.dynamodb_client
end

#push_to_jobs_table(job, category = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/funktor/incoming_job_handler.rb', line 72

def push_to_jobs_table(job, category = nil)
  resp = dynamodb_client.put_item({
    item: {
      payload: job.to_json,
      jobId: job.job_id,
      performAt: job.perform_at.iso8601,
      jobShard: job.shard,
      queueable: category.present? ? "false" : "true",
      category: category || (job.is_retry? ? "retry" : "scheduled")
    },
    table_name: delayed_job_table
  })
end

#push_to_work_queue(job) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/funktor/incoming_job_handler.rb', line 59

def push_to_work_queue(job)
  Funktor.logger.debug "job = #{job.to_json}"
  sqs_client.send_message({
    queue_url: job.work_queue_url,
    message_body: job.to_json,
    delay_seconds: job.delay
  })
end

#sqs_clientObject



20
21
22
# File 'lib/funktor/incoming_job_handler.rb', line 20

def sqs_client
  Funktor.sqs_client
end