11import importlib
22import os
3- from typing import List , Any , Callable , Optional , Union
3+ from typing import List , Any , Callable , Optional
44
55import croniter
66from django .apps import apps
1010from django .utils .translation import gettext_lazy as _
1111
1212from scheduler .queues import get_queues , logger , get_queue
13- from scheduler .rq_classes import DjangoWorker , OLD_MODEL_NAMES , JobExecution , MODEL_NAMES
13+ from scheduler .rq_classes import DjangoWorker , JobExecution , TASK_TYPES , MODEL_NAMES
1414from scheduler .settings import SCHEDULER_CONFIG , Broker
1515
1616
1717class TaskType (models .TextChoices ):
18- CRON = "CronTask " , _ ("Cron Task" )
19- REPEATABLE = "RepeatableTask " , _ ("Repeatable Task" )
20- ONCE = "OnceTask " , _ ("Run once" )
18+ CRON = "CronTaskType " , _ ("Cron Task" )
19+ REPEATABLE = "RepeatableTaskType " , _ ("Repeatable Task" )
20+ ONCE = "OnceTaskType " , _ ("Run once" )
2121
2222
2323def callable_func (callable_str : str ) -> Callable :
@@ -39,31 +39,25 @@ def get_next_cron_time(cron_string: Optional[str]) -> Optional[timezone.datetime
3939 return next_itr
4040
4141
42- def get_scheduled_task (task_model : str , task_id : int ) -> "BaseTask" : # noqa: F821
43- if isinstance (task_model , str ) and task_model not in OLD_MODEL_NAMES and task_model not in MODEL_NAMES :
44- raise ValueError (f"Job Model `{ task_model } ` does not exist, choices are { OLD_MODEL_NAMES } " )
45-
42+ def get_scheduled_task (task_type_str : str , task_id : int ) -> "BaseTask" : # noqa: F821
4643 # Try with new model names
4744 model = apps .get_model (app_label = "scheduler" , model_name = "Task" )
48- if task_model == "OnceTask" :
49- task = model .objects .filter (task_type = TaskType .ONCE , id = task_id ).first ()
45+ if task_type_str in TASK_TYPES :
46+ try :
47+ task_type = TaskType (task_type_str )
48+ task = model .objects .filter (task_type = TaskType .ONCE , id = task_id ).first ()
49+ if task is None :
50+ raise ValueError (f"Job { task_type } :{ task_id } does not exit" )
51+ return task
52+ except ValueError :
53+ raise ValueError (f"Invalid task type { task_type_str } " )
54+ elif task_type_str in MODEL_NAMES :
55+ model = apps .get_model (app_label = "scheduler" , model_name = task_type_str )
56+ task = model .objects .filter (id = task_id ).first ()
5057 if task is None :
51- raise ValueError (f"Job { task_model } :{ task_id } does not exit" )
58+ raise ValueError (f"Job { task_type_str } :{ task_id } does not exit" )
5259 return task
53- elif task_model == "RepeatableTask" :
54- task = model .objects .filter (task_type = TaskType .REPEATABLE , id = task_id ).first ()
55- if task is not None :
56- return task
57- elif task_model == "CronTask" :
58- task = model .objects .filter (task_type = TaskType .CRON , id = task_id ).first ()
59- if task is not None :
60- return task
61-
62- model = apps .get_model (app_label = "scheduler" , model_name = task_model )
63- task = model .objects .filter (id = task_id ).first ()
64- if task is None :
65- raise ValueError (f"Job { task_model } :{ task_id } does not exit" )
66- return task
60+ raise ValueError (f"Job Model { task_type_str } does not exist, choices are { TASK_TYPES } " )
6761
6862
6963def run_task (task_model : str , task_id : int ) -> Any :
0 commit comments