felicity-lims/felicity/apps/job/sched.py

97 lines
3.1 KiB
Python
Raw Normal View History

2023-05-17 16:54:05 +08:00
import logging
2023-09-11 13:02:05 +08:00
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
from felicity.apps.analysis.tasks import submit_results, verify_results
from felicity.apps.analytics.tasks import generate_report
2024-09-22 23:15:27 +08:00
from felicity.apps.impress.sample.tasks import (
impress_results,
prepare_for_impress,
cleanup_jobs,
)
2024-07-28 03:52:31 +08:00
from felicity.apps.job.enum import JobAction, JobCategory
2024-07-24 04:30:01 +08:00
from felicity.apps.job.services import JobService
2024-09-22 23:15:27 +08:00
from felicity.apps.shipment.tasks import (
dispatch_shipment,
populate_shipment_manually,
process_shipped_report,
return_shipped_report,
shipment_receive,
)
from felicity.apps.worksheet.tasks import (
populate_worksheet_plate,
populate_worksheet_plate_manually,
)
2023-05-17 16:54:05 +08:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# apscheduler
2023-09-11 13:02:05 +08:00
log = logging.getLogger("apscheduler.executors.default")
log.setLevel(logging.WARNING)
2023-05-17 16:54:05 +08:00
scheduler = AsyncIOScheduler()
2023-09-11 13:02:05 +08:00
2023-05-17 16:54:05 +08:00
async def run_jobs_if_exists():
async def unknown_action(action):
logging.warning(f"Unknown job action: {action}")
2024-07-24 04:30:01 +08:00
jobs = await JobService().fetch_sorted()
2023-05-17 16:54:05 +08:00
# logging.info(f"There are {len(jobs)} Jobs pending running.")
2023-09-11 13:02:05 +08:00
2023-05-17 16:54:05 +08:00
if len(jobs) == 0:
# felicity_pause_workforce()\
pass
2023-05-17 16:54:05 +08:00
else:
job_dispatch_table = {
2024-07-24 04:30:01 +08:00
JobCategory.WORKSHEET: {
JobAction.WORKSHEET_ASSIGN: populate_worksheet_plate,
JobAction.WORKSHEET_MANUAL_ASSIGN: populate_worksheet_plate_manually,
2023-05-17 16:54:05 +08:00
},
2024-07-24 04:30:01 +08:00
JobCategory.REPORT: {
JobAction.GENERATE_REPORT: generate_report,
2023-05-17 16:54:05 +08:00
},
2024-07-24 04:30:01 +08:00
JobCategory.IMPRESS: {
JobAction.IMPRESS_REPORT: impress_results,
2023-05-17 16:54:05 +08:00
},
2024-07-24 04:30:01 +08:00
JobCategory.RESULT: {
JobAction.RESULT_SUBMIT: submit_results,
JobAction.RESULT_VERIFY: verify_results,
2023-05-17 16:54:05 +08:00
},
2024-07-24 04:30:01 +08:00
JobCategory.SHIPMENT: {
JobAction.SHIPMENT_MANUAL_ASSIGN: populate_shipment_manually,
JobAction.SHIPMENT_DISPATCH: dispatch_shipment,
JobAction.SHIPMENT_RECEIVE: shipment_receive,
JobAction.SHIPPED_REPORT: return_shipped_report,
JobAction.DIAGNOSTIC_REPORT: process_shipped_report,
2023-05-17 16:54:05 +08:00
},
}
for job in jobs:
action_function = job_dispatch_table.get(job.category, {}).get(
job.action, unknown_action
)
logging.warning(f"Running Task: {job.action}")
await action_function(job.uid)
2023-11-22 17:13:16 +08:00
def felicity_workforce_init():
2024-02-16 23:48:19 +08:00
logging.info("Initialising felicity workforce ...")
2023-05-17 16:54:05 +08:00
scheduler.add_job(
func=run_jobs_if_exists, trigger=IntervalTrigger(seconds=10), id="felicity_wf"
2023-05-17 16:54:05 +08:00
)
scheduler.add_job(
func=prepare_for_impress,
trigger=IntervalTrigger(seconds=60 * 60),
2023-05-17 16:54:05 +08:00
id="felicity_impress",
)
scheduler.add_job(
func=cleanup_jobs,
trigger=IntervalTrigger(seconds=60 * 60 * 24),
id="felicity_jobs_clean",
)
2023-05-17 16:54:05 +08:00
scheduler.start()