lib/sidekiq/cron/job.rb in sidekiq-cron-0.3.1 vs lib/sidekiq/cron/job.rb in sidekiq-cron-0.4.0

- old
+ new

@@ -1,8 +1,7 @@ require 'sidekiq' require 'sidekiq/util' -require 'sidekiq/actor' require 'rufus-scheduler' module Sidekiq module Cron @@ -43,11 +42,11 @@ #enque cron job to queue def enque! time = Time.now @last_enqueue_time = time - if defined?(ActiveJob::Base) && @klass.to_s.constantize < ActiveJob::Base + if @active_job or defined?(ActiveJob::Base) && @klass.to_s.constantize < ActiveJob::Base Sidekiq::Client.push(active_job_message) else Sidekiq::Client.push(sidekiq_worker_message) end @@ -61,29 +60,39 @@ end # active job has different structure how it is loading data from sidekiq # queue, it createaswrapper arround job def active_job_message + if !"#{@active_job_queue_name_prefix}".empty? + queue_name = "#{@active_job_queue_name_prefix}_#{@queue}" + elsif defined?(ActiveJob::Base) && defined?(ActiveJob::Base.queue_name_prefix) && !"#{ActiveJob::Base.queue_name_prefix}".empty? + queue_name = "#{ActiveJob::Base.queue_name_prefix}_#{@queue}" + else + queue_name = @queue + end + { - 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper', - 'queue' => @queue, - 'args' => [{ + 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper', + 'queue' => queue_name, + 'description' => @description, + 'args' => [{ 'job_class' => @klass, 'job_id' => SecureRandom.uuid, - 'queue_name' => @queue, + 'queue_name' => queue_name, 'arguments' => @args }] } end # load cron jobs from Hash # input structure should look like: # { # 'name_of_job' => { - # 'class' => 'MyClass', - # 'cron' => '1 * * * *', - # 'args' => '(OPTIONAL) [Array or Hash]' + # 'class' => 'MyClass', + # 'cron' => '1 * * * *', + # 'args' => '(OPTIONAL) [Array or Hash]', + # 'description' => '(OPTIONAL) Description of job' # }, # 'My super iber cool job' => { # 'class' => 'SecondClass', # 'cron' => '*/5 * * * *' # } @@ -106,14 +115,15 @@ # load cron jobs from Array # input structure should look like: # [ # { - # 'name' => 'name_of_job', - # 'class' => 'MyClass', - # 'cron' => '1 * * * *', - # 'args' => '(OPTIONAL) [Array or Hash]' + # 'name' => 'name_of_job', + # 'class' => 'MyClass', + # 'cron' => '1 * * * *', + # 'args' => '(OPTIONAL) [Array or Hash]', + # 'description' => '(OPTIONAL) Description of job' # }, # { # 'name' => 'Cool Job for Second Class', # 'class' => 'SecondClass', # 'cron' => '*/5 * * * *' @@ -188,18 +198,19 @@ else false end end - attr_accessor :name, :cron, :klass, :args, :message + attr_accessor :name, :cron, :description, :klass, :args, :message attr_reader :last_enqueue_time def initialize input_args = {} args = input_args.stringify_keys @name = args["name"] @cron = args["cron"] + @description = args["description"] if args["description"] #get class from klass or class @klass = args["klass"] || args["class"] #set status of job @@ -213,10 +224,13 @@ end #get right arguments for job @args = args["args"].nil? ? [] : parse_args( args["args"] ) + @active_job = args["active_job"] == true || ("#{args["active_job"]}" =~ (/^(true|t|yes|y|1)$/i)) == 0 || false + @active_job_queue_name_prefix = args["queue_name_prefix"] + if args["message"] @message = args["message"] message_data = Sidekiq.load_json(@message) || {} @queue = message_data['queue'] || default elsif @klass @@ -266,10 +280,18 @@ def enable! @status = "enabled" save end + def enabled? + @status == "enabled" + end + + def disabled? + !enabled? + end + def status_from_redis out = "enabled" if exists? Sidekiq.redis do |conn| out = conn.hget redis_key, "status" @@ -292,13 +314,16 @@ def to_hash { name: @name, klass: @klass, cron: @cron, + description: @description, args: @args.is_a?(String) ? @args : Sidekiq.dump_json(@args || []), message: @message.is_a?(String) ? @message : Sidekiq.dump_json(@message || {}), status: @status, + active_job: @active_job, + queue_name_prefix: @active_job_queue_name_prefix, last_enqueue_time: @last_enqueue_time, } end def errors @@ -407,10 +432,10 @@ def last_time now = Time.now Rufus::Scheduler::CronLine.new(@cron).previous_time(now) end def formated_last_time now = Time.now - last_time(now).getutc + last_time(now).getutc.iso8601 end def self.exists? name out = false Sidekiq.redis do |conn|