/HerokuDynoSwitcher

A little script and trick to make your heroku app run forever without being concerned about dyno hours.

Primary LanguagePython

Heroku Dyno Switcher

📋 Description

A little python project to make your heroku app alive forever without being concerned about dyno hours. You do not need to bother adding a credit card to get more dyno hours.

ℹ️ I know this is just a petty trick, but at least, it's useful for me. If you use another service than heroku, or you have credit cards to get your heroku accounts verified, or you don't like what I'm doing, you may stop reading this and leave, thank you.

⚙️ Mechanism

The main idea is to use two accounts (yeah, you need 2 of them to make this work) with two same apps and shift the dyno every 1st and 15th of a month.

There are two methods you can use, No loop and Autoloop.

I recommend you to use no loop method with Github Actions because it's less complicated and less obstructive.

No loop method

There are two Github Actions workflows that have been set with cron jobs. They will run automatically at 00:00 UTC 1st and 15th of a month and when they're running, they will switch the dynos respectively.

Autoloop method

Every 10 mins, the script will check if today is either 1st or 15th of a month. If it's true, then it'll switch the dynos respectively.

⚠️ Drawbacks

  • This may make your app restart once more at 1st and 15th of the month though (besides the usual 24h restart in heroku free tier).
  • You need to deploy to both apps at first, and whenever there's a change you want to make to the app, you need to deploy to both apps too. Patience is really needed for that.
  • Only for autoloop method: The primary app's dyno will be automatically activated every 10 minutes for the entire day of 1st of a month and the secondary app's dyno will be automatically activated every 10 minutes for the entire day of 15th of a month. That will be bad for you when you don't want to activate the dynos at those certain days. Deactivate this script first before doing that.
  • Heroku API will be called more at 1st and 15th of a month with this script. At least, there'll be 18 API calls per hour per acc. It's not that many and it's acceptable, but please note this if you are planning to use heroku API calls.

👩🏻‍🤝‍👨🏼 Alternative

Someone already made a similar concept and it's easier to apply than this script.
https://heroku.viperadnan.gq/duo

You can just add your app names and heroku API keys there, but use it with your own risk.

You may use this script if you don't trust the site your credentials, as this repo is open source and you can see what's written in the code.

🚀 Deployment

Before you use this, know that problems may occur whenever because of my poor coding skill, so please, if you have time, check my code in script.py for Autoloop method and script_noloop.py for No loop method, report any error in Issues, and do pull request (or just make your own fork with your own features if it's personal). That will be highly appriciated.

What to do first?

You need to deploy two similar apps to two heroku accounts. If the day you deploy this script is under 15th (in UTC), only enable the dyno of the app in the first acc. If it's 15th or more than that (in UTC), only enable the dyno of the app in the second acc. Otherwise, the script won't run well in the first month (the next month will be adjusted correctly automatically).

Where to deploy this?

Platform Method Deployment Notes
Github Actions
(Recommended)
No Loop Click here Recommended deployment platform as "No loop" method won't be intrusive.
Heroku Autoloop Deploy to Heroku • You can deploy this script to another Heroku account.
• Unverified Heroku account works too, no need for 1000 dyno hours as this script will only run effectively on 1st (when dyno hours reset) and 15th of a month.
• Add additional variables later by yourself.
Railway Autoloop 1. Fork this repo
2. Set the variables
3. Connect the repo to Railway via Github deployment
Another free PaaS like Heroku with always-on apps feature.

🥪 Variables

Required Variables

  • FIRST_PROCESSTYPE is the process type of your app, you can use web, worker or something else. Check it in the Resources tab of your heroku app.
  • FIRST_A_APPNAME is the app name of your primary app.
  • FIRST_A_APIKEY is the API key of the heroku account your primary app is in. Check it in your heroku settings, in the last of the page.
  • FIRST_B_APPNAME is the app name of your secondary app.
  • FIRST_B_APIKEY is the API key of the heroku account your secondary app is in. Check it in your heroku settings, in the last of the page.

Optional Variables

The script supports up to 5 pair of apps simultaneously. You may add these vars if you need it.
SECOND_PROCESSTYPE, SECOND_A_APPNAME, SECOND_A_APIKEY, SECOND_B_APPNAME, SECOND_B_APIKEY
THIRD_PROCESSTYPE, THIRD_A_APPNAME, THIRD_A_APIKEY, THIRD_B_APPNAME, THIRD_B_APIKEY
FOURTH_PROCESSTYPE, FOURTH_A_APPNAME, FOURTH_A_APIKEY, FOURTH_B_APPNAME, FOURTH_B_APIKEY
FIFTH_PROCESSTYPE, FIFTH_A_APPNAME, FIFTH_A_APIKEY, FIFTH_B_APPNAME, FIFTH_B_APIKEY

If you need more than 5 apps, you can extend the code as much as you want, or if you don't know how to do it, just deploy more of this script, it works too.

📝 Credits and References