- Add Logger to the script
- Use ful path for config file so that it can be available if run from cron job
This commit is contained in:
parent
8804a87b9d
commit
c039efa4bf
@ -1,10 +1,24 @@
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import logging, traceback
|
||||
|
||||
load_dotenv(dotenv_path="config.env", override=True)
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='[%(asctime)s] %(levelname)s: %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler("app.log"),
|
||||
logging.StreamHandler() # logs to console
|
||||
]
|
||||
)
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
load_dotenv(dotenv_path=f"{script_dir}/config.env", override=True)
|
||||
|
||||
base_url = os.getenv("API_BASE_URL")
|
||||
print(f"base_url: {base_url}")
|
||||
|
||||
def login():
|
||||
payload = {
|
||||
"username": os.getenv("USERNAME"),
|
||||
@ -30,13 +44,19 @@ def project_proccess(jwt):
|
||||
return response
|
||||
|
||||
try:
|
||||
logging.info("Script started")
|
||||
jwt = login()
|
||||
logging.info("Login Success")
|
||||
response = project_proccess(jwt) # Call your function
|
||||
|
||||
if response.status_code == 200:
|
||||
print("Email sent")
|
||||
logging.info("Script started")
|
||||
else:
|
||||
print(f"Failed with response: {response}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
logging.info(f"Failed with response: {response}")
|
||||
except Exception:
|
||||
msg = traceback.format_exc()
|
||||
logging.error("An error occurred:\n%s", msg)
|
||||
print(f"An error occurred: {msg }")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user