Class: Cangaroo::PollJob

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
ClassConfiguration, Log
Defined in:
app/jobs/cangaroo/poll_job.rb

Instance Method Summary collapse

Methods included from Log

#log

Instance Method Details

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/jobs/cangaroo/poll_job.rb', line 13

def perform(*)
  log.set_context(self)

  last_poll = Time.at(arguments.first.fetch(:last_poll)).to_datetime
  current_time = DateTime.now

  if !perform?(current_time)
    log.info 'skipping poll'
    return
  end

  log.info 'initiating poll', last_poll: last_poll.to_i

  response = Cangaroo::Webhook::Client.new(destination_connection, path)
    .post({ last_poll: last_poll_timestamp.to_i }, @job_id, parameters)

  log.info 'processing poll results'

  command = HandleRequest.call(
    key: destination_connection.key,
    token: destination_connection.token,
    json_body: response.to_json,
    jobs: Rails.configuration.cangaroo.jobs
  )

  if !command.success?
    fail Cangaroo::Webhook::Error, command.message
  end

  log.info 'updating last poll', last_poll: current_time

  last_job_poll = Cangaroo::PollTimestamp.for_class(self.class)
  last_job_poll.value = current_time
  last_job_poll.save!

  log.reset_context!
end

#perform?(execution_time) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'app/jobs/cangaroo/poll_job.rb', line 51

def perform?(execution_time)
  last_poll_timestamp.nil? ||
  execution_time.to_i - last_poll_timestamp.to_i > self.class.frequency
end