-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart.py
44 lines (35 loc) · 1.05 KB
/
restart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Functions to handle redeployment of the bot on Heroku.
"""
import os
import psycopg2
import datetime
import subprocess
def clear_tables() -> None:
# Clears all the data within all the tables in Heroku.
DATABASE_URL = os.environ["DATABASE_URL"]
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
try:
cur.execute(
"TRUNCATE TABLE credentials, invalid_sheets, default_links RESTART IDENTITY CASCADE;"
)
conn.commit()
print("Tables cleared successfully.")
except Exception as e:
print("Failed to clear tables:", e)
conn.rollback()
finally:
cur.close()
conn.close()
def restart_heroku() -> None:
# Redeploys the app in Heroku.
app_name = os.environ["HEROKU_APP_NAME"].strip("'")
subprocess.run(["heroku", "ps:restart", "--app", app_name], check=True)
def main() -> None:
# Initiates redeployment once a week.
if datetime.datetime.now().weekday() == 6:
clear_tables()
restart_heroku()
if __name__ == "__main__":
main()