2015-10-05 43 views
1

首頁控制器:Sidekiq導軌不執行工作

class HomeController < ApplicationController 


    def index 
     if request.post? 
      @value = (params[:value] || 10).to_i 
      # Perform the calculation of the fibonnaci in background 
      puts('starting job...') 
      TestJob.perform_async("Luis",@value) 
     end 

     # Retrieve from Redis the last 10 calculations 
     @bottom = redis.lrange("calcs",-10,-1).reverse 
    end 

    private 
    def redis 
    @redis ||= Redis.new 
    end 
end 

HTML ERB文件(查看)

Fibonnaci calculator (try a few between 30 and 40) 
<%= form_tag(home_index_path) do %> 
    <%= text_field_tag(:value,@value) %> 
    <%= submit_tag(:calculate) %> 
<% end %> 

Past completed calculations 
<ul> 
    <% @bottom.each do |value| %> 
    <li><%= value %></li> 
    <% end %> 
</ul> 

TestJob類

class TestJob 
    include Sidekiq::Worker 

    # Calculate the nth fibonnaci sequence number 
    def fib(x) 
    return 0 if x < 1 
    x < 3 ? 1 : fib(x-1) + fib(x-2) 
    end 

    # Sidekiq has the same interface as resque, so we can call this method asynchronously by using SlowJob.perform_async 
    # When #perform_async is called the arguments are serialized to a Redis queue and later our background job processor 
    # will dequeue them and call the #perform method below with them 
    def perform(name, number) 
    time = Time.now 
    # Recursive Fibonnaci of larger than 30 numbers can take a while 
    result = fib(number) 
    duration = Time.now - time 

    text = "Dear #{name} the Fibonacci value of #{number} is #{result} (#{"%.2f" % duration }) #{time.strftime("%F %T")}" 

    # Put the results in a redis list so we can see them on the web interface at each new request 
    redis.rpush "calcs", text 
    end 

    # Create a redis client 
    def redis 
    @redis ||= Redis.new 
    end 
end 

所以,當我提交表單的HTML,forexample我把5放在表單然後發送請求被成功發送,我看到絕對沒有錯誤。

我看到這個輸出控制檯:

Started POST "/home/index" for 127.0.0.1 at 2015-10-05 17:23:35 +0530 
Processing by HomeController#index as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"cgQMV3sCP91zaeiPfYVC5mxfBZpDmgKgRZ7qe5PWCdqGUX59v8I4BZoKXbdYlqfsLyp6/cz8XY/3cEXe/mKh4A==", "value"=>"5", "commit"=>"calculate"} 
starting job... 
    Rendered home/index.html.erb within layouts/application (0.8ms) 
Completed 200 OK in 81ms (Views: 76.1ms | ActiveRecord: 0.0ms) 

而且我沒有看到在HTML文件中任何東西。 工作沒有得到執行。

回答

1

你是否開始sidekiq工作?

bundle exec sidekiq 
+0

工作。在5分鐘內接受你的答案。 – user1735921