import os
import schedule
import time
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def check_directories(directories):
"""
Checks if the specified directories exist.
Returns a dictionary of errors, or an empty dictionary if all directories exist.
"""
errors = {}
for directory in directories:
if not os.path.isdir(directory):
errors[directory] = "Directory does not exist"
elif not os.access(directory, os.R_OK):
errors[directory] = "Directory is not readable"
return errors
def dry_run_schedule(directories, job_function, schedule_time):
"""
Performs a dry-run of the schedule, checking directories for errors.
"""
errors = check_directories(directories)
if errors:
logging.warning("Directory errors detected during dry-run:")
for directory, error in errors.items():
logging.warning(f" {directory}: {error}")
print("Dry-run aborted due to directory errors.")
return False # Indicate dry-run failure
else:
logging.info("Dry-run successful. All directories exist and are readable.")
print("Dry-run successful. All directories exist and are readable.")
return True # Indicate dry-run success
def scheduled_job(job_function, schedule_time):
"""
The actual scheduled job.
"""
try:
job_function()
logging.info(f"Job executed successfully at {time.strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
logging.error(f"Job failed: {e}")
def main():
"""
Main function to set up and run the dry-run and scheduling.
"""
directories_to_check = ["/path/to/directory1", "/path/to/directory2", "/path/to/directory3"] # Replace with your directory paths
schedule_time = "0 * * * *" # Run every minute (example)
# Define a sample job function - replace with your actual job
def my_job():
print("Running my scheduled job...")
time.sleep(5) # Simulate some work
print("Job completed.")
if dry_run_schedule(directories_to_check, my_job, schedule_time):
schedule.every().minute.do(scheduled_job, my_job, schedule_time) #uncomment if you want to run the scheduled job
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
main()
Add your comment