Running Cron Tasks on Heroku

前端 未结 6 736
情书的邮戳
情书的邮戳 2020-12-05 03:06

I\'ve seen that Heroku charges $15/mo to run Delayed Job, and $3/mo to run cron tasks daily. Is it possible to skip that entirely and run my own cron tasks manually? Or ar

相关标签:
6条回答
  • 2020-12-05 03:11

    If you want to have scheduled jobs you can also use http://guardiano.getpeople.in that is a free service (for 10 jobs) for job scheduling.

    You just need to setup an HTTP endpoint in your application to recieve event notifications on POST or GET and you can also set some additional params to prevent not authorized actions.

    So you set a job in Guardiano that will cal http://yourapp.com/youraction and leave "minutes" blank if you want you action to run once in the future or set minutes to what you want to run your action every X minutes. In that way you only have to create your endpoint for you app and when this EP is called you execute something.

    So your app can sleep and you don't need to spend money and time setting up jobs and taking care that they are working properly.

    IMHO if you need something fast for an MVP or you need to setup a lot of jobs for different apps than a free service like that where you can actually outsource cronjobs is quite good.

    There was aslo an Heroku Addon called Temporize to do that but I'm not sure is still alive and working

    0 讨论(0)
  • 2020-12-05 03:15

    You can also just install a gem like rufus-scheduler if you're running a rails app and setup scheduling that way. I don't know if this is bad practice for some reason, but it's what I do with my app, and it seems to work fine.

    0 讨论(0)
  • 2020-12-05 03:16

    Updating the answer for 2020:

    1. You can use Heroku Scheduler which is Heroku's own add-on that lets you schedule commands using one-off dynos (so that you only pay for the run time of your jobs). The add-on itself is free, but doesn't really allow you to use cron but rather plain frequency: every day, every hour or every 10 minutes. Also, there's no guarantee that your job will execute at the scheduled time or at all..

    2. There are other 3rd party add-ons that can help you run one-off dynos using cron expressions for better flexibility and are more resilient than Heroku Scheduler (proper disclosure, my company is the creator of one such add-on).

    3. You can also use custom clock process (see here for more info) which essentially means that you have one dyno or process spawn tasks that run on other dynos. This usually costs more than using the aforementioned add-ons, but you have more granular control over your processes and since you only rely on Heroku, it may be more stable.

    0 讨论(0)
  • 2020-12-05 03:21

    Yes, I've successfully used a cron job on my local server which essentially runs

    $ heroku rake <rake task>
    

    at whatever intervals I've required. I've used in on both aspen and bamboo stacks.

    0 讨论(0)
  • 2020-12-05 03:22

    If you install the Heroku gem on your computer, you can then run your cron tasks manually as follows:

    $ heroku rake cron
    (in /disk1/home/slugs/xxxxxx_aa515b2_6c4f/mnt)
    Running cron at 2010/04/25 10:28:54...
    

    This will execute the exact same code as Heroku's daily/hourly cron add-on does; that is, for this to work, your application must have a Rakefile with a cron task, for example:

    desc "Runs cron maintenance tasks."
    task :cron do
      puts "Running cron at #{Time.now.strftime('%Y/%m/%d %H:%M:%S')}..."
      # TODO: your cron code goes here
    end
    

    Now, just add the heroku rake cron command to a crontab on any Unix server of yours, or even directly to your personal computer's crontab if you're running Linux or Mac OS X, and you can be scheduling cron jobs for your Heroku application as you please and without being charged for it.

    0 讨论(0)
  • 2020-12-05 03:27

    I'm not entirely sure what you mean by "run my own cron tasks manually". For cron specifically, you need access to crontab, which they can control, as they're their servers. If you have another way of doing it, it would probably be fine, but bear in mind that your app is not tied to a specific server when running under Heroku, and that the server will change between executions.

    Also, unless they've changed it since last time I checked, you can run daily cron tasks for free, but hourly costs $3/mo.

    EDIT: Yes, daily crons are free. See http://addons.heroku.com/.

    0 讨论(0)
提交回复
热议问题