1
1
import importlib
2
2
import os
3
- from typing import List , Any , Callable , Optional , Union
3
+ from typing import List , Any , Callable , Optional
4
4
5
5
import croniter
6
6
from django .apps import apps
10
10
from django .utils .translation import gettext_lazy as _
11
11
12
12
from 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
14
14
from scheduler .settings import SCHEDULER_CONFIG , Broker
15
15
16
16
17
17
class 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" )
21
21
22
22
23
23
def callable_func (callable_str : str ) -> Callable :
@@ -39,31 +39,25 @@ def get_next_cron_time(cron_string: Optional[str]) -> Optional[timezone.datetime
39
39
return next_itr
40
40
41
41
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
46
43
# Try with new model names
47
44
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 ()
50
57
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" )
52
59
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 } " )
67
61
68
62
69
63
def run_task (task_model : str , task_id : int ) -> Any :
0 commit comments