11# -*- coding: utf-8 -*-
2- """
3- * TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4- * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5- * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at http://opensource.org/licenses/MIT
7- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9- * specific language governing permissions and limitations under the License.
10- """
2+ # TencentBlueKing is pleased to support the open source community by making
3+ # 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available.
4+ # Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
5+ # Licensed under the MIT License (the "License"); you may not use this file except
6+ # in compliance with the License. You may obtain a copy of the License at
7+ #
8+ # http://opensource.org/licenses/MIT
9+ #
10+ # Unless required by applicable law or agreed to in writing, software distributed under
11+ # the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+ # either express or implied. See the License for the specific language governing permissions and
13+ # limitations under the License.
14+ #
15+ # We undertake not to change the open source license (MIT license) applicable
16+ # to the current version of the project delivered to anyone in the future.
17+
1118
1219import logging
1320import time
@@ -41,7 +48,7 @@ def __init__(self, status: PollingStatus, data: Optional[Any] = None):
4148 self .data = data
4249
4350 def __str__ (self ):
44- return f' stauts={ self .status } data={ self .data } '
51+ return f" stauts={ self .status } data={ self .data } "
4552
4653 @classmethod
4754 def doing (cls , * args , ** kwargs ):
@@ -73,7 +80,7 @@ class TaskPoller(ABC):
7380 :param metadata: metadata object of current poller
7481 """
7582
76- _registered_pollers : Dict [str , Type [' TaskPoller' ]] = {}
83+ _registered_pollers : Dict [str , Type [" TaskPoller" ]] = {}
7784
7885 max_retries_on_error = 10
7986 overall_timeout_seconds = 3600 * 24 * 7
@@ -87,7 +94,7 @@ def __init_subclass__(cls, *args, **kwargs):
8794 cls ._registered_pollers [cls .__name__ ] = cls
8895
8996 @classmethod
90- def get_poller_cls (cls , name : str ) -> Type [' TaskPoller' ]:
97+ def get_poller_cls (cls , name : str ) -> Type [" TaskPoller" ]:
9198 return cls ._registered_pollers [name ]
9299
93100 @classmethod
@@ -145,10 +152,10 @@ def exceeded_max_retries(self) -> bool:
145152 return (self .metadata .retries + 1 ) > self .max_retries_on_error
146153
147154 def __str__ (self ):
148- return ' <%s: params=%s>' % (self .__class__ .__name__ , self .params )
155+ return " <%s: params=%s>" % (self .__class__ .__name__ , self .params )
149156
150157 @classmethod
151- def get_async_task (self ) -> Any :
158+ def get_async_task (cls ) -> Any :
152159 """Return the async celery task object for polling in backend"""
153160 return check_status_until_finished
154161
@@ -182,10 +189,10 @@ def is_exception(self):
182189 return self .status .is_exception ()
183190
184191 def to_dict (self ):
185- return {' status' : self .status .value , ' message' : self .message , ' data' : self .data }
192+ return {" status" : self .status .value , " message" : self .message , " data" : self .data }
186193
187194 def __str__ (self ):
188- return ' <%s: %s is_exception=%s>' % (self .__class__ .__name__ , self .to_dict (), self .is_exception )
195+ return " <%s: %s is_exception=%s>" % (self .__class__ .__name__ , self .to_dict (), self .is_exception )
189196
190197
191198class CallbackHandler (ABC ):
@@ -194,13 +201,13 @@ class CallbackHandler(ABC):
194201 :params: params of current polling result
195202 """
196203
197- _registered_handlers : Dict [str , Type [' CallbackHandler' ]] = {}
204+ _registered_handlers : Dict [str , Type [" CallbackHandler" ]] = {}
198205
199206 def __init_subclass__ (cls , * args , ** kwargs ):
200207 cls ._registered_handlers [cls .__name__ ] = cls
201208
202209 @classmethod
203- def get_handler_cls (cls , name : str ) -> Type [' CallbackHandler' ]:
210+ def get_handler_cls (cls , name : str ) -> Type [" CallbackHandler" ]:
204211 return cls ._registered_handlers [name ]
205212
206213 @abstractmethod
@@ -220,7 +227,7 @@ def handle(self, result: CallbackResult, poller: TaskPoller):
220227 pass
221228
222229
223- @shared_task (acks_late = True , name = ' poll_task.check_status_until_finished' )
230+ @shared_task (acks_late = True , name = " poll_task.check_status_until_finished" )
224231def check_status_until_finished (poller_name : str , handler_name : str , params : Dict , queue : Optional [str ] = None ):
225232 """Main async task for polling
226233
@@ -232,9 +239,9 @@ def check_status_until_finished(poller_name: str, handler_name: str, params: Dic
232239 req = check_status_until_finished .request
233240 metadata = PollingMetadata (
234241 retries = req .retries ,
235- query_started_at = req .get (' query_started_at' , time .time ()),
236- queried_count = req .get (' queried_count' , 0 ),
237- last_polling_data = req .get (' last_polling_data' ),
242+ query_started_at = req .get (" query_started_at" , time .time ()),
243+ queried_count = req .get (" queried_count" , 0 ),
244+ last_polling_data = req .get (" last_polling_data" ),
238245 )
239246
240247 # Make handler and poller by name
@@ -250,18 +257,18 @@ def check_status_until_finished(poller_name: str, handler_name: str, params: Dic
250257 if next_metadata :
251258 # Start next polling
252259 countdown = poller .get_retry_delay ()
253- logger .debug (' Will retry query status for %s after %s seconds. metadata=%s' , poller , countdown , metadata )
260+ logger .debug (" Will retry query status for %s after %s seconds. metadata=%s" , poller , countdown , metadata )
254261 poller .get_async_task ().subtask (
255262 args = (poller_name , handler_name , params ),
256- kwargs = {' queue' : queue },
263+ kwargs = {" queue" : queue },
257264 countdown = countdown ,
258265 retries = next_metadata .retries ,
259266 queue = queue ,
260267 ).apply_async (
261268 headers = {
262- ' queried_count' : next_metadata .queried_count ,
263- ' query_started_at' : next_metadata .query_started_at ,
264- ' last_polling_data' : next_metadata .last_polling_data ,
269+ " queried_count" : next_metadata .queried_count ,
270+ " query_started_at" : next_metadata .query_started_at ,
271+ " last_polling_data" : next_metadata .last_polling_data ,
265272 }
266273 )
267274
@@ -280,7 +287,7 @@ def __init__(self, poller: TaskPoller, handler_cls: Type[CallbackHandler]):
280287 def run (self ) -> Optional [PollingMetadata ]:
281288 """Start schedule process"""
282289 if self .poller .exceeded_timeout ():
283- logger .info (' exceeded total timeout, ts_query_started=%s' % self .poller .metadata .query_started_at )
290+ logger .info (" exceeded total timeout, ts_query_started=%s" , self .poller .metadata .query_started_at )
284291 self ._callback_timeout ()
285292 return None
286293
@@ -314,10 +321,10 @@ def _safe_query(poller: TaskPoller) -> PollingResult:
314321 try :
315322 polling_result = poller .query ()
316323 except Exception as e :
317- logger .exception (' Exception when query status, poll_class=%s' % poller )
324+ logger .exception (" Exception when query status, poll_class=%s" , poller )
318325 raise PollingQueryError (str (e ))
319326
320- logger .debug (' Query status result, poll_class=%s, polling result: %s' % ( poller , polling_result ) )
327+ logger .debug (" Query status result, poll_class=%s, polling result: %s" , poller , polling_result )
321328 return polling_result
322329
323330 def _callback (self , result : CallbackResult ):
@@ -326,10 +333,10 @@ def _callback(self, result: CallbackResult):
326333
327334 def _callback_timeout (self ):
328335 """Callback handler with timeout result"""
329- ret = CallbackResult (status = CallbackStatus .TIMEOUT , message = ' exceeded total timeout' )
336+ ret = CallbackResult (status = CallbackStatus .TIMEOUT , message = " exceeded total timeout" )
330337 self .handler_cls ().handle (ret , self .poller )
331338
332339 def _callback_exception (self , e : Exception ):
333340 """Callback handler when exceeds max retries"""
334- ret = CallbackResult (status = CallbackStatus .EXCEPTION , message = f' exception: { e } ' )
341+ ret = CallbackResult (status = CallbackStatus .EXCEPTION , message = f" exception: { e } " )
335342 self .handler_cls ().handle (ret , self .poller )
0 commit comments