Class: Sidekiq::Cron::Job
- Inherits:
-
Object
- Object
- Sidekiq::Cron::Job
- Defined in:
- lib/sidekiq/cron/job.rb
Constant Summary collapse
- REMEMBER_THRESHOLD =
How long we would like to store information about previous enqueues.
24 * 60 * 60
- LAST_ENQUEUE_TIME_FORMAT =
Time format for enqueued jobs.
'%Y-%m-%d %H:%M:%S %z'
- GLOBALID_KEY =
Use serialize/deserialize key of GlobalID.
"_sc_globalid"
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#cron ⇒ Object
Returns the value of attribute cron.
-
#description ⇒ Object
Returns the value of attribute description.
-
#fetch_missing_args ⇒ Object
readonly
Returns the value of attribute fetch_missing_args.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#last_enqueue_time ⇒ Object
readonly
Returns the value of attribute last_enqueue_time.
-
#message ⇒ Object
Returns the value of attribute message.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.all(namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
Get all cron jobs.
- .count(namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
-
.create(hash) ⇒ Object
Create new instance of cron job.
-
.destroy(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
Destroy job by name.
-
.destroy_all! ⇒ Object
Remove all job from cron.
-
.destroy_removed_jobs(new_job_names) ⇒ Object
Remove “removed jobs” between current jobs and new jobs.
- .exists?(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Boolean
- .find(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
-
.load_from_array(array, options = {}) ⇒ Object
Load cron jobs from Array.
-
.load_from_array!(array, options = {}) ⇒ Object
Like #load_from_array.
-
.load_from_hash(hash, options = {}) ⇒ Object
Load cron jobs from Hash.
-
.load_from_hash!(hash, options = {}) ⇒ Object
Like #load_from_hash.
Instance Method Summary collapse
-
#active_job_message ⇒ Object
Active Job has different structure how it is loading data from Sidekiq queue, it creates a wrapper around job.
- #add_jid_history(jid) ⇒ Object
- #cron_expression_string ⇒ Object
- #date_as_argument? ⇒ Boolean
- #destroy ⇒ Object
- #disable! ⇒ Object
- #disabled? ⇒ Boolean
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
-
#enqueue!(time = Time.now.utc) ⇒ Object
Enqueue cron job to queue.
- #enqueue_active_job(klass_const) ⇒ Object
- #enqueue_args ⇒ Object
- #enqueue_sidekiq_worker(klass_const) ⇒ Object
- #errors ⇒ Object
- #exists? ⇒ Boolean
- #formatted_enqueue_time(now = Time.now.utc) ⇒ Object
- #formatted_last_time(now = Time.now.utc) ⇒ Object
- #human_cron ⇒ Object
-
#initialize(input_args = {}) ⇒ Job
constructor
A new instance of Job.
- #is_active_job?(klass = nil) ⇒ Boolean
- #jid_history_from_redis ⇒ Object
- #klass_valid ⇒ Object
- #last_enqueue_time_from_redis ⇒ Object
-
#last_time(now = Time.now.utc) ⇒ Object
Parse cron specification ‘* * * * *’ and returns time when last run should be performed.
- #pretty_message ⇒ Object
- #queue_name_with_prefix ⇒ Object
-
#remove_previous_enqueues(time) ⇒ Object
Remove previous information about run times, this will clear Redis and make sure that Redis will not overflow with memory.
- #save ⇒ Object
- #save_last_enqueue_time ⇒ Object
-
#should_enqueue?(time) ⇒ Boolean
Crucial part of whole enqueuing job.
-
#sidekiq_worker_message ⇒ Object
Sidekiq worker message.
- #sort_name ⇒ Object
- #status ⇒ Object
- #status_from_redis ⇒ Object
-
#test_and_enqueue_for_time!(time) ⇒ Object
Test if job should be enqueued.
-
#to_hash ⇒ Object
Export job data to hash.
- #valid? ⇒ Boolean
Constructor Details
#initialize(input_args = {}) ⇒ Job
Returns a new instance of Job.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sidekiq/cron/job.rb', line 23 def initialize input_args = {} args = Hash[input_args.map{ |k, v| [k.to_s, v] }] @fetch_missing_args = args.delete('fetch_missing_args') @fetch_missing_args = true if @fetch_missing_args.nil? @name = args["name"] @cron = args["cron"] @description = args["description"] if args["description"] @source = args["source"] == "schedule" ? "schedule" : "dynamic" default_namespace = Sidekiq::Cron.configuration.default_namespace @namespace = args["namespace"] || default_namespace if Sidekiq::Cron::Namespace.available_namespaces_provided? && !Sidekiq::Cron::Namespace.all.include?(@namespace) Sidekiq.logger.warn { "Cron Jobs - unexpected namespace #{@namespace} encountered. Assigning to default namespace." } @namespace = default_namespace end # Get class from klass or class. @klass = args["klass"] || args["class"] # Set status of job. @status = args['status'] || status_from_redis # Set last enqueue time - from args or from existing job. if args['last_enqueue_time'] && !args['last_enqueue_time'].empty? @last_enqueue_time = parse_enqueue_time(args['last_enqueue_time']) else @last_enqueue_time = last_enqueue_time_from_redis end # Get right arguments for job. @symbolize_args = args["symbolize_args"] == true || ("#{args["symbolize_args"]}" =~ (/^(true|t|yes|y|1)$/i)) == 0 || false @args = parse_args(args["args"]) @date_as_argument = args["date_as_argument"] == true || ("#{args["date_as_argument"]}" =~ (/^(true|t|yes|y|1)$/i)) == 0 || false @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"] @active_job_queue_name_delimiter = args["queue_name_delimiter"] # symbolize_args is only used when active_job is true Sidekiq.logger.warn { "Cron Jobs - 'symbolize_args' is gonna be ignored, as it is only used when 'active_job' is true" } if @symbolize_args && !@active_job if args["message"] @message = args["message"] = Sidekiq.load_json(@message) || {} @queue = ['queue'] || "default" @retry = ['retry'] elsif @klass = { "class" => @klass.to_s, "args" => @args, } # Get right data for message, # only if message wasn't specified before. klass_data = (@klass, @args) = klass_data.merge() # Override queue and retry if set in config, # only if message is hash - can be string (dumped JSON). if args['queue'] @queue = ['queue'] = args['queue'] else @queue = ['queue'] || "default" end if args['retry'] != nil @retry = ['retry'] = args['retry'] else @retry = ['retry'] end @message = end @queue_name_with_prefix = queue_name_with_prefix end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
21 22 23 |
# File 'lib/sidekiq/cron/job.rb', line 21 def args @args end |
#cron ⇒ Object
Returns the value of attribute cron.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def cron @cron end |
#description ⇒ Object
Returns the value of attribute description.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def description @description end |
#fetch_missing_args ⇒ Object (readonly)
Returns the value of attribute fetch_missing_args.
21 22 23 |
# File 'lib/sidekiq/cron/job.rb', line 21 def fetch_missing_args @fetch_missing_args end |
#klass ⇒ Object
Returns the value of attribute klass.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def klass @klass end |
#last_enqueue_time ⇒ Object (readonly)
Returns the value of attribute last_enqueue_time.
21 22 23 |
# File 'lib/sidekiq/cron/job.rb', line 21 def last_enqueue_time @last_enqueue_time end |
#message ⇒ Object
Returns the value of attribute message.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def @message end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
20 21 22 |
# File 'lib/sidekiq/cron/job.rb', line 20 def namespace @namespace end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
21 22 23 |
# File 'lib/sidekiq/cron/job.rb', line 21 def source @source end |
Class Method Details
.all(namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
Get all cron jobs.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/sidekiq/cron/job.rb', line 294 def self.all(namespace = Sidekiq::Cron.configuration.default_namespace) job_hashes = nil Sidekiq.redis do |conn| job_keys = job_keys_from_namespace(namespace) job_hashes = conn.pipelined do |pipeline| job_keys.each do |job_key| pipeline.hgetall(job_key) end end end job_hashes.compact.reject(&:empty?).collect do |h| # No need to fetch missing args from Redis since we just got this hash from there Sidekiq::Cron::Job.new(h.merge(fetch_missing_args: false)) end end |
.count(namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
311 312 313 314 315 316 317 318 319 |
# File 'lib/sidekiq/cron/job.rb', line 311 def self.count(namespace = Sidekiq::Cron.configuration.default_namespace) if namespace == '*' Namespace.all_with_count.reduce(0) do |memo, namespace_count| memo + namespace_count[:count] end else Sidekiq.redis { |conn| conn.scard(jobs_key(namespace)) } end end |
.create(hash) ⇒ Object
Create new instance of cron job.
336 337 338 |
# File 'lib/sidekiq/cron/job.rb', line 336 def self.create hash new(hash).save end |
.destroy(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
Destroy job by name.
341 342 343 344 345 346 347 348 349 350 |
# File 'lib/sidekiq/cron/job.rb', line 341 def self.destroy(name, namespace = Sidekiq::Cron.configuration.default_namespace) # If name is hash try to get name from it. name = name[:name] || name['name'] if name.is_a?(Hash) if (job = find(name, namespace)) job.destroy else false end end |
.destroy_all! ⇒ Object
Remove all job from cron.
542 543 544 545 546 547 |
# File 'lib/sidekiq/cron/job.rb', line 542 def self.destroy_all! all.each do |job| job.destroy end Sidekiq.logger.info { "Cron Jobs - deleted all jobs" } end |
.destroy_removed_jobs(new_job_names) ⇒ Object
Remove “removed jobs” between current jobs and new jobs
550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/sidekiq/cron/job.rb', line 550 def self.destroy_removed_jobs new_job_names current_jobs = Sidekiq::Cron::Job.all("*").filter_map { |j| j if j.source == "schedule" } current_job_names = current_jobs.map(&:name) removed_job_names = current_job_names - new_job_names removed_job_names.each do |j| job_to_destroy = current_jobs.detect { |job| job.name == j } Sidekiq::Cron::Job.destroy( job_to_destroy.name, job_to_destroy.namespace ) end removed_job_names end |
.exists?(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Boolean
579 580 581 582 583 584 585 |
# File 'lib/sidekiq/cron/job.rb', line 579 def self.exists?(name, namespace = Sidekiq::Cron.configuration.default_namespace) out = Sidekiq.redis do |conn| conn.exists(redis_key(name, namespace)) end [true, 1].include?(out) end |
.find(name, namespace = Sidekiq::Cron.configuration.default_namespace) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/sidekiq/cron/job.rb', line 321 def self.find(name, namespace = Sidekiq::Cron.configuration.default_namespace) # If name is hash try to get name from it. name = name[:name] || name['name'] if name.is_a?(Hash) return unless exists? name, namespace output = nil Sidekiq.redis do |conn| if exists? name, namespace output = Job.new conn.hgetall(redis_key(name, namespace)) end end output if output && output.valid? end |
.load_from_array(array, options = {}) ⇒ Object
Load cron jobs from Array. Input structure should look like: [
{
'namespace' => 'MyNamespace',
'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 * * * *'
}
]
276 277 278 279 280 281 282 283 |
# File 'lib/sidekiq/cron/job.rb', line 276 def self.load_from_array(array, = {}) errors = {} array.each do |job_data| job = new(job_data.merge()) errors[job.name] = job.errors unless job.save end errors end |
.load_from_array!(array, options = {}) ⇒ Object
Like #load_from_array. If exists old jobs in Redis but removed from args, destroy old jobs.
287 288 289 290 291 |
# File 'lib/sidekiq/cron/job.rb', line 287 def self.load_from_array!(array, = {}) job_names = array.map { |job| job["name"] || job[:name] } destroy_removed_jobs(job_names) load_from_array(array, ) end |
.load_from_hash(hash, options = {}) ⇒ Object
Load cron jobs from Hash. Input structure should look like: {
'name_of_job' => {
'namespace' => 'MyNamespace',
'class' => 'MyClass',
'cron' => '1 * * * *',
'args' => '(OPTIONAL) [Array or Hash]',
'description' => '(OPTIONAL) Description of job'
},
'My super iber cool job' => {
'class' => 'SecondClass',
'cron' => '*/5 * * * *'
}
}
243 244 245 246 247 248 249 |
# File 'lib/sidekiq/cron/job.rb', line 243 def self.load_from_hash(hash, = {}) array = hash.map do |key, job| job['name'] = key job end load_from_array(array, ) end |
.load_from_hash!(hash, options = {}) ⇒ Object
Like #load_from_hash. If exists old jobs in Redis but removed from args, destroy old jobs.
253 254 255 256 |
# File 'lib/sidekiq/cron/job.rb', line 253 def self.load_from_hash!(hash, = {}) destroy_removed_jobs(hash.keys) load_from_hash(hash, ) end |
Instance Method Details
#active_job_message ⇒ Object
Active Job has different structure how it is loading data from Sidekiq queue, it creates a wrapper around job.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sidekiq/cron/job.rb', line 212 def { 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper', 'wrapped' => @klass, 'queue' => @queue_name_with_prefix, 'description' => @description, 'args' => [{ 'job_class' => @klass, 'job_id' => SecureRandom.uuid, 'queue_name' => @queue_name_with_prefix, 'arguments' => enqueue_args }] } end |
#add_jid_history(jid) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/sidekiq/cron/job.rb', line 508 def add_jid_history(jid) jid_history = { jid: jid, enqueued: @last_enqueue_time } @history_size ||= Sidekiq::Cron.configuration.cron_history_size.to_i - 1 Sidekiq.redis do |conn| conn.lpush jid_history_key, Sidekiq.dump_json(jid_history) # Keep only last 10 entries in a fifo manner. conn.ltrim jid_history_key, 0, @history_size end end |
#cron_expression_string ⇒ Object
599 600 601 |
# File 'lib/sidekiq/cron/job.rb', line 599 def cron_expression_string parsed_cron.to_cron_s end |
#date_as_argument? ⇒ Boolean
164 165 166 |
# File 'lib/sidekiq/cron/job.rb', line 164 def date_as_argument? !!@date_as_argument end |
#destroy ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/sidekiq/cron/job.rb', line 523 def destroy Sidekiq.redis do |conn| # Delete from set. conn.srem self.class.jobs_key(@namespace), [redis_key] # Delete ran timestamps. conn.del job_enqueued_key # Delete jid_history. conn.del jid_history_key # Delete main job. conn.del redis_key end Sidekiq.logger.info { "Cron Jobs - deleted job with name #{@name} from namespace #{@namespace}" } end |
#disable! ⇒ Object
356 357 358 359 |
# File 'lib/sidekiq/cron/job.rb', line 356 def disable! @status = "disabled" save end |
#disabled? ⇒ Boolean
370 371 372 |
# File 'lib/sidekiq/cron/job.rb', line 370 def disabled? !enabled? end |
#enable! ⇒ Object
361 362 363 364 |
# File 'lib/sidekiq/cron/job.rb', line 361 def enable! @status = "enabled" save end |
#enabled? ⇒ Boolean
366 367 368 |
# File 'lib/sidekiq/cron/job.rb', line 366 def enabled? @status == "enabled" end |
#enqueue!(time = Time.now.utc) ⇒ Object
Enqueue cron job to queue.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sidekiq/cron/job.rb', line 132 def enqueue! time = Time.now.utc @last_enqueue_time = time klass_const = Sidekiq::Cron::Support.safe_constantize(@klass.to_s) jid = if klass_const if is_active_job?(klass_const) enqueue_active_job(klass_const).try :provider_job_id else enqueue_sidekiq_worker(klass_const) end else if @active_job Sidekiq::Client.push() else Sidekiq::Client.push() end end save_last_enqueue_time add_jid_history jid Sidekiq.logger.debug { "enqueued #{@name}: #{@message}" } end |
#enqueue_active_job(klass_const) ⇒ Object
173 174 175 |
# File 'lib/sidekiq/cron/job.rb', line 173 def enqueue_active_job(klass_const) klass_const.set(queue: @queue).perform_later(*enqueue_args) end |
#enqueue_args ⇒ Object
168 169 170 171 |
# File 'lib/sidekiq/cron/job.rb', line 168 def enqueue_args args = date_as_argument? ? @args + [Time.now.to_f] : @args deserialize_argument(args) end |
#enqueue_sidekiq_worker(klass_const) ⇒ Object
177 178 179 |
# File 'lib/sidekiq/cron/job.rb', line 177 def enqueue_sidekiq_worker(klass_const) klass_const.set(queue: queue_name_with_prefix, retry: @retry).perform_async(*enqueue_args) end |
#errors ⇒ Object
440 441 442 |
# File 'lib/sidekiq/cron/job.rb', line 440 def errors @errors ||= [] end |
#exists? ⇒ Boolean
587 588 589 |
# File 'lib/sidekiq/cron/job.rb', line 587 def exists? self.class.exists? @name, @namespace end |
#formatted_enqueue_time(now = Time.now.utc) ⇒ Object
571 572 573 |
# File 'lib/sidekiq/cron/job.rb', line 571 def formatted_enqueue_time now = Time.now.utc last_time(now).getutc.to_f.to_s end |
#formatted_last_time(now = Time.now.utc) ⇒ Object
575 576 577 |
# File 'lib/sidekiq/cron/job.rb', line 575 def formatted_last_time now = Time.now.utc last_time(now).getutc.iso8601 end |
#human_cron ⇒ Object
380 381 382 383 384 |
# File 'lib/sidekiq/cron/job.rb', line 380 def human_cron Cronex::ExpressionDescriptor.new(cron).description rescue cron end |
#is_active_job?(klass = nil) ⇒ Boolean
157 158 159 160 161 162 |
# File 'lib/sidekiq/cron/job.rb', line 157 def is_active_job?(klass = nil) @active_job || defined?(::ActiveJob::Base) && begin klass ||= Sidekiq::Cron::Support.safe_constantize(@klass.to_s) klass ? klass < ::ActiveJob::Base : false end end |
#jid_history_from_redis ⇒ Object
407 408 409 410 411 412 413 414 415 416 |
# File 'lib/sidekiq/cron/job.rb', line 407 def jid_history_from_redis out = Sidekiq.redis do |conn| conn.lrange(jid_history_key, 0, -1) rescue nil end out && out.map do |jid_history_raw| Sidekiq.load_json jid_history_raw end end |
#klass_valid ⇒ Object
467 468 469 470 471 472 473 474 475 |
# File 'lib/sidekiq/cron/job.rb', line 467 def klass_valid case @klass when Class true when String @klass.size > 0 else end end |
#last_enqueue_time_from_redis ⇒ Object
397 398 399 400 401 402 403 404 405 |
# File 'lib/sidekiq/cron/job.rb', line 397 def last_enqueue_time_from_redis out = nil if fetch_missing_args Sidekiq.redis do |conn| out = parse_enqueue_time(conn.hget(redis_key, "last_enqueue_time")) rescue nil end end out end |
#last_time(now = Time.now.utc) ⇒ Object
Parse cron specification ‘* * * * *’ and returns time when last run should be performed
567 568 569 |
# File 'lib/sidekiq/cron/job.rb', line 567 def last_time now = Time.now.utc parsed_cron.previous_time(now.utc).utc end |
#pretty_message ⇒ Object
374 375 376 377 378 |
# File 'lib/sidekiq/cron/job.rb', line 374 def JSON.pretty_generate Sidekiq.load_json() rescue JSON::ParserError end |
#queue_name_with_prefix ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/sidekiq/cron/job.rb', line 188 def queue_name_with_prefix return @queue unless is_active_job? if !"#{@active_job_queue_name_delimiter}".empty? queue_name_delimiter = @active_job_queue_name_delimiter elsif defined?(::ActiveJob::Base) && defined?(::ActiveJob::Base.queue_name_delimiter) && !::ActiveJob::Base.queue_name_delimiter.empty? queue_name_delimiter = ::ActiveJob::Base.queue_name_delimiter else queue_name_delimiter = '_' end if !"#{@active_job_queue_name_prefix}".empty? queue_name = "#{@active_job_queue_name_prefix}#{queue_name_delimiter}#{@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_name_delimiter}#{@queue}" else queue_name = @queue end queue_name end |
#remove_previous_enqueues(time) ⇒ Object
Remove previous information about run times, this will clear Redis and make sure that Redis will not overflow with memory.
116 117 118 119 120 |
# File 'lib/sidekiq/cron/job.rb', line 116 def remove_previous_enqueues time Sidekiq.redis do |conn| conn.zremrangebyscore(job_enqueued_key, 0, "(#{(time.to_f - REMEMBER_THRESHOLD).to_s}") end end |
#save ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/sidekiq/cron/job.rb', line 477 def save # If job is invalid, return false. return false unless valid? Sidekiq.redis do |conn| # Add to set of all jobs conn.sadd self.class.jobs_key(@namespace), [redis_key] # Add information for this job! conn.hset redis_key, to_hash.transform_values! { |v| v || '' }.flatten # Add information about last time! - don't enqueue right after scheduler poller starts! time = Time.now.utc exists = conn.exists(job_enqueued_key) unless exists == true || exists == 1 conn.zadd(job_enqueued_key, time.to_f.to_s, formatted_last_time(time).to_s) Sidekiq.logger.info { "Cron Jobs - added job with name #{@name} in the namespace #{@namespace}" } end end true end |
#save_last_enqueue_time ⇒ Object
501 502 503 504 505 506 |
# File 'lib/sidekiq/cron/job.rb', line 501 def save_last_enqueue_time Sidekiq.redis do |conn| # Update last enqueue time. conn.hset redis_key, 'last_enqueue_time', serialized_last_enqueue_time end end |
#should_enqueue?(time) ⇒ Boolean
Crucial part of whole enqueuing job.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/sidekiq/cron/job.rb', line 103 def should_enqueue? time return false unless status == "enabled" return false if past_scheduled_time?(time) return false if enqueued_after?(time) enqueue = Sidekiq.redis do |conn| conn.zadd(job_enqueued_key, formatted_enqueue_time(time), formatted_last_time(time)) end enqueue == true || enqueue == 1 end |
#sidekiq_worker_message ⇒ Object
Sidekiq worker message.
182 183 184 185 186 |
# File 'lib/sidekiq/cron/job.rb', line 182 def = @message.is_a?(String) ? Sidekiq.load_json(@message) : @message ["args"] = enqueue_args end |
#sort_name ⇒ Object
591 592 593 |
# File 'lib/sidekiq/cron/job.rb', line 591 def sort_name "#{status == "enabled" ? 0 : 1}_#{name}".downcase end |
#status ⇒ Object
352 353 354 |
# File 'lib/sidekiq/cron/job.rb', line 352 def status @status end |
#status_from_redis ⇒ Object
386 387 388 389 390 391 392 393 394 395 |
# File 'lib/sidekiq/cron/job.rb', line 386 def status_from_redis out = "enabled" if fetch_missing_args Sidekiq.redis do |conn| status = conn.hget redis_key, "status" out = status if status end end out end |
#test_and_enqueue_for_time!(time) ⇒ Object
Test if job should be enqueued.
123 124 125 126 127 128 129 |
# File 'lib/sidekiq/cron/job.rb', line 123 def test_and_enqueue_for_time! time if should_enqueue?(time) enqueue! remove_previous_enqueues(time) end end |
#to_hash ⇒ Object
Export job data to hash.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/sidekiq/cron/job.rb', line 419 def to_hash { name: @name, namespace: @namespace, klass: @klass.to_s, cron: @cron, description: @description, source: @source, args: @args.is_a?(String) ? @args : Sidekiq.dump_json(@args || []), date_as_argument: date_as_argument? ? "1" : "0", message: @message.is_a?(String) ? @message : Sidekiq.dump_json(@message || {}), status: @status, active_job: @active_job ? "1" : "0", queue_name_prefix: @active_job_queue_name_prefix, queue_name_delimiter: @active_job_queue_name_delimiter, retry: @retry.nil? || @retry.is_a?(Numeric) ? @retry : @retry.to_s, last_enqueue_time: serialized_last_enqueue_time, symbolize_args: symbolize_args? ? "1" : "0", } end |
#valid? ⇒ Boolean
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/sidekiq/cron/job.rb', line 444 def valid? # Clear previous errors. @errors = [] errors << "'name' must be set" if @name.nil? || @name.size == 0 errors << "'namespace' must be set" if @namespace.nil? || @namespace.size == 0 errors << "'namespace' cannot be '*'" if @namespace == "*" if @cron.nil? || @cron.size == 0 errors << "'cron' must be set" else begin @parsed_cron = do_parse_cron(@cron) rescue => e errors << "'cron' -> #{@cron.inspect} -> #{e.class}: #{e.}" end end errors << "'klass' (or class) must be set" unless klass_valid errors.empty? end |