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

.enqueue_with_active_job(config) ⇒ Object

Enqueues the job using the ActiveJob.

Parameters:

  • config (Hash)

    The job configuration



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

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



74
75
76
# File 'lib/sidekiq-scheduler/utils.rb', line 74

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

  • the (Array/Hash)

    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



101
102
103
104
105
106
107
108
# File 'lib/sidekiq-scheduler/utils.rb', line 101

def self.new_rufus_scheduler(options = {})
  Rufus::Scheduler.new(options).tap do |scheduler|
    scheduler.define_singleton_method(:on_post_trigger) do |job, triggered_time|
      SidekiqScheduler::Utils.update_job_last_time(job.tags[0], triggered_time)
      SidekiqScheduler::Utils.update_job_next_time(job.tags[0], job.next_time)
    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



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

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) ? klass.constantize : 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



126
127
128
# File 'lib/sidekiq-scheduler/utils.rb', line 126

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



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

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