From 0d16d9d861e4316a02e0dff773f353ee97563930 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Mon, 14 Oct 2024 00:40:59 +0530 Subject: [PATCH 1/3] feat: add task scheduler for repitetive tasks --- task_scheduler/README.md | 61 ++++++++++++++++++++++++++++++++ task_scheduler/requirements.txt | 2 ++ task_scheduler/task_scheduler.py | 39 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 task_scheduler/README.md create mode 100644 task_scheduler/requirements.txt create mode 100644 task_scheduler/task_scheduler.py diff --git a/task_scheduler/README.md b/task_scheduler/README.md new file mode 100644 index 000000000..13940a375 --- /dev/null +++ b/task_scheduler/README.md @@ -0,0 +1,61 @@ + +# Automated Task Scheduler for Repetitive Tasks + +This script is designed to automate repetitive tasks by scheduling them at predefined intervals using Python's `schedule` library. It logs task execution, making it easy to track the status of scheduled jobs. + +## Functionalities + +- Schedule and automate repetitive tasks. +- Log task execution to a file for monitoring and debugging. +- Customizable task functions to fit user needs. + +## Setup Instructions + +1. **Clone the repository:** + ```bash + git clone + ``` + +2. **Navigate to the project directory:** + ```bash + cd automated-task-scheduler + ``` + +3. **Install the required dependencies:** + ```bash + pip install -r requirements.txt + ``` + +4. **Run the script:** + ```bash + python task_scheduler.py + ``` + +## Detailed Explanation of Script + +- **Task Functions:** `task_one` and `task_two` are sample tasks. You can modify these functions to suit your automation needs. + +- **Scheduling:** The script schedules `task_one` to run every 10 minutes and `task_two` to run every hour using the `schedule` library. You can adjust these intervals in the `setup_schedule()` function. + +- **Logging:** Each task's execution is logged in the `task_scheduler.log` file with timestamps, providing visibility into when tasks were run. + +- **Main Loop:** The script continuously runs in an infinite loop (`run_scheduler()`), checking for pending tasks and executing them at the scheduled times. + +## Output + +The script generates a `task_scheduler.log` file, which logs the execution of tasks like this: + +``` +2024-10-13 12:00:00 - Task 1 is running +2024-10-13 12:10:00 - Task 1 is running +2024-10-13 13:00:00 - Task 2 is running +``` + +## Author(s) + +- ARNAV RAJ + +## Disclaimers + +- Ensure that the script is running continuously in the background for tasks to execute as scheduled. +- The `schedule` library is suitable for lightweight task scheduling. For more complex scheduling requirements, consider using a task queue like Celery or a cron job. diff --git a/task_scheduler/requirements.txt b/task_scheduler/requirements.txt new file mode 100644 index 000000000..6727d06a8 --- /dev/null +++ b/task_scheduler/requirements.txt @@ -0,0 +1,2 @@ +schedule==1.1.0 +pandas==1.2.0 \ No newline at end of file diff --git a/task_scheduler/task_scheduler.py b/task_scheduler/task_scheduler.py new file mode 100644 index 000000000..f52fc2bfe --- /dev/null +++ b/task_scheduler/task_scheduler.py @@ -0,0 +1,39 @@ +import schedule +import time +import logging + +# Set up logging +logging.basicConfig( + filename='task_scheduler.log', + level=logging.INFO, + format='%(asctime)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' +) + +# Example task 1: Function to be scheduled +def task_one(): + logging.info("Task 1 is running") + +# Example task 2: Another task to be scheduled +def task_two(): + logging.info("Task 2 is running") + +# Schedule tasks +def setup_schedule(): + # Schedule task_one every 10 minutes + schedule.every(10).minutes.do(task_one) + + # Schedule task_two every hour + schedule.every().hour.do(task_two) + + # Add more tasks here as needed + +# Main loop to keep the scheduler running +def run_scheduler(): + setup_schedule() + while True: + schedule.run_pending() + time.sleep(1) + +if __name__ == "__main__": + run_scheduler() From 64ef5c522a41c9d1fd8c1ba118d621eb1f2bbf57 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Mon, 14 Oct 2024 13:26:31 +0530 Subject: [PATCH 2/3] feat: add task scheduler for repitetive tasks --- task_scheduler/task_scheduler.py | 38 ++++++++------------------------ 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/task_scheduler/task_scheduler.py b/task_scheduler/task_scheduler.py index f52fc2bfe..77c5fbff3 100644 --- a/task_scheduler/task_scheduler.py +++ b/task_scheduler/task_scheduler.py @@ -1,39 +1,19 @@ -import schedule import time -import logging - -# Set up logging -logging.basicConfig( - filename='task_scheduler.log', - level=logging.INFO, - format='%(asctime)s - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S' -) +import schedule -# Example task 1: Function to be scheduled -def task_one(): - logging.info("Task 1 is running") +def task(): + print("Task is being executed") -# Example task 2: Another task to be scheduled -def task_two(): - logging.info("Task 2 is running") +def task2(): + print("Another task is being executed") -# Schedule tasks -def setup_schedule(): - # Schedule task_one every 10 minutes - schedule.every(10).minutes.do(task_one) - - # Schedule task_two every hour - schedule.every().hour.do(task_two) - - # Add more tasks here as needed +def run_tasks(): + schedule.every(1).minutes.do(task) + schedule.every(2).minutes.do(task2) -# Main loop to keep the scheduler running -def run_scheduler(): - setup_schedule() while True: schedule.run_pending() time.sleep(1) if __name__ == "__main__": - run_scheduler() + run_tasks() From b43c48293b298c1b2b0925b8156b0cdb691bf971 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Mon, 14 Oct 2024 13:27:53 +0530 Subject: [PATCH 3/3] feat: add task scheduler for repitetive tasks --- task_scheduler/task_scheduler.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/task_scheduler/task_scheduler.py b/task_scheduler/task_scheduler.py index 77c5fbff3..b92e86a80 100644 --- a/task_scheduler/task_scheduler.py +++ b/task_scheduler/task_scheduler.py @@ -1,12 +1,15 @@ import time import schedule + def task(): print("Task is being executed") + def task2(): print("Another task is being executed") + def run_tasks(): schedule.every(1).minutes.do(task) schedule.every(2).minutes.do(task2) @@ -15,5 +18,6 @@ def run_tasks(): schedule.run_pending() time.sleep(1) + if __name__ == "__main__": run_tasks()