**Refactor scheduling mechanism to use `schedule` library**
Browse files- Replace `APScheduler` with `schedule` for task scheduling
- Implement a background thread to run the scheduler
- Add utility functions to manage and clear scheduled tasks
- Update task planning functions to use the new scheduling approach
- Improve error handling and logging for task scheduling
- __pycache__/functi.cpython-311.pyc +0 -0
- app.py +2 -1
- functi.py +125 -27
__pycache__/functi.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/functi.cpython-311.pyc and b/__pycache__/functi.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -199,7 +199,8 @@ stylekit = {
|
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
planning()
|
| 202 |
-
|
|
|
|
| 203 |
gui = Gui(pages=pages)
|
| 204 |
gui.run(
|
| 205 |
debug=True,
|
|
|
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
planning()
|
| 202 |
+
scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
|
| 203 |
+
scheduler_thread.start()
|
| 204 |
gui = Gui(pages=pages)
|
| 205 |
gui.run(
|
| 206 |
debug=True,
|
functi.py
CHANGED
|
@@ -12,16 +12,27 @@ from gradio_client import Client
|
|
| 12 |
import pandas as pd
|
| 13 |
from requests_oauthlib import OAuth2Session
|
| 14 |
import requests
|
| 15 |
-
|
|
|
|
| 16 |
from timing_lin import *
|
| 17 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 18 |
from apscheduler.triggers.cron import CronTrigger
|
| 19 |
from functools import partial
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
Linked_account_name = " "
|
| 26 |
Linked_social_network = " "
|
| 27 |
data_schedule ={}
|
|
@@ -74,12 +85,18 @@ def wait(delay):
|
|
| 74 |
time.sleep(delay)
|
| 75 |
|
| 76 |
def replanifier_toutes_les_tâches(df):
|
| 77 |
-
|
|
|
|
| 78 |
df.apply(
|
| 79 |
-
lambda r:
|
| 80 |
axis=1
|
| 81 |
)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def post_generation_for_robot(id,social,idd) :
|
| 84 |
try :
|
| 85 |
print("⏳ Tâche planifizzzzzzzzzzzzzzzée pour",flush = True)
|
|
@@ -190,7 +207,7 @@ def post_publishing_for_robot(id_social,id_user,idd,ss) :
|
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
-
def
|
| 194 |
# Parse schedule_time_str and adjusted_time
|
| 195 |
parts = schedule_time_str.strip().split()
|
| 196 |
part_adj = adjusted_time.strip().split()
|
|
@@ -211,15 +228,15 @@ def planifier_ligne(id_schedule, id_social, user_id, schedule_time_str, ss, adju
|
|
| 211 |
print(f"❌ Heure invalide : {hm}", flush=True)
|
| 212 |
return
|
| 213 |
|
| 214 |
-
# Map day names to
|
| 215 |
day_map = {
|
| 216 |
-
"monday":
|
| 217 |
-
"tuesday":
|
| 218 |
-
"wednesday":
|
| 219 |
-
"thursday":
|
| 220 |
-
"friday":
|
| 221 |
-
"saturday":
|
| 222 |
-
"sunday":
|
| 223 |
}
|
| 224 |
|
| 225 |
jour_key = jour.lower()
|
|
@@ -229,28 +246,109 @@ def planifier_ligne(id_schedule, id_social, user_id, schedule_time_str, ss, adju
|
|
| 229 |
print(f"❌ Jour non reconnu : {jour}/{jour_adj}", flush=True)
|
| 230 |
return
|
| 231 |
|
| 232 |
-
# Remove previous jobs for this schedule
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
except Exception as e:
|
| 237 |
print(f"❌ Erreur lors de la suppression des tâches : {e}", flush=True)
|
| 238 |
|
| 239 |
# Schedule publishing
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
CronTrigger(day_of_week=day_map[jour_key], hour=hour, minute=minute),
|
| 243 |
-
id=f"pub-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 244 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
# Schedule generation
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
CronTrigger(day_of_week=day_map[jour_key_adj], hour=hour_adj, minute=minute_adj),
|
| 250 |
-
id=f"gen-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 251 |
)
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
print(f"⏳
|
| 254 |
|
| 255 |
def add_scheduling(state):
|
| 256 |
"""Add new scheduling with thread safety"""
|
|
|
|
| 12 |
import pandas as pd
|
| 13 |
from requests_oauthlib import OAuth2Session
|
| 14 |
import requests
|
| 15 |
+
import schedule
|
| 16 |
+
import threading
|
| 17 |
from timing_lin import *
|
| 18 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 19 |
from apscheduler.triggers.cron import CronTrigger
|
| 20 |
from functools import partial
|
| 21 |
|
| 22 |
+
active_jobs = {}
|
| 23 |
+
|
| 24 |
+
def run_scheduler():
|
| 25 |
+
"""Fonction pour faire tourner le scheduler en arrière-plan"""
|
| 26 |
+
while True:
|
| 27 |
+
schedule.run_pending()
|
| 28 |
+
time.sleep(1)
|
| 29 |
|
| 30 |
|
| 31 |
+
def clear_all_schedules():
|
| 32 |
+
"""Fonction utilitaire pour supprimer toutes les tâches planifiées"""
|
| 33 |
+
schedule.clear()
|
| 34 |
+
print("🧹 Toutes les tâches ont été supprimées", flush=True)
|
| 35 |
+
|
| 36 |
Linked_account_name = " "
|
| 37 |
Linked_social_network = " "
|
| 38 |
data_schedule ={}
|
|
|
|
| 85 |
time.sleep(delay)
|
| 86 |
|
| 87 |
def replanifier_toutes_les_tâches(df):
|
| 88 |
+
# Efface toutes les anciennes tâches
|
| 89 |
+
clear_all_schedules()
|
| 90 |
df.apply(
|
| 91 |
+
lambda r: planifier_ligne_pub(r["id"],r["id_social"], r["user_id"], r["schedule_time"],r["social_network"],r["adjusted_time"]),
|
| 92 |
axis=1
|
| 93 |
)
|
| 94 |
|
| 95 |
+
df.apply(
|
| 96 |
+
lambda r: planifier_ligne_gen(r["id"],r["id_social"], r["user_id"], r["schedule_time"],r["social_network"],r["adjusted_time"]),
|
| 97 |
+
axis=1
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
def post_generation_for_robot(id,social,idd) :
|
| 101 |
try :
|
| 102 |
print("⏳ Tâche planifizzzzzzzzzzzzzzzée pour",flush = True)
|
|
|
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
+
def planifier_ligne_pub(id_schedule, id_social, user_id, schedule_time_str, ss, adjusted_time):
|
| 211 |
# Parse schedule_time_str and adjusted_time
|
| 212 |
parts = schedule_time_str.strip().split()
|
| 213 |
part_adj = adjusted_time.strip().split()
|
|
|
|
| 228 |
print(f"❌ Heure invalide : {hm}", flush=True)
|
| 229 |
return
|
| 230 |
|
| 231 |
+
# Map day names to schedule format
|
| 232 |
day_map = {
|
| 233 |
+
"monday": schedule.every().monday,
|
| 234 |
+
"tuesday": schedule.every().tuesday,
|
| 235 |
+
"wednesday": schedule.every().wednesday,
|
| 236 |
+
"thursday": schedule.every().thursday,
|
| 237 |
+
"friday": schedule.every().friday,
|
| 238 |
+
"saturday": schedule.every().saturday,
|
| 239 |
+
"sunday": schedule.every().sunday,
|
| 240 |
}
|
| 241 |
|
| 242 |
jour_key = jour.lower()
|
|
|
|
| 246 |
print(f"❌ Jour non reconnu : {jour}/{jour_adj}", flush=True)
|
| 247 |
return
|
| 248 |
|
| 249 |
+
# Remove previous jobs for this schedule
|
| 250 |
+
pub_job_id = f"publop-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 251 |
+
gen_job_id = f"gen-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 252 |
+
|
| 253 |
+
try:
|
| 254 |
+
# Supprimer les anciennes tâches si elles existent
|
| 255 |
+
if pub_job_id in active_jobs:
|
| 256 |
+
schedule.cancel_job(active_jobs[pub_job_id])
|
| 257 |
+
del active_jobs[pub_job_id]
|
| 258 |
+
|
| 259 |
+
if gen_job_id in active_jobs:
|
| 260 |
+
schedule.cancel_job(active_jobs[gen_job_id])
|
| 261 |
+
del active_jobs[gen_job_id]
|
| 262 |
except Exception as e:
|
| 263 |
print(f"❌ Erreur lors de la suppression des tâches : {e}", flush=True)
|
| 264 |
|
| 265 |
# Schedule publishing
|
| 266 |
+
pub_job = day_map[jour_key].at(f"{hour:02d}:{minute:02d}").do(
|
| 267 |
+
post_publishing_for_robot, id_social, user_id, id_schedule, ss
|
|
|
|
|
|
|
| 268 |
)
|
| 269 |
+
pub_job.tag = pub_job_id
|
| 270 |
+
# active_jobs[pub_job_id] = pub_job
|
| 271 |
+
|
| 272 |
+
# Schedule generation
|
| 273 |
+
# gen_job = day_map[jour_key_adj].at(f"{hour_adj:02d}:{minute_adj:02d}").do(
|
| 274 |
+
# post_generation_for_robot, user_id, id_social, id_schedule
|
| 275 |
+
# )
|
| 276 |
+
# gen_job.tag = gen_job_id
|
| 277 |
+
# active_jobs[gen_job_id] = gen_job
|
| 278 |
+
print(f"⏳ Scheduler: Tâche planifiée pour {pub_job.tag} ", flush=True)
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def planifier_ligne_gen(id_schedule, id_social, user_id, schedule_time_str, ss, adjusted_time):
|
| 283 |
+
# Parse schedule_time_str and adjusted_time
|
| 284 |
+
parts = schedule_time_str.strip().split()
|
| 285 |
+
part_adj = adjusted_time.strip().split()
|
| 286 |
+
if len(parts) != 2 or ':' not in parts[1]:
|
| 287 |
+
print(f"❌ Format invalide : {schedule_time_str}", flush=True)
|
| 288 |
+
return
|
| 289 |
+
if len(part_adj) != 2 or ':' not in part_adj[1]:
|
| 290 |
+
print(f"❌ Format invalide : {adjusted_time}", flush=True)
|
| 291 |
+
return
|
| 292 |
+
|
| 293 |
+
jour, hm = parts
|
| 294 |
+
jour_adj, hm_adj = part_adj
|
| 295 |
+
|
| 296 |
+
try:
|
| 297 |
+
hour, minute = map(int, hm.split(':'))
|
| 298 |
+
hour_adj, minute_adj = map(int, hm_adj.split(':'))
|
| 299 |
+
except ValueError:
|
| 300 |
+
print(f"❌ Heure invalide : {hm}", flush=True)
|
| 301 |
+
return
|
| 302 |
+
|
| 303 |
+
# Map day names to schedule format
|
| 304 |
+
day_map = {
|
| 305 |
+
"monday": schedule.every().monday,
|
| 306 |
+
"tuesday": schedule.every().tuesday,
|
| 307 |
+
"wednesday": schedule.every().wednesday,
|
| 308 |
+
"thursday": schedule.every().thursday,
|
| 309 |
+
"friday": schedule.every().friday,
|
| 310 |
+
"saturday": schedule.every().saturday,
|
| 311 |
+
"sunday": schedule.every().sunday,
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
jour_key = jour.lower()
|
| 315 |
+
jour_key_adj = jour_adj.lower()
|
| 316 |
+
|
| 317 |
+
if jour_key not in day_map or jour_key_adj not in day_map:
|
| 318 |
+
print(f"❌ Jour non reconnu : {jour}/{jour_adj}", flush=True)
|
| 319 |
+
return
|
| 320 |
+
|
| 321 |
+
# Remove previous jobs for this schedule
|
| 322 |
+
pub_job_id = f"publop-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 323 |
+
gen_job_id = f"gen-{id_schedule}-{schedule_time_str}--{id_social}"
|
| 324 |
+
|
| 325 |
+
try:
|
| 326 |
+
# Supprimer les anciennes tâches si elles existent
|
| 327 |
+
if pub_job_id in active_jobs:
|
| 328 |
+
schedule.cancel_job(active_jobs[pub_job_id])
|
| 329 |
+
del active_jobs[pub_job_id]
|
| 330 |
+
|
| 331 |
+
if gen_job_id in active_jobs:
|
| 332 |
+
schedule.cancel_job(active_jobs[gen_job_id])
|
| 333 |
+
del active_jobs[gen_job_id]
|
| 334 |
+
except Exception as e:
|
| 335 |
+
print(f"❌ Erreur lors de la suppression des tâches : {e}", flush=True)
|
| 336 |
+
|
| 337 |
+
# # Schedule publishing
|
| 338 |
+
# pub_job = day_map[jour_key].at(f"{hour:02d}:{minute:02d}").do(
|
| 339 |
+
# post_publishing_for_robot, id_social, user_id, id_schedule, ss
|
| 340 |
+
# )
|
| 341 |
+
# pub_job.tag = pub_job_id
|
| 342 |
+
# # active_jobs[pub_job_id] = pub_job
|
| 343 |
|
| 344 |
# Schedule generation
|
| 345 |
+
gen_job = day_map[jour_key_adj].at(f"{hour_adj:02d}:{minute_adj:02d}").do(
|
| 346 |
+
post_generation_for_robot, user_id, id_social, id_schedule
|
|
|
|
|
|
|
| 347 |
)
|
| 348 |
+
gen_job.tag = gen_job_id
|
| 349 |
+
active_jobs[gen_job_id] = gen_job
|
| 350 |
|
| 351 |
+
print(f"⏳ Scheduler: Tâche planifiée pour {gen_job.tag} ", flush=True)
|
| 352 |
|
| 353 |
def add_scheduling(state):
|
| 354 |
"""Add new scheduling with thread safety"""
|