如何在Heroku上运行Clockwork脚本?(寻求低成本解决方案)
Hey there! Since you're new to Heroku and hunting for the cheapest way to get your Clockwork script up and running, I’ve got two solid options for you—depending on what your tasks need to do.
Option 1: Use a Free Heroku Dyno (For Continuous Scheduling)
If your Clockwork script needs to run continuously (like handling precise minute-by-minute schedules or maintaining persistent state), this is the way to go. Heroku’s free dynos are perfect for testing or non-critical tasks, though they do have some limits (more on that below).
Step 1: Add a Procfile to your project
Heroku uses aProcfileto define which processes to run. Create this file in your Rails app’s root directory (no file extension!) and add this line:clock: bundle exec clockwork clock.rbThis tells Heroku to spin up a "clock" process that executes your script.
Step 2: Push your code to Heroku
Commit the Procfile to Git and push it up:git add Procfile git commit -m "Add clock process definition" git push heroku mainStep 3: Start the clock dyno
Run this command to launch one free dyno for your clock process:heroku ps:scale clock=1A quick heads-up: Free dynos sleep for 6 hours every day, and each account gets 550 free dyno hours per month. That means if you run this clock dyno 24/7, it’ll use up all your free hours after ~23 days. For non-critical tasks, this is totally fine—but if you need 24/7 uptime, you’ll have to upgrade to a Hobby dyno ($7/month), which is still pretty affordable.
Option 2: Use Heroku Scheduler (100% Free, For Periodic Tasks)
If your tasks are simple and run on fixed intervals (like hourly, daily, or weekly), Heroku Scheduler is the cheapest pick—it’s completely free, with up to 100 task runs per month. No need to keep a dyno running 24/7!
Step 1: Convert your Clockwork logic to a Rake task
Move your task code into a Rake task so Scheduler can trigger it. Create a file atlib/tasks/clock_tasks.rakewith something like this:namespace :clock do desc "Execute my scheduled task" task run_job: :environment do # Paste your Clockwork task logic here # Example: User.send_reminders puts "Scheduled task executed successfully!" end endTest it locally first with
bundle exec rake clock:run_jobto make sure it works.Step 2: Deploy the Rake task to Heroku
Commit the file and push to Heroku:git add lib/tasks/clock_tasks.rake git commit -m "Add scheduled rake task" git push heroku mainStep 3: Set up Scheduler
- Go to your app’s dashboard on Heroku, navigate to the Resources tab.
- Search for "Scheduler" in the add-ons section and add it (it’s free).
- Open the Scheduler dashboard, click Add job.
- In the "Run command" field, enter
rake clock:run_job. - Choose your desired frequency (Every hour, Every day, etc.) and save.
Which Option Should You Pick?
- Go with Option 1 if you need precise, flexible scheduling (like tasks running every 15 minutes) or your script needs to maintain state while running.
- Go with Option 2 if your tasks are straightforward and run on fixed, less frequent intervals—it’s 100% free and doesn’t tie up your dyno hours.
内容的提问来源于stack exchange,提问作者jorgeregidor

