家庭控制器:
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上提交表单时,例如我在表单中输入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文件中看不到任何内容。 工作没有完成。
答案 0 :(得分:1)
你是否开始了sidekiq工作者?
bundle exec sidekiq