Sidekiq rails 未执行作业
Sidekiq rails not performing job
家庭控制器:
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>
测试作业class
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,然后 post 请求成功发送,我完全没有看到任何错误。
我在控制台看到这个输出:
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 文件中没有看到任何内容。
作业未执行。
你启动sidekiq worker了吗?
bundle exec 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>
测试作业class
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,然后 post 请求成功发送,我完全没有看到任何错误。
我在控制台看到这个输出:
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 文件中没有看到任何内容。 作业未执行。
你启动sidekiq worker了吗?
bundle exec sidekiq