Module: SkRequeue::Core::InstanceMethods

Defined in:
lib/sk_requeue/core.rb

Instance Method Summary collapse

Instance Method Details

#build_model_key(action) ⇒ Object



68
69
70
# File 'lib/sk_requeue/core.rb', line 68

def build_model_key(action)
  key = "#{model_name}:#{id}:#{action}"
end

#klassObject



95
96
97
# File 'lib/sk_requeue/core.rb', line 95

def klass
  self.class
end

#model_nameObject



103
104
105
# File 'lib/sk_requeue/core.rb', line 103

def model_name
  klass.to_s
end

#next_enqueue_duration(frequency = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/sk_requeue/core.rb', line 72

def next_enqueue_duration(frequency = nil)
  frequency ||= worker_frequency
  now = Time.zone.now + 5.hours + 30.minutes
  base_time = now.beginning_of_day
  while ((base_time - now) < 0)
    base_time = base_time +
      frequency
  end
  base_time - now
end

#perform(*args) ⇒ Object



83
84
85
# File 'lib/sk_requeue/core.rb', line 83

def perform(*args)
  worker_klass.perform_in(*args)
end

#perform_uniq(action, sk_frequency, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sk_requeue/core.rb', line 55

def perform_uniq(action, sk_frequency , *args)
  ss = Sidekiq::ScheduledSet.new
  ss.each do |job|
    next if job.klass != klass.worker_name
    key = build_model_key(action)
    if klass.build_job_key(job) == key
      Rails.logger.warn("Skipping Current Request: A Job with key => #{key} already queued")
      return
    end
  end
  perform(next_enqueue_duration(sk_frequency), model_name, id, action, *args)
end

#requeue(action, *args) ⇒ Object



47
48
49
# File 'lib/sk_requeue/core.rb', line 47

def requeue(action, *args)
  perform_uniq(action, worker_frequency, *args)
end

#requeue_in(action, proxy_frequency, *args) ⇒ Object



51
52
53
# File 'lib/sk_requeue/core.rb', line 51

def requeue_in(action, proxy_frequency, *args)
  perform_uniq(action,proxy_frequency, *args)
end

#worker_frequencyObject



91
92
93
# File 'lib/sk_requeue/core.rb', line 91

def worker_frequency
  @worker_frequency ||= worker_frequency_in_mins * 60
end

#worker_frequency_in_minsObject



87
88
89
# File 'lib/sk_requeue/core.rb', line 87

def worker_frequency_in_mins
  freq = self.try(klass.frequency_column.to_sym) || 60
end

#worker_klassObject



99
100
101
# File 'lib/sk_requeue/core.rb', line 99

def worker_klass
  klass.worker_name.constantize
end