p1-taskqueue 0.1.5__py3-none-any.whl → 0.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of p1-taskqueue might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: p1-taskqueue
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: A Task Queue Wrapper for Dekoruma Backend
5
5
  Author-email: Chalvin <engineering@dekoruma.com>
6
6
  Project-URL: Homepage, https://github.com/Dekoruma/p1-taskqueue
@@ -0,0 +1,9 @@
1
+ taskqueue/__init__.py,sha256=nNHXIyysNLom9f8of-d__pWJ-YF53Mbrbsb1frPzPPI,298
2
+ taskqueue/celery_app.py,sha256=dUT-7XzsSQbr8vKrLv7f_6iYxTCUEJZHEt9fL-KIQ5U,3302
3
+ taskqueue/cmanager.py,sha256=raQwdccYTpWRiW8UGU5OcUEN0jZr9TTlIEnhZGtamiI,12274
4
+ taskqueue/libs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ taskqueue/libs/helper_test.py,sha256=_yfPhm_7CzbN30j7E_Ld5KwXRlGHhTNrRTOBZQySEIU,3656
6
+ p1_taskqueue-0.1.7.dist-info/METADATA,sha256=y3FYv9zHOECh2FzdJRBKofYPGvlr1a1A0wUk4B98TyU,1508
7
+ p1_taskqueue-0.1.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ p1_taskqueue-0.1.7.dist-info/top_level.txt,sha256=hA3SM1ik2K8iPqtlt_-_nJ4TAePwHPN3vsOc4EiynqU,10
9
+ p1_taskqueue-0.1.7.dist-info/RECORD,,
taskqueue/__init__.py CHANGED
@@ -7,6 +7,7 @@ __author__ = "Chalvin"
7
7
  __email__ = "engineering@dekoruma.com"
8
8
 
9
9
  from .cmanager import cm
10
+ from .cmanager import taskqueue_class
10
11
  from .celery_app import celery_app
11
12
 
12
- __all__ = ["cm", "celery_app"]
13
+ __all__ = ["cm", "celery_app", "taskqueue_class"]
taskqueue/cmanager.py CHANGED
@@ -28,6 +28,26 @@ def _is_class_method(func: Any) -> bool:
28
28
  )
29
29
 
30
30
 
31
+ def taskqueue_class(cls):
32
+ """Decorator to automatically capture init arguments for taskqueue."""
33
+ original_init = cls.__init__
34
+
35
+ def wrapped_init(self, *args, **kwargs):
36
+ self._taskqueue_init_args = list(args)
37
+ self._taskqueue_init_kwargs = dict(kwargs)
38
+ original_init(self, *args, **kwargs)
39
+
40
+ cls.__init__ = wrapped_init
41
+ return cls
42
+
43
+
44
+ def _extract_init_args_from_instance(instance: Any) -> Tuple[list, dict]:
45
+ """Extract init arguments from instance."""
46
+ init_args = getattr(instance, '_taskqueue_init_args', [])
47
+ init_kwargs = getattr(instance, '_taskqueue_init_kwargs', {})
48
+ return init_args, init_kwargs
49
+
50
+
31
51
  def _split_function_and_queue_kwargs(kwargs: Dict[str, Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]:
32
52
  # To prevent confusion whether a kwargs is for function or queue kwargs(i.e celery options and on_commit),
33
53
  # ignore confusing kwargs while give warning
@@ -62,10 +82,20 @@ def _build_dynamic_task_call(func: Any, *args: Any, **func_kwargs: Any) -> Tuple
62
82
  module_path = klass.__module__
63
83
  class_name = klass.__name__
64
84
  method_name = func.__name__
85
+
86
+ init_args, init_kwargs = _extract_init_args_from_instance(instance)
87
+
65
88
  task_name = "taskqueue.cmanager.dynamic_class_method_executor"
66
- task_args = [module_path, class_name,
67
- method_name, list(args), dict(func_kwargs)]
68
- task_kwargs: Dict[str, Any] = {}
89
+ task_args = []
90
+ task_kwargs: Dict[str, Any] = {
91
+ "module_path": module_path,
92
+ "class_name": class_name,
93
+ "method_name": method_name,
94
+ "args": list(args),
95
+ "kwargs": dict(func_kwargs),
96
+ "init_args": init_args,
97
+ "init_kwargs": init_kwargs,
98
+ }
69
99
  return task_name, task_args, task_kwargs
70
100
 
71
101
  module_path = getattr(func, "__module__", None)
@@ -75,8 +105,13 @@ def _build_dynamic_task_call(func: Any, *args: Any, **func_kwargs: Any) -> Tuple
75
105
  "Unsupported callable type for Celery enqueue. Provide a module-level function or a class method.")
76
106
 
77
107
  task_name = "taskqueue.cmanager.dynamic_function_executor"
78
- task_args = [module_path, function_name, list(args), dict(func_kwargs)]
79
- task_kwargs = {}
108
+ task_args = []
109
+ task_kwargs = {
110
+ "module_path": module_path,
111
+ "function_name": function_name,
112
+ "args": list(args),
113
+ "kwargs": dict(func_kwargs),
114
+ }
80
115
  return task_name, task_args, task_kwargs
81
116
 
82
117
 
@@ -160,17 +195,18 @@ class CManager:
160
195
  task_name, task_args, task_kwargs = _build_dynamic_task_call(
161
196
  func, *func_args, **func_kwargs)
162
197
 
163
- self._send_task(task_name, task_args, task_kwargs, queue_kwargs)
198
+ task_id = self._send_task(task_name, task_args,
199
+ task_kwargs, queue_kwargs)
164
200
 
165
- logger.info('[_enqueue_op_base %s] Submit Celery Task SUCCESS, task_name: %s args: %s, kwargs: %s' % (
166
- enqueue_op_type, task_name, task_args, task_kwargs))
201
+ logger.info('[_enqueue_op_base %s] Submit Celery Task SUCCESS, task_name: %s args: %s, kwargs: %s, task_id: %s' % (
202
+ enqueue_op_type, task_name, task_args, task_kwargs, task_id))
167
203
 
168
204
  except Exception as e:
169
205
  logger.exception('[_enqueue_op_base %s] Submit Celery Task FAILED, error: %s, args: %s, kwargs: %s' % (
170
206
  enqueue_op_type, str(e), args, kwargs))
171
207
  raise e
172
208
 
173
- def _send_task(self, task_name: str, task_args: list, task_kwargs: dict, queue_kwargs: Dict[str, Any]) -> None:
209
+ def _send_task(self, task_name: str, task_args: list, task_kwargs: dict, queue_kwargs: Dict[str, Any]) -> str:
174
210
  celery_app = self._get_celery_app()
175
211
 
176
212
  queue_name = queue_kwargs.pop("channel", None)
@@ -194,8 +230,9 @@ class CManager:
194
230
  else:
195
231
  task_kwargs_with_retry["retry"] = retry_policy
196
232
 
197
- celery_app.send_task(task_name, args=task_args,
198
- kwargs=task_kwargs_with_retry, **send_opts)
233
+ task = celery_app.send_task(task_name, args=task_args,
234
+ kwargs=task_kwargs_with_retry, **send_opts)
235
+ return str(task.id)
199
236
 
200
237
 
201
238
  cm = CManager()
@@ -203,7 +240,7 @@ cm = CManager()
203
240
 
204
241
  # Dynamic task executors - handle function and class method execution
205
242
  @shared_task(bind=True, max_retries=K_MAX_RETRY_COUNT)
206
- def dynamic_function_executor(self, module_path, function_name, args=None, kwargs=None, retry=None):
243
+ def dynamic_function_executor(self, module_path=None, function_name=None, args=None, kwargs=None, retry=None):
207
244
  job_id = self.request.id
208
245
  try:
209
246
  module = importlib.import_module(module_path)
@@ -235,12 +272,14 @@ def dynamic_function_executor(self, module_path, function_name, args=None, kwarg
235
272
 
236
273
 
237
274
  @shared_task(bind=True, max_retries=K_MAX_RETRY_COUNT)
238
- def dynamic_class_method_executor(self, module_path, class_name, method_name, args=None, kwargs=None, retry=None):
275
+ def dynamic_class_method_executor(self, module_path=None, class_name=None, method_name=None, args=None, kwargs=None, init_args=None, init_kwargs=None, retry=None):
239
276
  job_id = self.request.id
240
277
  try:
241
278
  module = importlib.import_module(module_path)
242
279
  class_obj = getattr(module, class_name)
243
- instance = class_obj()
280
+ init_args = init_args or []
281
+ init_kwargs = init_kwargs or {}
282
+ instance = class_obj(*init_args, **init_kwargs)
244
283
  method = getattr(instance, method_name)
245
284
  args = args or []
246
285
  kwargs = kwargs or {}
@@ -1,9 +0,0 @@
1
- taskqueue/__init__.py,sha256=gVDUAurwUijthE9_36FmhAQTBf7veGgjnew-amrTrmg,241
2
- taskqueue/celery_app.py,sha256=dUT-7XzsSQbr8vKrLv7f_6iYxTCUEJZHEt9fL-KIQ5U,3302
3
- taskqueue/cmanager.py,sha256=jFrunboXWpRP4yFASr62HfezUxJQ2b_V4046q-87L2k,10965
4
- taskqueue/libs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- taskqueue/libs/helper_test.py,sha256=_yfPhm_7CzbN30j7E_Ld5KwXRlGHhTNrRTOBZQySEIU,3656
6
- p1_taskqueue-0.1.5.dist-info/METADATA,sha256=yLSKcc1vB-J51skVqKQGbJ6FPDgrJGDo3bpZ4a9xnX4,1508
7
- p1_taskqueue-0.1.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- p1_taskqueue-0.1.5.dist-info/top_level.txt,sha256=hA3SM1ik2K8iPqtlt_-_nJ4TAePwHPN3vsOc4EiynqU,10
9
- p1_taskqueue-0.1.5.dist-info/RECORD,,