Class: Cyclid::API::Plugins::Local

Inherits:
Dispatcher show all
Extended by:
Health::Helpers
Defined in:
app/cyclid/plugins/dispatcher/local.rb

Overview

Local Sidekiq based dispatcher

Constant Summary

Constants included from Health::Helpers

Health::Helpers::STATUSES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Health::Helpers

health_status

Methods inherited from Dispatcher

human_name

Methods inherited from Base

author, config?, config_schema, default_config, get_config, homepage, human_name, license, register_plugin, set_config, update_config, version

Class Method Details

.metadataObject

Plugin metadata



74
75
76
77
78
79
# File 'app/cyclid/plugins/dispatcher/local.rb', line 74

def self.
  super.merge!(version: Cyclid::Api::VERSION,
               license: 'Apache-2.0',
               author: 'Liqwyd Ltd.',
               homepage: 'http://docs.cyclid.io')
end

.statusObject

Perform a health check; for this plugin that means:

Is Sidekiq running? Is the queue size healthy?



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/cyclid/plugins/dispatcher/local.rb', line 56

def self.status
  stats = Sidekiq::Stats.new
  if stats.processes_size.zero?
    health_status(:error,
                  'no Sidekiq process is running')
  elsif stats.enqueued > 10
    health_status(:warning,
                  "Sidekiq queue length is too high: #{stats.enqueued}")
  elsif stats.default_queue_latency > 60
    health_status(:warning,
                  "Sidekiq queue latency is too high: #{stats.default_queue_latency}")
  else
    health_status(:ok,
                  'sidekiq is okay')
  end
end

Instance Method Details

#dispatch(job, record, callback = nil) ⇒ Object

Queue the job to be run asynchronously.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/cyclid/plugins/dispatcher/local.rb', line 27

def dispatch(job, record, callback = nil)
  Cyclid.logger.debug "dispatching job: #{job}"

  job_definition = job.to_hash.to_json

  record.job_name = job.name
  record.job_version = job.version
  record.job = job_definition
  record.save!

  # The callback instance has to be serailised into JSON to survive the
  # trip through Redis to Sidekiq
  callback_json = callback.nil? ? nil : Oj.dump(callback)

  # Create a SideKiq worker and pass in the job
  Worker::Local.perform_async(job_definition, record.id, callback_json)

  # The JobRecord ID is as good a job identifier as anything
  return record.id
end