Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3985596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:54:34+00:00 2026-05-20T05:54:34+00:00

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

  • 0

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")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:54:34+00:00Added an answer on May 20, 2026 at 5:54 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which will delete a file off a BlackBerry. However,
So I have a python script that runs a loop in which it calls
So I have a program that runs something like the following public class SHandler
I have the following script which runs fine but I think a) it is
I have a (PHP) script which runs on a new row in MySQL. However,
I have the following PHP script which runs nicely and inserts all the data
I have a script which runs when the system boot first time. But that
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
Currently Im have the following script which checks to see if a checkbox value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.