job_info.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. from app.utils.cron_utils import *
  6. from configs.globals import g
  7. def create_job_info(db: Session, item: models.JobInfo):
  8. db.add(item)
  9. db.commit()
  10. db.refresh(item)
  11. return item
  12. def get_job_infos(db: Session):
  13. res: List[models.JobInfo] = db.query(models.JobInfo)\
  14. .filter(models.JobInfo.project_id == g.project_id)\
  15. .filter(models.JobInfo.delete_status == 1)\
  16. .order_by(models.JobInfo.create_time.desc()).all() # TODO: 排序
  17. return res
  18. def update_job_info(db: Session, id: int, update_item: models.JobInfo):
  19. db.commit()
  20. db.flush()
  21. db.refresh(update_item)
  22. return update_item
  23. def update_job_trigger_status(db: Session, id: int, trigger_status: int):
  24. db_item = db.query(models.JobInfo).filter(models.JobInfo.id == id).first()
  25. if not db_item:
  26. raise Exception('未找到该任务')
  27. db_item.trigger_status = trigger_status
  28. db_item.update_time = int(time.time())
  29. db.commit()
  30. db.flush()
  31. db.refresh(db_item)
  32. return db_item
  33. def get_job_info(db: Session, id: int):
  34. db_item = db.query(models.JobInfo).filter(models.JobInfo.id == id).first()
  35. if not db_item:
  36. raise Exception('未找到该任务')
  37. return db_item
  38. def delete_job_info(db: Session, job_id: int):
  39. job_item = db.query(models.JobInfo).filter(models.JobInfo.id == job_id).first()
  40. if not job_item:
  41. raise Exception('未找到该任务')
  42. if job_item.trigger_status == 1:
  43. raise Exception('该任务未停用,不能删除')
  44. job_item.delete_status = 0
  45. db.commit()
  46. db.flush()
  47. db.refresh(job_item)
  48. return job_item