Sha256: 34aa86c6a3892ee646094ae19251a9280f995a68001386779c9b1bbe7ebf49d5

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

class EndlessUninterruptiveWorker
  include Shoryuken::Worker

  # Usage:
  # QUEUE="super-q"
  # MAX_EXECUTION_TIME=2000 QUEUE=$QUEUE \
  # bundle exec ./bin/shoryuken -r ./examples/endless_interruptive_worker.rb -q $QUEUE -c 8

  class << self
    def queue
      ENV['QUEUE'] || 'default'
    end

    def max_execution_time
      ENV["MAX_EXECUTION_TIME"] ? ENV["MAX_EXECUTION_TIME"].to_i : 100
    end

    def rng
      @rng ||= Random.new
    end

    # returns a random number between 0 and 100
    def random_number(hi = 1000)
      (rng.rand * hi).to_i
    end
  end

  def perform(sqs_msg, body)
    Shoryuken.logger.info("Received message: '#{body}'")

    execution_ms = self.class.random_number(self.class.max_execution_time)
    Shoryuken.logger.info("Going to burn metal for #{execution_ms}ms")
    end_time = Time.now + execution_ms.to_f / 1000
    while Time.now < end_time do
      # burn metal
    end

    new_body = "#{execution_ms}-" + body.to_s

    self.class.perform_async(new_body.slice(0, 512))
  end

  shoryuken_options queue: queue, auto_delete: true
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
shoryuken-3.0.7 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.6 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.5 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.4 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.3 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.2 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.1 test_workers/endless_uninterruptive_worker.rb
shoryuken-3.0.0 test_workers/endless_uninterruptive_worker.rb
shoryuken-2.1.3 test_workers/endless_uninterruptive_worker.rb
shoryuken-2.1.2 test_workers/endless_uninterruptive_worker.rb