简体   繁体   English

在涉及数据库访问的Rails上调度ruby

[英]Scheduling in ruby on rails involving database access

I want to schedule daily reports to subscribed users via email. 我想安排每日报告通过电子邮件发送给订阅的用户。
For that I have written action in reports_controller that fetch data from database & convert it into pdf using pdfkit/wkhtmltopdf. 为此,我在reports_controller中编写了操作,该操作从数据库中获取数据并使用pdfkit / wkhtmltopdf将其转换为pdf。
The action works fine when called from get request.But when converted so that be defined like 从get请求中调用该动作时效果很好,但是在转换时这样定义

def self.dailymail
   ac = ActionController::Base.new()
   kit = PDFKit.new        #retrieve data from db  
   pdf = kit.to_pdf
   ReportMailer.send_reports(ac.send_data(pdf)).deliver
 end

It raises exception at send_data call when used with rufus scheduler: rufus调度程序一起使用时,它会在send_data调用时引发异常:

RackDelegation#content_type= delegated to @_response.content_type=, but @_response is nil: #<ActionController::Base:0x206b068 @_routes=nil, @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, ...


so, my question is what how can I solve this problem or Is there any alternate scheduler in rails that work fair on both Windows and Linux? 所以,我的问题是我该如何解决这个问题?或者在Rails中是否有其他调度程序可以在Windows和Linux上正常使用?
I wish to know any scheduler that can be helpful to send reports fetched from database. 我希望知道任何有助于发送从数据库获取的报告的调度程序

As willglynn already points out, you should get rid of any controller interaction. 正如willglynn已经指出的那样,您应该摆脱任何控制器交互。 There's simply no need here and it makes things unnecessarily complicated. 这里根本没有必要,这会使事情变得不必要地复杂。 So your code should look more like 所以你的代码应该看起来更像

def self.dailymail
   kit = PDFKit.new        #retrieve data from db  
   pdf = kit.to_pdf
   ReportMailer.send_reports(pdf).deliver
end

If you got problems with the rufus scheduler (which I don't know), you could create a rake task to send out your mails and use the OS scheduler (eg cron on Linux) to call the task. 如果您对rufus调度程序有问题(我不知道),则可以创建一个rake任务来发送邮件,并使用OS调度程序(例如Linux上的cron)来调用该任务。 Having the rake task would be also convenient for testing. 进行rake任务也将便于测试。

I agree with claasz regarding the rake task. 对于耙任务,我同意claasz的观点。 Check out the whenever gem https://github.com/javan/whenever There is no suport for windows Task Scheduler, but it does support creating cron jobs. 随时查看https://github.com/javan/whenever。Windows Task Scheduler没有支持,但它确实支持创建cron作业。

Check out the documentation for the details, but esentially the gem creates cron jobs based on what you configure in the schedule.rb file that is created when you install the gem. 请查看文档以获取详细信息,但从本质上讲,gem根据您在安装gem时创建的schedule.rb文件中配置的内容创建cron作业。

sample content of schedule.rb: schedule.rb的样本内容:

every 3.hours do
  runner "MyModel.some_process"
  rake "my:rake:task"
  command "/usr/bin/my_great_command"
end

This would be like running bundle exec rake my:rake:task every 3 hours 这就像每3个小时运行一次bundle exec rake my:rake:task

After creating the schedule.rb you will need to run the whenever command from the console in order to add your schedule to cron. 创建schedule.rb之后,您需要从控制台运行everyever命令,以便将计划添加到cron。 If you run whenever without arguments, the output shows you the contents of the schedule.rb. 如果您在没有参数的情况下运行,则输出将显示schedule.rb的内容。 There is an argument you need to provide that I can't remember off the top of my head, just pass --help and I think you'll get the answer. 您需要提供一个论据,我不记得了,只需传递--help,我想您就会得到答案。

Hope this helps 希望这可以帮助

EDIT:The argument is -w to write to cron-tab 编辑:参数是-w写入cron-tab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM