Module: SidekiqScheduler::Utils

Defined in:
lib/sidekiq-scheduler/utils.rb

Constant Summary collapse

RUFUS_METADATA_KEYS =
%w(description at cron every in interval enabled)

Class Method Summary collapse

Class Method Details

.active_job_enqueue?(klass) ⇒ Boolean

Returns true if the enqueuing needs to be done for an ActiveJob

class false otherwise.

Parameters:

  • klass (Class)

    the class to check is decendant from ActiveJob

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/sidekiq-scheduler/utils.rb', line 77

def self.active_job_enqueue?(klass)
  klass.is_a?(Class) && defined?(ActiveJob::Enqueuing) &&
    klass.included_modules.include?(ActiveJob::Enqueuing)
end

.enqueue_with_active_job(config) ⇒ Object

Enqueues the job using the ActiveJob.

Parameters:

  • config (Hash)

    The job configuration



92
93
94
95
96
97
98
# File 'lib/sidekiq-scheduler/utils.rb', line 92

def self.enqueue_with_active_job(config)
  options = {
    queue: config['queue']
  }.keep_if { |_, v| !v.nil? }

  initialize_active_job(config['class'], config['args']).enqueue(options)
end

.enqueue_with_sidekiq(config) ⇒ Object

Enqueues the job using the Sidekiq client.

Parameters:

  • config (Hash)

    The job configuration



85
86
87
# File 'lib/sidekiq-scheduler/utils.rb', line 85

def self.enqueue_with_sidekiq(config)
  Sidekiq::Client.push(sanitize_job_config(config))
end

.initialize_active_job(klass, args) ⇒ Object

Initializes active_job using the passed parameters.

Parameters:

  • klass (Class)

    The class to initialize

  • args (Array, Hash)

    The parameters passed to the klass initializer

Returns:

  • (Object)

    instance of the class klass



63
64
65
66
67
68
69
# File 'lib/sidekiq-scheduler/utils.rb', line 63

def self.initialize_active_job(klass, args)
  if args.is_a?(Array)
    klass.new(*args)
  else
    klass.new(args)
  end
end

.new_rufus_scheduler(options = {}) ⇒ Rufus::Scheduler

Creates a new instance of rufus scheduler.

Returns:

  • (Rufus::Scheduler)

    the scheduler instance



112
113
114
115
116
117
118
119
120
121
# File 'lib/sidekiq-scheduler/utils.rb', line 112

def self.new_rufus_scheduler(options = {})
  Rufus::Scheduler.new(options).tap do |scheduler|
    scheduler.define_singleton_method(:on_post_trigger) do |job, triggered_time|
      if (job_name = job.tags[0])
        SidekiqScheduler::Utils.update_job_last_time(job_name, triggered_time)
        SidekiqScheduler::Utils.update_job_next_time(job_name, job.next_time)
      end
    end
  end
end

.sanitize_job_config(config) ⇒ Hash

Removes the hash values associated to the rufus metadata keys.

Parameters:

  • config (Hash)

    The job configuration

Returns:

  • (Hash)

    the sanitized job config



105
106
107
# File 'lib/sidekiq-scheduler/utils.rb', line 105

def self.sanitize_job_config(config)
  config.reject { |k, _| RUFUS_METADATA_KEYS.include?(k) }
end

.stringify_keys(object) ⇒ Object

Stringify keys belonging to a hash.

Also stringifies nested keys and keys of hashes inside arrays, and sets

Parameters:

  • object (Object)

Returns:

  • (Object)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sidekiq-scheduler/utils.rb', line 15

def self.stringify_keys(object)
  if object.is_a?(Hash)
    Hash[[*object.map { |k, v| [k.to_s, stringify_keys(v) ]} ]]

  elsif object.is_a?(Array) || object.is_a?(Set)
    object.map { |v| stringify_keys(v) }

  else
    object
  end
end

.symbolize_keys(object) ⇒ Object

Symbolize keys belonging to a hash.

Also symbolizes nested keys and keys of hashes inside arrays, and sets

Parameters:

  • object (Object)

Returns:

  • (Object)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq-scheduler/utils.rb', line 34

def self.symbolize_keys(object)
  if object.is_a?(Hash)
    Hash[[*object.map { |k, v| [k.to_sym, symbolize_keys(v) ]} ]]

  elsif object.is_a?(Array) || object.is_a?(Set)
    object.map { |v| symbolize_keys(v) }

  else
    object
  end
end

.try_to_constantize(klass) ⇒ Class

Constantize a given string.

Parameters:

  • klass (String)

    The string to constantize

Returns:

  • (Class)

    the class corresponding to the klass param



51
52
53
54
55
# File 'lib/sidekiq-scheduler/utils.rb', line 51

def self.try_to_constantize(klass)
  klass.is_a?(String) ? Object.const_get(klass) : klass
rescue NameError
  klass
end

.update_job_last_time(name, last_time) ⇒ Object

Pushes job’s last execution time

Parameters:

  • name (String)

    The job’s name

  • last_time (Time)

    The job’s last execution time



139
140
141
# File 'lib/sidekiq-scheduler/utils.rb', line 139

def self.update_job_last_time(name, last_time)
  SidekiqScheduler::RedisManager.set_job_last_time(name, last_time) if last_time
end

.update_job_next_time(name, next_time) ⇒ Object

Pushes job’s next time execution

Parameters:

  • name (String)

    The job’s name

  • next_time (Time)

    The job’s next time execution



127
128
129
130
131
132
133
# File 'lib/sidekiq-scheduler/utils.rb', line 127

def self.update_job_next_time(name, next_time)
  if next_time
    SidekiqScheduler::RedisManager.set_job_next_time(name, next_time)
  else
    SidekiqScheduler::RedisManager.remove_job_next_time(name)
  end
end