Sha256: a2c4bec618a0e758c95a229eeb4c15e417603a9becc655bfbf48db632aa52bbf

Contents?: true

Size: 1.94 KB

Versions: 18

Compression:

Stored size: 1.94 KB

Contents

require 'sidekiq/client'
require 'sidekiq/core_ext'

module Sidekiq

  ##
  # Include this module in your worker class and you can easily create
  # asynchronous jobs:
  #
  # class HardWorker
  #   include Sidekiq::Worker
  #
  #   def perform(*args)
  #     # do some work
  #   end
  # end
  #
  # Then in your Rails app, you can do this:
  #
  #   HardWorker.perform_async(1, 2, 3)
  #
  # Note that perform_async is a class method, perform is an instance method.
  module Worker
    attr_accessor :jid

    def self.included(base)
      base.extend(ClassMethods)
      base.class_attribute :sidekiq_options_hash
    end

    def logger
      Sidekiq.logger
    end

    module ClassMethods

      def perform_async(*args)
        client_push('class' => self, 'args' => args)
      end

      def perform_in(interval, *args)
        int = interval.to_f
        ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
        client_push('class' => self, 'args' => args, 'at' => ts)
      end
      alias_method :perform_at, :perform_in

      ##
      # Allows customization for this type of Worker.
      # Legal options:
      #
      #   :queue - use a named queue for this Worker, default 'default'
      #   :retry - enable the RetryJobs middleware for this Worker, default *true*
      #   :timeout - timeout the perform method after N seconds, default *nil*
      #   :backtrace - whether to save any error backtrace in the retry payload to display in web UI,
      #      can be true, false or an integer number of lines to save, default *false*
      def sidekiq_options(opts={})
        self.sidekiq_options_hash = get_sidekiq_options.merge((opts || {}).stringify_keys)
      end

      DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' }

      def get_sidekiq_options # :nodoc:
        self.sidekiq_options_hash ||= DEFAULT_OPTIONS
      end

      def client_push(item) # :nodoc:
        Sidekiq::Client.push(item.stringify_keys)
      end

    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
sidekiq-2.10.1 lib/sidekiq/worker.rb
sidekiq-2.10.0 lib/sidekiq/worker.rb
sidekiq-2.9.0 lib/sidekiq/worker.rb
sidekiq-2.8.0 lib/sidekiq/worker.rb
sidekiq-2.7.5 lib/sidekiq/worker.rb
sidekiq-2.7.4 lib/sidekiq/worker.rb
sidekiq-2.7.3 lib/sidekiq/worker.rb
sidekiq-2.7.2 lib/sidekiq/worker.rb
sidekiq-2.7.1 lib/sidekiq/worker.rb
sidekiq-2.7.0 lib/sidekiq/worker.rb
sidekiq-2.6.5 lib/sidekiq/worker.rb
sidekiq-2.6.4 lib/sidekiq/worker.rb
sidekiq-2.6.3 lib/sidekiq/worker.rb
sidekiq-2.6.2 lib/sidekiq/worker.rb
sidekiq-2.6.1 lib/sidekiq/worker.rb
sidekiq-2.6.0 lib/sidekiq/worker.rb
sidekiq-2.5.4 lib/sidekiq/worker.rb
sidekiq-2.5.3 lib/sidekiq/worker.rb