-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.py
executable file
·67 lines (46 loc) · 1.73 KB
/
cron.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# 59 22 * * * /usr/bin/python3 /path/to/your_script.py << 11:59pm
import asyncio
import os
import datetime
from dotenv import load_dotenv
from utils.email import send_email
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR) # Set the log level to ERROR or the desired level
current_date = datetime.datetime.now()
current_date_str = current_date.strftime("%d-%m-%Y")
async def main():
load_dotenv(".env")
directory = os.getenv("directory")
# Email values
SENDER_EMAIL = os.getenv('sender_email')
PASSWORD = os.getenv('sender_password')
if directory is not None:
os.chdir(directory)
else:
logger.error("Error: Invalid directory path.")
return
process = await asyncio.create_subprocess_shell(
"python3 main.py",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
output, _ = await process.communicate()
output = output.decode()
# Send email with the output
from utils.data_json import usersd_list
for i in usersd_list("emails"):
await send_email(output, SENDER_EMAIL, PASSWORD, i)
output_dir = os.getenv("output_dir")
output_file = os.path.join(output_dir, f"output_{current_date_str}.html")
with open(output_file, "w") as file:
file.write(output)
os.chmod(output_file, 0o644)
if __name__ == "__main__":
try:
log_file = "error.log" # Specify the desired log file path
logging.basicConfig(filename=log_file, level=logging.ERROR) # Configure logging to write ERROR level messages to the file
asyncio.run(main())
except Exception as e:
logger.error(f"An error occurred: {e}")