<commit_before>require 'redis'
require 'json'
require 'yaml'
require 'logger'

module When
  DEFAULT_CONFIG = {
    schedule_key:      'when:schedules',
    worker_queue_key:  'when:queue:default',
    delayed_queue_key: 'when:delayed'
  }

  def self.schedule(name, cron, klass, *args)
    schedule = {'class' => klass.to_s, 'cron' => cron, 'args' => args}
    if redis.hset(schedule_key, name.to_s, schedule.to_json)
      logger.info("Scheduled '#{name}' => #{schedule}.")
    else
      msg = "Could not schedule '#{name}' => #{schedule}."
      logger.fatal(msg)
      raise msg
    end
  end

  def self.unschedule(name)
    json_sched = redis.hget(schedule_key, name.to_s)
    schedule = JSON.parse(json_sched) if json_sched
    if redis.hdel(schedule_key, name.to_s) > 0
      logger.info("Unscheduled '#{name}' => #{schedule}.")
      true
    else
      logger.warn("Could not unschedule '#{name}'. No schedule by that name was found.")
      false
    end
  end

  def self.unschedule_all
    count = redis.del(schedule_key)
    logger.info("Cleared #{count} schedules.")
    count
  end

  def self.schedules
    schedules = redis.hgetall(schedule_key)
    schedules.each { |k, v| schedules[k] = JSON.parse(v) }
  end

  def self.enqueue_at(time, klass, *args)
    job = { 'jid' => SecureRandom.uuid, 'class' => klass.to_s, 'args' => args }
    if redis.zadd(delayed_queue_key, time.to_i, job.to_json)
      logger.info("Delayed: will enqueue #{job} to run at #{time}.")
      job['jid']
    else<commit_msg>redis.hset returns false when successful if a key is overwritten<commit_after>require 'redis'
require 'json'
require 'yaml'
require 'logger'

module When
  DEFAULT_CONFIG = {
    schedule_key:      'when:schedules',
    worker_queue_key:  'when:queue:default',
    delayed_queue_key: 'when:delayed'
  }

  def self.schedule(name, cron, klass, *args)
    schedule = {'class' => klass.to_s, 'cron' => cron, 'args' => args}
    redis.hset(schedule_key, name.to_s, schedule.to_json)
    logger.info("Scheduled '#{name}' => #{schedule}.")
  end

  def self.unschedule(name)
    json_sched = redis.hget(schedule_key, name.to_s)
    schedule = JSON.parse(json_sched) if json_sched
    if redis.hdel(schedule_key, name.to_s) > 0
      logger.info("Unscheduled '#{name}' => #{schedule}.")
      true
    else
      logger.warn("Could not unschedule '#{name}'. No schedule by that name was found.")
      false
    end
  end

  def self.unschedule_all
    count = redis.del(schedule_key)
    logger.info("Cleared #{count} schedules.")
    count
  end

  def self.schedules
    schedules = redis.hgetall(schedule_key)
    schedules.each { |k, v| schedules[k] = JSON.parse(v) }
  end

  def self.enqueue_at(time, klass, *args)
    job = { 'jid' => SecureRandom.uuid, 'class' => klass.to_s, 'args' => args }
    if redis.zadd(delayed_queue_key, time.to_i, job.to_json)
      logger.info("Delayed: will enqueue #{job} to run at #{time}.")
      job['jid']
    else
{'fraction_non_alphanumeric': 0.14343434343434344, 'fraction_numerical': 0.0006734006734006734, 'mean_word_length': 3.349926793557833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1821341', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1052, 'n_words': 244}