Resque 作业无法连接到外部主机
Resque jobs can't connect to outside hosts
在我的 Rails 应用程序中,我设置了 ActiveJob 以与 Resque 一起使用。我创建了这个作业来发送 iOS 推送通知 grocer
gem:
class SendPushNotificationJob < ActiveJob::Base
queue_as :default
def perform(device_token, msg)
# TODO: Move the grocer initialization somewhere else.
pusher = Grocer.pusher(
certificate: ENV['PUSH_CERT_PATH'],
passphrase: ENV['PUSH_CERT_KEY'],
gateway: "gateway.sandbox.push.apple.com",
port: 2195,
retries: 3
)
Resque.logger.debug "PUSHER OBJECT IS #{pusher.inspect}!"
notification = Grocer::Notification.new(
device_token: device_token,
alert: msg)
Resque.logger.debug "NOTIFICATION OBJECT IS #{notification.inspect}"
res = pusher.push(notification)
Resque.logger.debug "PUSH RESULT IS #{res.inspect}"
end
end
拾取此作业时 Resque 日志的输出为:
** [22:08:31 2016-07-06] 24074: Found job on default
** [22:08:31 2016-07-06] 24074: got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])
** [22:08:31 2016-07-06] 24074: resque-1.26.0: Processing default since 1467860911 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [22:08:31 2016-07-06] 24074: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])]
** [22:08:31 2016-07-06] 24074: resque-1.26.0: Forked 24092 at 1467860911
** [22:08:31 2016-07-06] 24092: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])]
** [22:08:31 2016-07-06] 24092: PUSHER OBJECT IS #<Grocer::Pusher:0x007f862152f4f0 @connection=#<Grocer::Connection:0x007f862152f630 @certificate="<cert path>", @passphrase="<passphrase>", @gateway="gateway.sandbox.push.apple.com", @port=2195, @retries=3>>!
** [22:08:31 2016-07-06] 24092: NOTIFICATION OBJECT IS #<Grocer::Notification:0x007f8627a23f00 @identifier=0, @device_token="<my_device_token>", @alert="Test message", @encoded_payload=nil>
然后出现异常:
** [22:08:31 2016-07-06] 24092: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}]) failed: #<SocketError: getaddrinfo: nodename nor servname provided, or not known>
这是我所知道的:
- 网关 URL 设置正确。
- 环境变量设置正确。
- 此作业在使用
perform_now
而不是 perform_later
时有效。
最后,这不是 grocer
的问题。我创建了另一个作业,它只记录 URL:
的来源
class TestExternalConnectionJob < ActiveJob::Base
queue_as :default
def perform(url)
res = HTTParty.get(url)
if res
Resque.logger.debug "SUCCESS"
Resque.logger.debug res.body
else
Resque.logger.debug "FAILURE"
end
end
end
运行 TestExternalConnectionJob.perform_later('http://whosebug.com')
在 Rails 控制台中在 Resque 日志中产生此输出:
** [22:14:41 2016-07-06] 24285: Found job on default
** [22:14:41 2016-07-06] 24285: got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])
** [22:14:41 2016-07-06] 24285: resque-1.26.0: Processing default since 1467861281 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [22:14:41 2016-07-06] 24285: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])]
** [22:14:41 2016-07-06] 24285: resque-1.26.0: Forked 24300 at 1467861281
** [22:14:41 2016-07-06] 24300: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])]
** [22:14:41 2016-07-06] 24300: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}]) failed: #<SocketError: Failed to open TCP connection to whosebug.com:80 (getaddrinfo: nodename nor servname provided, or not known)>
因此,我的 tl;dr、none 个 Resque 作业能够连接到外部服务。
附录
我的 lib/resque.rake
文件:
require 'resque/tasks'
namespace :resque do
task :setup => :environment do
require 'resque'
end
end
我的 config/initializers/resque.rb
文件:
require 'resque'
Resque.redis = Redis.new(url: 'redis://127.0.0.1:6379')
Resque.logger = Logger.new(Rails.root.join('log', "#{Rails.env}_resque.log"))
Resque.logger.level = Logger::DEBUG
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
好吧,写那个问题 浪费了很多时间。
出于某种原因,在重新启动并重新启动我的所有进程后,我能够连接到外部主机。
以前,我在 没有 重新启动的情况下多次重新启动进程。我正在使用 eye
来管理它们,因此在尝试解决此问题时我多次发出 eye restart
命令以重新启动我的应用程序所依赖的所有进程(postgres
、redis
和 resque-worker
)。每次重启后我都会得到同样的错误。
但不知何故,重启成功了。
电脑有时候很奇怪。
在我的 Rails 应用程序中,我设置了 ActiveJob 以与 Resque 一起使用。我创建了这个作业来发送 iOS 推送通知 grocer
gem:
class SendPushNotificationJob < ActiveJob::Base
queue_as :default
def perform(device_token, msg)
# TODO: Move the grocer initialization somewhere else.
pusher = Grocer.pusher(
certificate: ENV['PUSH_CERT_PATH'],
passphrase: ENV['PUSH_CERT_KEY'],
gateway: "gateway.sandbox.push.apple.com",
port: 2195,
retries: 3
)
Resque.logger.debug "PUSHER OBJECT IS #{pusher.inspect}!"
notification = Grocer::Notification.new(
device_token: device_token,
alert: msg)
Resque.logger.debug "NOTIFICATION OBJECT IS #{notification.inspect}"
res = pusher.push(notification)
Resque.logger.debug "PUSH RESULT IS #{res.inspect}"
end
end
拾取此作业时 Resque 日志的输出为:
** [22:08:31 2016-07-06] 24074: Found job on default
** [22:08:31 2016-07-06] 24074: got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])
** [22:08:31 2016-07-06] 24074: resque-1.26.0: Processing default since 1467860911 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [22:08:31 2016-07-06] 24074: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])]
** [22:08:31 2016-07-06] 24074: resque-1.26.0: Forked 24092 at 1467860911
** [22:08:31 2016-07-06] 24092: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}])]
** [22:08:31 2016-07-06] 24092: PUSHER OBJECT IS #<Grocer::Pusher:0x007f862152f4f0 @connection=#<Grocer::Connection:0x007f862152f630 @certificate="<cert path>", @passphrase="<passphrase>", @gateway="gateway.sandbox.push.apple.com", @port=2195, @retries=3>>!
** [22:08:31 2016-07-06] 24092: NOTIFICATION OBJECT IS #<Grocer::Notification:0x007f8627a23f00 @identifier=0, @device_token="<my_device_token>", @alert="Test message", @encoded_payload=nil>
然后出现异常:
** [22:08:31 2016-07-06] 24092: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"SendPushNotificationJob", "job_id"=>"c88619a4-d2ea-4b27-a3c8-3ea5be04a130", "queue_name"=>"default", "arguments"=>["<my_device_token>", "Test message"], "locale"=>"en"}]) failed: #<SocketError: getaddrinfo: nodename nor servname provided, or not known>
这是我所知道的:
- 网关 URL 设置正确。
- 环境变量设置正确。
- 此作业在使用
perform_now
而不是perform_later
时有效。
最后,这不是 grocer
的问题。我创建了另一个作业,它只记录 URL:
class TestExternalConnectionJob < ActiveJob::Base
queue_as :default
def perform(url)
res = HTTParty.get(url)
if res
Resque.logger.debug "SUCCESS"
Resque.logger.debug res.body
else
Resque.logger.debug "FAILURE"
end
end
end
运行 TestExternalConnectionJob.perform_later('http://whosebug.com')
在 Rails 控制台中在 Resque 日志中产生此输出:
** [22:14:41 2016-07-06] 24285: Found job on default
** [22:14:41 2016-07-06] 24285: got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])
** [22:14:41 2016-07-06] 24285: resque-1.26.0: Processing default since 1467861281 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [22:14:41 2016-07-06] 24285: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])]
** [22:14:41 2016-07-06] 24285: resque-1.26.0: Forked 24300 at 1467861281
** [22:14:41 2016-07-06] 24300: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}])]
** [22:14:41 2016-07-06] 24300: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | [{"job_class"=>"TestExternalConnectionJob", "job_id"=>"523da5ca-2b6c-452e-95ef-33edece69996", "queue_name"=>"default", "arguments"=>["http://whosebug.com"], "locale"=>"en"}]) failed: #<SocketError: Failed to open TCP connection to whosebug.com:80 (getaddrinfo: nodename nor servname provided, or not known)>
因此,我的 tl;dr、none 个 Resque 作业能够连接到外部服务。
附录
我的 lib/resque.rake
文件:
require 'resque/tasks'
namespace :resque do
task :setup => :environment do
require 'resque'
end
end
我的 config/initializers/resque.rb
文件:
require 'resque'
Resque.redis = Redis.new(url: 'redis://127.0.0.1:6379')
Resque.logger = Logger.new(Rails.root.join('log', "#{Rails.env}_resque.log"))
Resque.logger.level = Logger::DEBUG
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
好吧,写那个问题 浪费了很多时间。
出于某种原因,在重新启动并重新启动我的所有进程后,我能够连接到外部主机。
以前,我在 没有 重新启动的情况下多次重新启动进程。我正在使用 eye
来管理它们,因此在尝试解决此问题时我多次发出 eye restart
命令以重新启动我的应用程序所依赖的所有进程(postgres
、redis
和 resque-worker
)。每次重启后我都会得到同样的错误。
但不知何故,重启成功了。
电脑有时候很奇怪。