Reputation: 21
I built a simple python bot program which tweets out some characters every 30 minutes. Everything worked great on my local machine, using the python time function:
import time
sleep.time(1800)
I successfully got the program to tweet at exactly 30 minutes. But deploying to Heroku it never worked out ( Using a free account) initial I left the python time function to regulate the time interval of the bot but this never worked. The issues isn't it almost keeping to time like 28 minutes, 27 minutes,etc but weirdly random, like 5 minutes interval , and the next 30 minutes , then 25 minutes next.
Note: Due to Heroku sleep regulations, I changed the set time of the local machine from 30 minutes to 29 minutes.
I looked up the issue and tried to regulate it, built a clock.py program to control the Heroku dyno.
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
#execute very 30 minutes
@sched.scheduled_job('interval', minutes=29)
def scheduled_job():
#start logic
sched.start()
Still the bot keeps posting randomly and won't adhere to the the 29 minutes interval stipulated. Here is the output from the -- Heroku logs --tail
2019-11-25T13:30:40.887261+00:00 heroku[scheduler.9975]: State changed from starting to up
2019-11-25T13:30:41.883677+00:00 heroku[scheduler.9975]: State changed from up to complete
2019-11-25T13:30:41.863844+00:00 heroku[scheduler.9975]: Process exited with status 127
2019-11-25T13:30:41.812019+00:00 app[scheduler.9975]: bash: herokugitbot: command not found
2019-11-25T13:50:36.990415+00:00 heroku[clock.1]: State changed from crashed to starting
2019-11-25T13:50:42.027765+00:00 heroku[clock.1]: Starting process with command `python bot.py`
2019-11-25T13:50:42.611562+00:00 heroku[clock.1]: State changed from starting to up
2019-11-25T13:50:44.607705+00:00 heroku[clock.1]: State changed from up to crashed
2019-11-25T13:50:44.587362+00:00 heroku[clock.1]: Process exited with status 0
2019-11-25T14:01:01.725817+00:00 app[api]: Starting process with command `herokugitbot` by user [email protected]
2019-11-25T14:01:07.615698+00:00 heroku[scheduler.9046]: Starting process with command `herokugitbot`
2019-11-25T14:01:08.266222+00:00 heroku[scheduler.9046]: State changed from starting to up
2019-11-25T14:01:09.905828+00:00 heroku[scheduler.9046]: State changed from up to complete
2019-11-25T14:01:09.820645+00:00 app[scheduler.9046]: bash: herokugitbot: command not found
2019-11-25T14:01:09.886053+00:00 heroku[scheduler.9046]: Process exited with status 127
2019-11-25T14:03:16.747799+00:00 heroku[clock.1]: State changed from crashed to starting
2019-11-25T14:03:21.593237+00:00 heroku[clock.1]: Starting process with command `python bot.py`
2019-11-25T14:03:22.185303+00:00 heroku[clock.1]: State changed from starting to up
2019-11-25T14:03:24.861963+00:00 heroku[clock.1]: State changed from up to crashed
2019-11-25T14:03:24.846089+00:00 heroku[clock.1]: Process exited with status 0
How do I go about making sure it sticks to the exact 29 minutes time?
I tried Heroku scheduler but it only allow for 10minutes, 1 hour and 1 day
Upvotes: 1
Views: 814
Reputation: 1417
If you don't mind it posting exactly at 13:30, 14:00, 14:30 etc. you could do something like the following:
import time
import datetime
while True:
current_time = datetime.datetime.now()
if current_time.minute in [0, 30]:
# your function that tweets
post_tweet()
# sleep to avoid running the function again in the next loop
time.sleep(120)
If you want to tweet the first time when you start the script and then again after 30 minutes you could use something like this:
import time
import datetime
start_time = datetime.datetime.now()
interval = start_time + datetime.timedelta(minutes=30)
# dynamically create the interval times
tweet_times = [start_time.minute, interval.minute]
while True:
current_time = datetime.datetime.now()
if current_time.minute in tweet_times:
# your function that tweets
post_tweet()
# sleep to avoid running the function again in the next loop
time.sleep(120)
Upvotes: 1