rails sidekiq后台进程

时间:2013-08-01 08:25:14

标签: ruby ruby-on-rails-3 background-process sidekiq

我在配置sidekiq服务器时遇到问题,一旦刷新页面,该过程似乎就在前台运行。 / consumers / fetch我需要永久地将它放在后台。

consumers_controller.rb

require 'kafka'

class ConsumersController < ApplicationController

  def fetch

    @consumer = Kafka::Consumer.new( { :host => ENV["host"],
        :port => ENV["port"],
        :topic => ENV["topic"]})

    @consumer.loop do |message|
      logger.info "-------------#{message.inspect}--------------"
      logger.info "-------------#{message.first.payload.inspect}--------------"
      unless message.blank?
        ConsumerWorker.perform_async(message.first.payload)
      end
    end
  end
end

consumer_worker.rb

class ConsumerWorker

  include Sidekiq::Worker

  def perform(message)
    payload = message.first["payload"]
    hash = JSON.parse(payload)
    return @message = Message.new(hash) if hash["concern"] == 'order_create' or hash["concern"] == 'first_payment'
  end

end

message.rb

class Message

  attr_reader :bundle_id, :order_id, :order_number, :event

  def initialize(message)
    @payload = message["payload"]
    @bundle_id = @payload["bundle_id"]
    @order_id  = @payload["order_id"]
    @order_number = @payload["order_number"]
    @event = message["concern"]
  end
end

1 个答案:

答案 0 :(得分:0)

我认为你需要移动这个块

@consumer.loop do |message|
end

在你的工作人员内部以某种方式,因为我认为消费是在块执行后完成的。