【发布时间】:2016-12-28 12:05:25
【问题描述】:
我真的是 Ruby 的新手。我理解这门语言,但我很难找到正确的方式来构建项目,有很多文章和教程建议不要使用简单的ruby 命令运行,而是例如rackup。如果我的应用程序使用多个 gem,我无法理解使用其他命令,它们被用作包装器?
至于我的项目。我正在使用 Sinatra 和 Rack & Sidekiq 创建简单的 API,我正在按如下方式启动我的应用程序。
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
最后是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 并且效果很好。
请帮我找出问题。
附:如果我以错误的方式做其他事情(不遵循最佳实践和指南),请告诉我。
【问题讨论】:
-
还是不行吗?一切似乎都很好,您能确定触发功能后的日志吗?
标签: ruby sinatra config rack sidekiq