Twitter bot deployed with Heroku shuts down - heroku

I'm quite new to the Heroku platform, I don't understand why my Twitter bot deployed gets shut down after some time. I don't know if it's the dyno or something else.
bot.py
import time
import json
import requests
import tweepy
from os import environ
consumer_key = environ['api_key'] #API key
consumer_secret = environ['api_key_secret'] #API key scret
key = environ['access_token'] #Access token
secret = environ['access_token_secret'] #Access token secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # Authentication
auth.set_access_token(key, secret) # Grant access to API
api = tweepy.API(auth) # Connect to API
def get_quote():
url = 'https://programming-quotes-api.herokuapp.com/Quotes/random'
response = requests.get(url)
data = json.loads(response.text)
data = data['en']+'\n--'+data['author']
return data
def tweet_quote():
interval = 60 * 20 # 20 minutes
while True:
quote = get_quote()
api.update_status(quote)
time.sleep(interval)
if __name__ == "__main__":
tweet_quote()
Procfile
web: python server.py
worker: python bot.py
dyno info here

Your Procfile shows that you have a web process and a worker process.
Assuming you are using free dynos, this behaviour is expected (bold added):
If an app has a free web dyno, and that dyno receives no web traffic in a 30-minute period, it will sleep. In addition to the web dyno sleeping, the worker dyno (if present) will also sleep.
Free worker dynos on apps that do not have a web dyno do not sleep, but of course they'll consume roughly 720 free dyno hours every month (24 hours per day × 30 days in the month).
You have a few options:
Run your worker regularly (but not constantly) via the Heroku Scheduler
E.g. maybe run it once an hour for a limited amount of time / tweets
Upgrade to paid dynos
Move your free worker dyno to another app that has no web dynos to prevent it from sleeping...
...though that might not make a lot of sense for your application
Keep your web dyno alive by pinging it at least once every 30 minutes...
...but then you'll run out of free dyno hours roughly 20 days into each month (or sooner, if your account is not verified)

Related

Worker Dyno go to sleep with the Web dyno

I'm using heroku to host my discord bot
My bot is mostly designed to be used with the discord chats, but it have a web dashboard for when in need too. I put the bot on heroku, everything seems to be working well, I know about the 30 minutes sleep time for web dyno, and it was fine, since the web dashboard is only used every so often, not 24/24 like the bot. The issue here is, when the web dyno go offline, it also brought the worker dyno down too, which is not what I wanted. Although the documents say that "Worker Dynos do not sleep, since they don't handle web requests", clearly that my worker dyno is sleeping with the web dyno. I don't want to use pinging services, since my 1000 free hours is not enough for both dynos to stay online. Is there a workaround for this? Thanks

will heroku app go to sleep lead to data lose in the redis addon

I build a Flask app in heroku using a free dyno and a free redis addon.
What I noticed is that my data in redis addon will get lost after some period of time.
My question is this: What is the cause of this? To avoid this loss, should I upgrade the dyno or the redis addon?
my server.py:
app = Flask(__name__)
redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
redis = redis.from_url(redis_url)
if __name__ == "__main__":
app.run(host='0.0.0.0')

Heroku, schedule new Dyno sleeptime

With the announcement of the new Dyno pricing on Heroku I had some questions about the mechanics of the new free Dyno.
I was wondering if it was possible to schedule sleep time for a Dyno. This way I could make sure that my application is down while most of my users are sleeping.
No, this is not possible. Your app will automatically sleep when it doesn't get any requests for 30 minutes, and will be awaken if a request comes in.
If you have exceeded your daily quota, the app will just not be awaken.

Heroku free quota per-app or total?

Is heroku's free 750 hours separate per app, or is it a total of 750 hours shared across all your apps?
From their site:
"Each app you create has free access to 750 dyno-hours per month and a starter-tier database."
However, from another answer on StackOverflow:
"Heroku provides, for free, 1 dyno. A dyno is an instance of your application running and responding to requests. If each instance of your application can serve each request in 100ms, then you get 600 requests/minute with the free account."
Based off of what it says there and my experience it's per app. That's talking about instances of dynos not apps. "If each instance of your application can serve each request in 100ms, then you get 600 requests/minute with the free account."
Each app you create within Heroku gets 750 dyno-hours per month for free. The number of requests your app can receive depend on the configuration of the app. For example, an app running unicorn can handle more requests than an app running something else, as unicorn can run multiple workers per dyno.
I've personally run an app on Heroku with 3 unicorn workers on one dyno, 24 hours a day, all month, and always been free (because that is ~750 dyno-hours).

Heroku: How can I dynamically scale dynos based on load time?

There are random times throughout the day that my app can have pretty extreme increases in load based on various factors.
At those times, I'd like to automatically increase the number of dynos.
I'd like to base the increase on the load time. So if it's taking X amount of time for pages to load, increase dynos. Otherwise, go back down.
Anything like that exist?
HireFire is able to auto-scale both your web- and worker dynos based on various metrics. We currently support the following metric sources/types:
HireFire (Response Time) | Web Dynos
HireFire (Job Queue) | Worker Dynos
Heroku Logplex (Response Time) | Web Dynos
Heroku Logplex (Requests Per Minute) | Web Dynos
Heroku Logplex (Dyno CPU Load) | Web Dynos
NewRelic (Apdex) | Web Dynos
NewRelic (Response Time) | Web Dynos
NewRelic (Requests Per Minute) | Web Dynos
HireFire (Response Time) performs a basic HTTP request to measure response times.
HireFire (Job Queue) allows you to auto-scale your worker dynos based on their queue-sizes. You'll setup a very simple endpoint on your end (we have a RubyGem for this, but it can be done in any language with any library very easily). We'll periodically perform checkups and scale your worker dyno formation accordingly.
New Relic allows you to integrate with New Relic. It'll periodically fetch up-to-date metric data such as Average Response Time, Requests Per Minute and Apdex to determine your web dyno formation.
Heroku Logplex is our latest addition. This approach relies on your (and Heroku's) logs. Your logs will be streamed from Heroku's Logplex to HireFire's Logdrain, where it will be parsed for metric data. This allows you to auto-scale your web dynos in a more reliable way than with HireFire/ResponseTime as it grabs data directly from Heroku's Router. This approach doesn't require any external dependencies such as New Relic either.
Get in touch if you have any questions!
There is a new plug and play addon that just launched for this: https://addons.heroku.com/adept-scale
Seems like heroku-autoscale is a good bet.

Resources