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
CodePudding user response:
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 noweb
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)