Sha256: 639194909c4aca6f19c8ba781e82b5359392bdd057014e123f48eb3605d4e76d

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

#
# bundle exec ruby examples/async_sum.rb client
# bundle exec ruby examples/async_sum.rb server
#

require "tennis"
require "tennis/backend/rabbit"

logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
amqp_url = "amqp://guest:guest@localhost:5672"

Tennis.configure do |config|
  config.backend = Tennis::Backend::Rabbit.new(logger: logger, url: amqp_url)
  config.logger = logger
end

class Job
  include Tennis::Job

  def sum(*numbers)
    sleep 0.4
    total = numbers.inject(&:+)
    puts "Sum #{numbers} => #{total}"
  end

  def job_dump
    nil
  end

  def self.job_load(_)
    new
  end
end


if ARGV[0] == "server"
  require "tennis/launcher"

  # Start Tennis.
  launcher = Tennis::Launcher.new(concurrency: 2, job_classes: [Job])
  launcher.async.start

  begin
    sleep 5 while true
  rescue Interrupt
    puts "Stopping the server..."
  ensure
    launcher.async.stop
  end
else
  # Instanciate a job and add the sum to the job to do.
  numbers = (1..9).to_a
  10.times do
    Job.new.async.sum(*numbers.sample(3))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tennis-jobs-rmq-0.4.0 examples/async_sum.rb