如何运行“cron 脚本”在 Heroku 上通过 Rails 上的用户交互?

发布于 2024-10-18 03:48:56 字数 1558 浏览 0 评论 0原文

我有以下脚本,每天在 heroku 上的 cron 上运行一次。

但是,我意识到我希望用户能够通过按下网页上的按钮来启动相同的过程。

有没有办法创建一个 cron 可以调用或通过网络请求调用的“子例程”?我不想使用运行作业的单独服务。

我只是放了一个片段来说明......

letter_todos = Todo.current_date_lte(Date.today).asset_is("Letter").done_date_null

  unless letter_todos.blank? #check if there any ToDos

   # group by asset_id so that each batch is specific to the asset_id
   letter_todos.group_by(&:asset_id).each do |asset_id, letter_todos|
   #  pdf = Prawn::Document.new(:margin => 100) #format the PDF document
       html_file = ''
       letter_todos.each do |todo| #loop through all Letter_Todos

         contact = Contact.find(todo.contact_id) #get associated contact
         letter = Letter.find(todo.asset_id) #get associated Letter 

         redcloth_contact_letter = RedCloth.new(letter.substituted_message(contact, [])).to_html

         html_file = html_file + redcloth_contact_letter
         html_file = html_file + "<p style='display: none; page-break-after: always'><center> ... </center> </p>"

       end 

       kit = PDFKit.new(html_file)
       kit.stylesheets << "#{RAILS_ROOT}/public/stylesheets/compiled/pdf.css" 
       file = kit.to_pdf

       letter = Letter.find(asset_id)
       #OutboundMailer.deliver_pdf_email(file)
       kit.to_file("#{RAILS_ROOT}/tmp/PDF-#{letter.title}-#{Date.today}.pdf")

       # Create new BatchPrint record

       batch = BatchPrint.new
       batch.pdf = File.new("#{RAILS_ROOT}/tmp/PDF-#{letter.title}-#{Date.today}.pdf")

I have the following script which runs once a day on cron on heroku.

However, I realize that I would like the option for the user to be able to press a button from a web page to initiate this same process.

Is there a way to create a 'subroutine' that either cron can call or from a web request? I don't want to use a separate service that runs jobs.

I've just put a snippet to illustrate.....

letter_todos = Todo.current_date_lte(Date.today).asset_is("Letter").done_date_null

  unless letter_todos.blank? #check if there any ToDos

   # group by asset_id so that each batch is specific to the asset_id
   letter_todos.group_by(&:asset_id).each do |asset_id, letter_todos|
   #  pdf = Prawn::Document.new(:margin => 100) #format the PDF document
       html_file = ''
       letter_todos.each do |todo| #loop through all Letter_Todos

         contact = Contact.find(todo.contact_id) #get associated contact
         letter = Letter.find(todo.asset_id) #get associated Letter 

         redcloth_contact_letter = RedCloth.new(letter.substituted_message(contact, [])).to_html

         html_file = html_file + redcloth_contact_letter
         html_file = html_file + "<p style='display: none; page-break-after: always'><center> ... </center> </p>"

       end 

       kit = PDFKit.new(html_file)
       kit.stylesheets << "#{RAILS_ROOT}/public/stylesheets/compiled/pdf.css" 
       file = kit.to_pdf

       letter = Letter.find(asset_id)
       #OutboundMailer.deliver_pdf_email(file)
       kit.to_file("#{RAILS_ROOT}/tmp/PDF-#{letter.title}-#{Date.today}.pdf")

       # Create new BatchPrint record

       batch = BatchPrint.new
       batch.pdf = File.new("#{RAILS_ROOT}/tmp/PDF-#{letter.title}-#{Date.today}.pdf")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2024-10-25 03:48:56

我通过将相关函数放入 lib 中的文件(例如 lib/tasks_n_stuff.rb)中来完成此操作:

module TasksNStuff
    def self.do_something
        # ...doing something...
    end
end

然后我可以从 Rake 任务中调用 if:

desc 'Make sure we depend on :environment, so we can get to the Railsy stuff...'
task :do_something => :environment do
    TasksNStuff.do_something
end

或从控制器(或任何地方,实际上):

class WhateverController < ApplicationController
    def do_something
        TasksNStuff.do_something
    end
end

因为你可以将 rake 任务作为 cron 作业运行(cd /my/rails/root; rake do_something),这应该就是您所需要的。干杯!

I've done this by putting the function in question in a file in lib (lib/tasks_n_stuff.rb, say):

module TasksNStuff
    def self.do_something
        # ...doing something...
    end
end

Then I can call if from a Rake task:

desc 'Make sure we depend on :environment, so we can get to the Railsy stuff...'
task :do_something => :environment do
    TasksNStuff.do_something
end

Or from a controller (or anywhere, really):

class WhateverController < ApplicationController
    def do_something
        TasksNStuff.do_something
    end
end

And since you can run a rake task as a cron job (cd /my/rails/root; rake do_something), that should be all you need. Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文