我真的是Ruby的新手。我理解語言,但我很難找到正確的方式來構建項目,有很多文章和教程建議不要使用簡單的ruby
命令運行,但例如rackup
。如果我的應用程序使用多個gem,那麼我無法使用其他命令的想法,這些命令用作包裝器?SIdekiq不執行異步(Sinatra&Rack)
至於我的項目。我創建簡單的API與Sinatra
& Rack & Sidekiq
,我開始我的應用程序如下。
rackup -p1600 --host 192.168.0.130 config.ru
但我使用sidekiq
剛剛開始,它需要Redis server
我已經安裝了它,現在一切工作沒有錯誤。
但問題是我的任務沒有被處理。
這裏是我的榜樣
我的終點
post '/items' do
item_url = params[:item_url]
halt(400, {error: 'Item url is not provided'}.to_json) if item_url.nil?
begin
item_handler = ItemHandler.new item_url
item_handler.start_processing
item_handler.item_status.to_json
rescue APIErrors::AlreadyExistsError => e
halt(409, {error: e.message}.to_json)
rescue APIErrors::InvalidPayloadError => e
halt(400, {error: e.message}.to_json)
end
end
而且我ItemHandler
Sidekiq.configure_server do |config|
config.redis = {password: 'password'}
end
Sidekiq.configure_client do |config|
config.redis = {password: 'password'}
end
class ItemHandler
...
...
...
def start_processing
ItemWorker.perform_async(@item.id)
end
而且finnally ItemWorker
require 'sidekiq'
class ItemWorker
include Sidekiq::Worker
attr_accessor :item
def perform(id)
# Get item model
@item = ItemModel.where(_id: id)
logger.info "Doing hard work"
puts @item.status
# Start item processing
process_item
end
def process_item
logger.info "Doing hard work"
puts 'Start processing'
@item.status = ItemModel::STATUS[:downloading]
@item.save
result = download_item_file
if result > RESULT_OK
@item.status = ItemModel::STATUS[:failed_download]
@item.save
puts 'Failed to download file'
else
puts 'File downloaded'
@item.status = ItemModel::STATUS[:downloaded]
@item.save
end
end
def download_item_file
return if @item.nil?
dl_command = EXTERNAL_DOWNLOAD.dup
dl_command['|url|'] = @item.url
system dl_command
$?.exitstatus
end
end
並沒有什麼發生,在控制檯沒有輸出,什麼都沒有。以前我用fork
而不是sidekiq
,它工作正常。
請幫我找到問題。
P.S.如果我以錯誤的方式做其他事情(不遵循最佳做法&指南),請告訴我。
它仍然無法使用?一切似乎都很好,你可以確定在觸發該功能後我們的日誌? – amrdruid