@@ -22,6 +22,10 @@ class Singleton(BaseTask):
22
22
raise_on_duplicate = None
23
23
lock_expiry = None
24
24
25
+ def __init__ (self , * args , ** kwargs ):
26
+ self ._unlock_to_super_retry = False
27
+ super (Singleton , self ).__init__ (* args , ** kwargs )
28
+
25
29
@property
26
30
def _raise_on_duplicate (self ):
27
31
if self .raise_on_duplicate is not None :
@@ -75,17 +79,8 @@ def generate_lock(self, task_name, task_args=None, task_kwargs=None):
75
79
key_prefix = self .singleton_config .key_prefix ,
76
80
)
77
81
78
- def apply_async (
79
- self ,
80
- args = None ,
81
- kwargs = None ,
82
- task_id = None ,
83
- producer = None ,
84
- link = None ,
85
- link_error = None ,
86
- shadow = None ,
87
- ** options
88
- ):
82
+ def apply_async (self , args = None , kwargs = None , task_id = None , producer = None ,
83
+ link = None , link_error = None , shadow = None , ** options ):
89
84
args = args or []
90
85
kwargs = kwargs or {}
91
86
task_id = task_id or uuid ()
@@ -117,14 +112,15 @@ def apply_async(
117
112
118
113
def lock_and_run (self , lock , * args , task_id = None , ** kwargs ):
119
114
lock_aquired = self .aquire_lock (lock , task_id )
120
- if lock_aquired :
115
+ if lock_aquired or self . _unlock_to_super_retry :
121
116
try :
122
117
return super (Singleton , self ).apply_async (
123
118
* args , task_id = task_id , ** kwargs
124
119
)
125
120
except Exception :
126
121
# Clear the lock if apply_async fails
127
- self .unlock (lock )
122
+ if lock_aquired :
123
+ self .unlock (lock )
128
124
raise
129
125
130
126
def release_lock (self , task_args = None , task_kwargs = None ):
@@ -137,7 +133,9 @@ def unlock(self, lock):
137
133
def on_duplicate (self , existing_task_id ):
138
134
if self ._raise_on_duplicate :
139
135
raise DuplicateTaskError (
140
- "Attempted to queue a duplicate of task ID {}" .format (existing_task_id ),
136
+ "Attempted to queue a duplicate of task ID {}" .format (
137
+ existing_task_id
138
+ ),
141
139
task_id = existing_task_id ,
142
140
)
143
141
return self .AsyncResult (existing_task_id )
@@ -147,3 +145,13 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
147
145
148
146
def on_success (self , retval , task_id , args , kwargs ):
149
147
self .release_lock (task_args = args , task_kwargs = kwargs )
148
+
149
+ def retry (self , args = None , kwargs = None , exc = None , throw = True ,
150
+ eta = None , countdown = None , max_retries = None , ** options ):
151
+ self ._unlock_to_super_retry = True
152
+ retry_task = super (Singleton , self ).retry (
153
+ args , kwargs , exc , throw , eta , countdown , max_retries , ** options
154
+ )
155
+ self ._unlock_to_super_retry = False
156
+
157
+ return retry_task
0 commit comments