potatooine commited on
Commit
3584438
Β·
1 Parent(s): 614a069

not working i dont know if i need to drag config_id down here

Browse files
Files changed (1) hide show
  1. api_monitor.py +119 -1
api_monitor.py CHANGED
@@ -5,6 +5,11 @@ import hashlib
5
  import psycopg2
6
  import os
7
  from dotenv import load_dotenv
 
 
 
 
 
8
 
9
  # Load environment variables from .env file
10
  load_dotenv()
@@ -360,13 +365,124 @@ def activate_monitoring(config_id, mcp_api_key):
360
 
361
  ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
362
  """
 
363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
  return {
365
  "success": False,
366
- "message": "Function not implemented yet; this is a placeholder.",
367
  "config_id": config_id,
368
  }
369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
  ## testing
372
  if __name__ == "__main__":
@@ -386,3 +502,5 @@ if __name__ == "__main__":
386
  time_to_start="",
387
  )
388
  print(response)
 
 
 
5
  import psycopg2
6
  import os
7
  from dotenv import load_dotenv
8
+ from apscheduler.schedulers.background import BackgroundScheduler
9
+ from apscheduler.triggers.interval import IntervalTrigger
10
+ from apscheduler.triggers.date import DateTrigger
11
+ import time
12
+ import threading
13
 
14
  # Load environment variables from .env file
15
  load_dotenv()
 
365
 
366
  ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
367
  """
368
+ #get config from database
369
 
370
+
371
+ #check if config_id is valid!
372
+
373
+ #need to extract
374
+ '''
375
+ mcp_api_key,
376
+ name,
377
+ description,
378
+ method,
379
+ base_url,
380
+ endpoint,
381
+ param_keys_values,
382
+ header_keys_values,
383
+ additional_params,
384
+ schedule_interval_minutes,
385
+ stop_after_hours,
386
+ time_to_start,
387
+ this
388
+ '''
389
+
390
+ # using time_to_start, schedule_interval_minutes, and stop_after_hours
391
+ #label using name and description
392
+
393
+
394
+
395
+
396
+
397
+
398
+ #attempt to create the scheduler
399
+ try:
400
+ create_schedule_thread(
401
+ tag = "defualt tag", # this is not a function
402
+ start_delay_sec=1, # Use the time_to_start if provided
403
+ interval_sec=3, # Convert schedule_interval_minutes to seconds
404
+ duration_hours=0.015, # Use stop_after_hours if provided
405
+ config_id=config_id, # Pass the config_id for tracking
406
+ )
407
+
408
+
409
+
410
+
411
+
412
+ except Exception as e:
413
+ return {
414
+ "success": False,
415
+ "message": f"Failed to create scheduler: {str(e)}",
416
+ "config_id": config_id,
417
+ }
418
+
419
+
420
+ # if we get down here something is horribly wrong, we should never get here
421
  return {
422
  "success": False,
423
+ "message": "SOMETHING WENT HORRIBLY WRONG, THIS SHOULD NEVER HAPPEN",
424
  "config_id": config_id,
425
  }
426
 
427
+ # πŸ” Create a new generator per schedule
428
+ def make_data_generator(tag):
429
+ i = 0
430
+ while True:
431
+ yield f"{tag}-{i}"
432
+ i += 1
433
+
434
+ # 🧱 Function that runs inside each thread
435
+ def schedule_runner(tag, start_delay_sec, interval_sec, duration_hours,config_id):
436
+ scheduler = BackgroundScheduler()
437
+ start_time = datetime.now() + timedelta(seconds=start_delay_sec)
438
+ end_time = start_time + timedelta(hours=duration_hours)
439
+ generator = make_data_generator(tag)
440
+
441
+ # The actual job to be scheduled
442
+ def job_func(config_id):
443
+ value = next(generator)
444
+ print(f"[{tag}] πŸ”„ Job ran with: {value} at {datetime.now()}")
445
+ yield {
446
+ "success": True,
447
+ "message": f"[{tag}] πŸ”„ Job ran with: {value} at {datetime.now()}",
448
+ "config_id": config_id}
449
+
450
+
451
+ # Graceful shutdown job
452
+ def shutdown_func():
453
+ print(f"[{tag}] β›” Scheduler shutting down at {datetime.now()}")
454
+ scheduler.shutdown()
455
+
456
+ # Add the jobs
457
+ scheduler.add_job(
458
+ job_func(config_id),
459
+ trigger=IntervalTrigger(start_date=start_time, seconds=interval_sec, end_date=end_time)
460
+ )
461
+ scheduler.add_job(
462
+ shutdown_func,
463
+ trigger=DateTrigger(run_date=end_time)
464
+ )
465
+
466
+ scheduler.start()
467
+ print(f"[{tag}] πŸ•’ Scheduler running from {start_time} to {end_time}, every {interval_sec}s.")
468
+
469
+ # Keep thread alive while scheduler is running
470
+ while scheduler.running:
471
+ time.sleep(1)
472
+
473
+ print(f"[{tag}] βœ… Scheduler completed.")
474
+
475
+ # 🧡 Thread spawner
476
+ def create_schedule_thread(tag, start_delay_sec, interval_sec, duration_hours,config_id):
477
+ thread = threading.Thread(
478
+ target=schedule_runner,
479
+ args=(tag, start_delay_sec, interval_sec, duration_hours,config_id),
480
+ daemon=True # Automatically closes with main program
481
+ )
482
+ thread.start()
483
+ return thread
484
+
485
+
486
 
487
  ## testing
488
  if __name__ == "__main__":
 
502
  time_to_start="",
503
  )
504
  print(response)
505
+
506
+