class Sidecloq::Schedule

Schedule loads and parses recurring job specs from files, hashes, and redis

Constants

REDIS_KEY

Attributes

job_specs[R]

Public Class Methods

from_hash(hash) click to toggle source
# File lib/sidecloq/schedule.rb, line 34
def self.from_hash(hash)
  hash = hash[env] if hash.key?(env)

  specs = hash.each_with_object({}) do |(name, spec), memo|
    memo[name] = spec.dup.tap do |s|
      s['class'] = name unless spec.key?('class') || spec.key?(:class)
      s['args'] = s['args'] || s[:args] || []
    end
  end

  new(specs)
end
from_redis() click to toggle source
# File lib/sidecloq/schedule.rb, line 18
def self.from_redis
  specs = {}

  if redis { |r| r.exists(REDIS_KEY) }
    specs = {}

    redis { |r| r.hgetall(REDIS_KEY) }.tap do |h|
      h.each do |name, config|
        specs[name] = JSON.parse(config)
      end
    end
  end

  from_hash(specs)
end
from_yaml(filename) click to toggle source
# File lib/sidecloq/schedule.rb, line 14
def self.from_yaml(filename)
  from_hash(YAML.load_file(filename))
end
new(specs) click to toggle source
# File lib/sidecloq/schedule.rb, line 10
def initialize(specs)
  @job_specs = specs
end

Private Class Methods

env() click to toggle source
# File lib/sidecloq/schedule.rb, line 76
def self.env
  rails_env || rack_env
end
rack_env() click to toggle source
# File lib/sidecloq/schedule.rb, line 84
def self.rack_env
  ENV['RACK_ENV']
end
rails_env() click to toggle source
# File lib/sidecloq/schedule.rb, line 80
def self.rails_env
  Rails.env if defined?(Rails)
end

Public Instance Methods

save_redis() click to toggle source
# File lib/sidecloq/schedule.rb, line 53
def save_redis
  reset_redis_schedule
  save_all_to_redis
end
save_yaml(filename) click to toggle source
# File lib/sidecloq/schedule.rb, line 47
def save_yaml(filename)
  File.open(filename, 'w') do |h|
    h.write @job_specs.to_yaml
  end
end

Private Instance Methods

reset_redis_schedule() click to toggle source
# File lib/sidecloq/schedule.rb, line 60
def reset_redis_schedule
  redis do |r|
    r.hkeys(REDIS_KEY).each do |k|
      r.hdel(REDIS_KEY, k)
    end
  end
end
save_all_to_redis() click to toggle source
# File lib/sidecloq/schedule.rb, line 68
def save_all_to_redis
  redis do |r|
    @job_specs.each do |name, spec|
      r.hset(REDIS_KEY, name, spec.to_json)
    end
  end
end