Commit ece858c0 authored by Nirmal M.D.S's avatar Nirmal M.D.S

scheduler initial commit

parent 4cefa247
import mysql.connector
import datetime
import sched
import time
# Configure database connections
db_config = {
'host': 'localhost',
'user': 'root',
'password': '',
'database': 'rptestdb',
}
# Establish a connection to the MySQL database
try:
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
except mysql.connector.Error as err:
print(f"Error: {err}")
cursor = None
# Function to fetch and process data
def fetch_and_process_data():
# Fetch user data
cursor.execute("SELECT user_id, stress_level FROM user")
user_data = cursor.fetchall()
# Fetch task data
cursor.execute("SELECT task_id, level FROM task")
task_data = cursor.fetchall()
# Process user data
users = [{"user_id": user_id, "stress_level": stress_level} for user_id, stress_level in user_data]
# Process task data
tasks = [{"task_id": task_id, "level": level} for task_id, level in task_data]
# Compare stress_level and level and assign tasks accordingly
assigned_tasks = []
for user in users:
for task in tasks:
if user["stress_level"] == "Low" and task["level"] == "High":
assigned_tasks.append({"user_id": user["user_id"], "task_id": task["task_id"]})
tasks.remove(task) # Remove the assigned task from the tasks list
break # Exit the inner loop after assigning a task
elif user["stress_level"] == "High" and task["level"] == "Low":
assigned_tasks.append({"user_id": user["user_id"], "task_id": task["task_id"]})
tasks.remove(task) # Remove the assigned task from the tasks list
break # Exit the inner loop after assigning a task
# Insert assigned tasks into the assignedtask table
for assigned_task in assigned_tasks:
cursor.execute("INSERT INTO assignedtask (user_id, task_id) VALUES (%s, %s)",
(assigned_task["user_id"], assigned_task["task_id"]))
db_connection.commit()
return assigned_tasks
# Define the function to assign tasks
def assign_tasks():
assigned_tasks = fetch_and_process_data()
print("Assigned tasks:", assigned_tasks)
# Initialize the scheduler
s = sched.scheduler(time.time, time.sleep)
# Schedule the assignment task to run daily at 10:30 PM
def schedule_daily_task():
now = datetime.datetime.now()
target_time = datetime.datetime.combine(now.date(), datetime.time(10,19)) # 12:00 AM
if now >= target_time:
target_time += datetime.timedelta(days=1) # If the target time is already passed, schedule for the next day
time_until_next_run = (target_time - now).total_seconds()
s.enter(time_until_next_run, 1, assign_tasks)
s.run()
# Run the scheduler loop
while True:
schedule_daily_task()
time.sleep(60) # Wait for 1 minute before scheduling the next task
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment