@@ -27,10 +27,21 @@ class CeleryBeat(t.TypedDict):
27
27
args : t .NotRequired [Args ]
28
28
kwargs : t .NotRequired [KwArgs ]
29
29
30
- def __init__ (self , ** kwargs ): # type: ignore[misc]
31
- kwargs ["task" ] = f"{ settings .SERVICE_NAME } .{ kwargs ['task' ]} "
32
30
33
- super ().__init__ (** kwargs )
31
+ def namespace_task (task : t .Union [str , t .Callable ]):
32
+ """Namespace a task by the service it's in.
33
+
34
+ Args:
35
+ task: The name of the task.
36
+
37
+ Returns:
38
+ The name of the task in the format: "{SERVICE_NAME}.{TASK_NAME}".
39
+ """
40
+
41
+ if callable (task ):
42
+ task = f"{ task .__module__ } .{ task .__name__ } "
43
+
44
+ return f"{ settings .SERVICE_NAME } .{ task } "
34
45
35
46
36
47
def shared_task (* args , ** kwargs ):
@@ -39,21 +50,12 @@ def shared_task(*args, **kwargs):
39
50
tasks to a specific service.
40
51
"""
41
52
42
- def get_name (func : t .Callable ):
43
- return "." .join (
44
- [
45
- settings .SERVICE_NAME ,
46
- func .__module__ ,
47
- func .__name__ ,
48
- ]
49
- )
50
-
51
53
if len (args ) == 1 and callable (args [0 ]):
52
54
func = args [0 ]
53
- return _shared_task (name = get_name (func ))(func )
55
+ return _shared_task (name = namespace_task (func ))(func )
54
56
55
57
def wrapper (func : t .Callable ):
56
58
kwargs .pop ("name" , None )
57
- return _shared_task (name = get_name (func ), * args , ** kwargs )(func )
59
+ return _shared_task (name = namespace_task (func ), * args , ** kwargs )(func )
58
60
59
61
return wrapper
0 commit comments