-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
76 lines (74 loc) · 3.86 KB
/
Copy pathexecutor.py
File metadata and controls
76 lines (74 loc) · 3.86 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os, sqlite3
from time import sleep
from datetime import datetime
table_name = "task_table"
def run_executor():
# connect to the SQLite database
with sqlite3.connect('task_db.db') as connection:
cursor = connection.cursor() #create the cursor object
while True: #infinite loop to check for tasks
sleep(2) #sleep for 2 second, then check for tasks
query_str = f"SELECT * FROM {table_name}"
cursor.execute(query_str)
rows = cursor.fetchall()
for row in rows: #row is a tuple
# check the time and compare it to when the task should run
now = datetime.now()
if row[2] == "date":
# Execute the command if the date is less than or equal to now
if datetime.fromisoformat(row[2]) <= now:
command = row[1]
try:
os.system(command)
#remove task from DB
delete_str = f"DELETE FROM {table_name} WHERE command='{command}'"
cursor.execute(delete_str)
connection.commit()
except Exception as e:
print(f"Error executing command {command}: {e}")
elif row[2] == "interval":
# Execute the command if the the command has not been run in the last interval seconds
# if never run, then run
if row[5] is None:
command = row[1]
try:
os.system(command)
#set last run to now
update_str = f"UPDATE {table_name} SET last_run='{now}' WHERE command='{command}'"
cursor.execute(update_str)
connection.commit()
except Exception as e:
print(f"Error executing command {command}: {e}")
#check how long its been since the last run
else:
td = now - datetime.fromisoformat(row[5])
total_seconds_since_last_run = int(td.total_seconds())
if row[4] <= total_seconds_since_last_run:
command = row[1]
try:
os.system(command)
#set last run to now
update_str = f"UPDATE {table_name} SET last_run='{now}' WHERE command='{command}'"
cursor.execute(update_str)
connection.commit()
except Exception as e:
print(f"Error executing command {command}: {e}")
elif row[2] == "cron":
#run daily at the minute, hour, second passed
# Extract hour, minute, and second
current_hour = now.hour
current_minute = now.minute
current_second = now.second
if (current_hour == row[6] and current_minute == row[7] and current_second == row[8]):
command = row[1]
try:
os.system(command)
#set last run to now
update_str = f"UPDATE {table_name} SET last_run='{now}' WHERE command='{command}'"
cursor.execute(update_str)
connection.commit()
except Exception as e:
print(f"Error executing command {command}: {e}")
else:
print(f"Invalid trigger type: {row[2]}")
continue