Ruby-on-rails 获取延迟作业以记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3500200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting delayed job to log
提问by badnaam
#Here is how I have delayed job set up.
Delayed::Worker.backend = :active_record
#Delayed::Worker.logger = Rails.logger
Delayed::Worker.logger = ActiveSupport::BufferedLogger.new("log/
##{Rails.env}_delayed_jobs.log", Rails.logger.level)
Delayed::Worker.logger.auto_flushing = 1
class Delayed::Job
def logger
Delayed::Worker.logger
end
end
if JobsCommon::check_job_exists("PeriodicJob").blank?
Delayed::Job.enqueue PeriodicJob.new(), 0, 30.seconds.from_now
end
#end
#Here is my simple job.
class PeriodicJob
def perform
Rails.logger.info "Periodic job writing #{Time.now}"
Delayed::Job.enqueue PeriodicJob.new(), 0,
30.seconds.from_now
end
end
I don't see any log messages from delayed job in my rails logs or delayed job log file, the only messages I see are jobs starting/success/failure in the delayed_jobs.log file.
我在 rails 日志或延迟作业日志文件中没有看到来自延迟作业的任何日志消息,我看到的唯一消息是 delay_jobs.log 文件中的作业开始/成功/失败。
this is causing big problems, including detecting bugs and memory leaks in workers almost impossible! Please help!
这会导致大问题,包括几乎不可能检测到工人中的错误和内存泄漏!请帮忙!
回答by Seamus Abshere
We've gotten it to work on Rails 3/Delayed Job 2.0.3 by hacking Rails.logger itself to use a different log file (the one we want for delayed_job entries) and also setting the delayed job logger to use the exact same object:
我们已经让它在 Rails 3/Delayed Job 2.0.3 上工作,通过攻击 Rails.logger 本身使用不同的日志文件(我们想要的 delay_job 条目),并将延迟作业记录器设置为使用完全相同的对象:
file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing, part #1
file_handle.sync = true
# Be paranoid about syncing, part #2
Rails.logger.auto_flushing = true
# Hack the existing Rails.logger object to use our new file handle
Rails.logger.instance_variable_set :@log, file_handle
# Calls to Rails.logger go to the same object as Delayed::Worker.logger
Delayed::Worker.logger = Rails.logger
If the above code doesn't work, try replacing Rails.loggerwith RAILS_DEFAULT_LOGGER.
如果上面的代码不能正常工作,请尝试更换Rails.logger用RAILS_DEFAULT_LOGGER。
回答by Itay k
This may be a simple workaround but it works well enough for me:
这可能是一个简单的解决方法,但对我来说效果很好:
system("echo #{your message here} >> logfile.log")
Simple but works
简单但有效
回答by Roman
I have it working with the following setup in initializer:
我让它在初始化程序中使用以下设置:
require 'delayed/worker'
Delayed::Worker.logger = Rails.logger
module Delayed
class Worker
def say_with_flushing(text, level = Logger::INFO)
if logger
say_without_flushing(text, level)
logger.flush
end
end
alias_method_chain :say, :flushing
end
end
回答by workdreamer
i simply did this:
我只是这样做:
/config/environments/development.rb
MyApp::Application.configure do
[...]
[...]
[...]
Delayed::Worker.logger = Rails.logger
end
In every next request you do the mail will be appear on the log.
在您执行的下一个请求中,邮件将出现在日志中。
NOTE: Sometimes you have to refresh the page to the mail be logged on the log. Don't forget to restart the server ;)
注意:有时您必须刷新页面以将邮件记录在日志中。不要忘记重新启动服务器;)
回答by Abdo
DelayedJob does not seem to output if there is something wrong:
如果出现问题,DelayedJob 似乎不会输出:
1- Non-active record classes need to be required and initialized:
1- 非活动记录类需要被要求和初始化:
How: Create a file config/initializers/load_classes_for_dj.rb Add to it the lines:
如何:创建一个文件 config/initializers/load_classes_for_dj.rb 向其中添加以下行:
require 'lib/libtest/delayed_test.rb'
DelayedTest
Note that if you have '#{config.root}/lib/libtest' in config.autoload_paths in config/application.rb, you do not need to do the require.
请注意,如果您在 config/application.rb 的 config.autoload_paths 中有 '#{config.root}/lib/libtest',则不需要执行该要求。
Source: Rails Delayed Job & Library Class
来源: Rails 延迟作业和库类
2- Classes that implement the Singleton module won't work by calling: SingletonClass.instance.delay.sayhello
2- 实现 Singleton 模块的类将无法通过调用工作: SingletonClass.instance.delay.sayhello
In order to fix that, do the following:
为了解决这个问题,请执行以下操作:
class SingletonClass
include Singleton
# create a class method that does the call for you
def self.delayed_sayhello
SingletonClass.instance.sayhello
end
def sayhello
# this is how you can actually write to delayed_job.log
# https://stackoverflow.com/questions/9507765/delayed-job-not-logging
Delayed::Worker.logger.add(Logger::INFO, "hello there")
end
end
In order to call the delayed class method, do the following:
为了调用延迟类方法,请执行以下操作:
SingletonClass.delay.delayed_sayhello
Yes, I am calling .delay on a class here. Since classes in Ruby are also objects, the call is valid here and allows me to access the class method "delayed_sayhello"
是的,我在这里的一堂课上调用 .delay 。由于 Ruby 中的类也是对象,调用在这里是有效的,并允许我访问类方法“delayed_sayhello”
3- Do not pass ActiveRecord objects or some complex objects to your call but rather pass ids, look up the objects in the database in your delayed method, and then do your work:
3- 不要将 ActiveRecord 对象或一些复杂的对象传递给您的调用,而是传递 id,在您的延迟方法中查找数据库中的对象,然后执行您的工作:
DO NOT DO:
不要做:
DelayedClass.new.delay.do_some_processing_on_album Album.first
DO THIS INSTEAD:
改为这样做:
DelayedClass.new.delay.do_some_processing_on_album Album.first.id
and inside DelayedClass do_some_processing_on_album , do
在 DelayedClass do_some_processing_on_album 里面,做
a = Album.find_by_id id
There was a stackoverflow post about this that I saw a while ago -- not sure which :-)
我前段时间看到了一个关于这个的stackoverflow帖子——不确定是哪个:-)
4- For completion, this is how to do mailers (do not call the deliver method):
4- 为了完成,这是如何做邮件的(不要调用传递方法):
Notifier.delay.signup(user_id)
As per 3, do not pass the user's object but rather their id, do the lookup inside the signup method.
根据第 3 条,不要传递用户的对象,而是传递他们的 id,在注册方法中进行查找。
Now once you've ensured you have followed the guidelines above, you can use:
现在,一旦您确保已遵循上述指南,您就可以使用:
Delayed::Worker.logger.add(Logger::INFO, "hello")
If you are still facing issues, I suggest you break down your problem:
如果您仍然面临问题,我建议您分解您的问题:
a- Create a new class b- Make sure you have it included and initialized as per step 1 c- Add logging from step 4 and try calling MyNewClass.new.delay to see if it works
a- 创建一个新类 b- 确保按照步骤 1 包含并初始化它 c- 从步骤 4 添加日志记录并尝试调用 MyNewClass.new.delay 以查看它是否有效
I hope this helps you guys :-)
我希望这对你们有帮助:-)
回答by Peter P.
Don't forget to change the ActiveRecord::Base.logger
不要忘记更改 ActiveRecord::Base.logger
Delayed::Worker.logger = Logger.new("log/delayed_job.log", 5, 104857600)
if caller.last =~ /script\/delayed_job/ or (File.basename(##代码##) == "rake" and ARGV[0] =~ /jobs\:work/)
ActiveRecord::Base.logger = Delayed::Worker.logger
end
More detailed answer: Have delayed_job log "puts", sql queries and jobs status

