2016-04-07 14:32:31 -04:00
|
|
|
class Jobs::Onceoff < Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def self.name_for(klass)
|
|
|
|
klass.name.sub(/^Jobs\:\:/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pass `force: true` to force it happen again
|
|
|
|
def execute(args)
|
|
|
|
job_name = self.class.name_for(self.class)
|
|
|
|
|
|
|
|
if args[:force] || !OnceoffLog.where(job_name: job_name).exists?
|
2016-04-07 15:31:32 -04:00
|
|
|
return if $redis.exists(self.class.name)
|
2016-04-07 15:07:24 -04:00
|
|
|
DistributedMutex.synchronize(self.class.name) do
|
2016-04-12 09:08:10 -04:00
|
|
|
return if OnceoffLog.where(job_name: job_name).exists? && !args[:force]
|
2016-04-07 15:07:24 -04:00
|
|
|
execute_onceoff(args)
|
|
|
|
OnceoffLog.create(job_name: job_name)
|
|
|
|
end
|
2016-04-07 14:32:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enqueue_all
|
2016-04-07 15:07:24 -04:00
|
|
|
previously_ran = OnceoffLog.pluck(:job_name).uniq
|
|
|
|
|
2016-04-07 14:32:31 -04:00
|
|
|
ObjectSpace.each_object(Class).select { |klass| klass < self }.each do |klass|
|
2016-04-07 15:07:24 -04:00
|
|
|
job_name = name_for(klass)
|
|
|
|
unless previously_ran.include?(job_name)
|
|
|
|
Jobs.enqueue(job_name.underscore.to_sym)
|
|
|
|
end
|
2016-04-07 14:32:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|