86 Add the cron functions to the cron loop.
89 int: _description_: The overall status of the injection.
97 seconds=CRON_CONST.CHECK_ACTIONS_INTERVAL
103 seconds=CRON_CONST.RESET_FOOD_COUNTERS_INTERVAL
105 if CRON_CONST.ENABLE_TEST_CRONS
is True:
108 args=(datetime.now,),
110 seconds=CRON_CONST.TEST_CRONS_INTERVAL
116 seconds=CRON_CONST.TEST_CRONS_INTERVAL
118 if CRON_CONST.CLEAN_TOKENS
is True:
123 seconds=CRON_CONST.CLEAN_TOKENS_INTERVAL
125 if CRON_CONST.CLEAN_VERIFICATION
is True:
130 seconds=CRON_CONST.CLEAN_VERIFICATION_INTERVAL
132 if CRON_CONST.RENEW_OATH_TOKENS
is True:
137 seconds=CRON_CONST.RENEW_OATH_TOKENS_INTERVAL
192 Remove the tokens that have passed their lifespan.
194 title =
"clean_expired_tokens"
195 date_node =
"expiration_date"
196 current_time = datetime.now()
197 self.
disp.log_info(
"Cleaning expired tokens", title)
199 table=CONST.TAB_CONNECTIONS,
204 if isinstance(current_tokens, int):
205 msg =
"There is no data to be cleared in "
206 msg += f
"{CONST.TAB_CONNECTIONS} table."
207 self.
disp.log_warning(msg, title)
209 self.
disp.log_debug(f
"current tokens = {current_tokens}", title)
210 for i
in current_tokens:
211 if i[date_node]
is not None and i[date_node] !=
"" and isinstance(i[date_node], str)
is True:
215 msg = f
"Converted {i[date_node]} to a datetime instance"
216 msg += f
" ({datetime_node})."
217 self.
disp.log_debug(msg, title)
219 datetime_node = i[date_node]
220 self.
disp.log_debug(f
"Did not convert {i[date_node]}.", title)
221 if datetime_node < current_time:
223 table=CONST.TAB_CONNECTIONS,
224 where=f
"id='{i['id']}'"
226 self.
disp.log_debug(f
"Removed {i}.", title)
229 f
"Did not remove {i} because it is not yet time.", title
231 self.
disp.log_debug(
"Cleaned expired tokens", title)
235 Remove the nodes in the verification table that have passed their lifespan.
237 title =
"clean_expired_verification_nodes"
238 date_node =
"expiration"
239 current_time = datetime.now()
241 f
"Cleaning expired lines in the {CONST.TAB_VERIFICATION} table.",
245 table=CONST.TAB_VERIFICATION,
250 if isinstance(current_lines, int):
251 msg =
"There is no data to be cleared in "
252 msg += f
"{CONST.TAB_VERIFICATION} table."
253 self.
disp.log_warning(
258 self.
disp.log_debug(f
"current lines = {current_lines}", title)
259 for i
in current_lines:
260 if i[date_node]
is not None and i[date_node] !=
"" and isinstance(i[date_node], str)
is True:
264 msg = f
"Converted {i[date_node]} to a datetime instance"
265 msg += f
" ({datetime_node})."
266 self.
disp.log_debug(msg, title)
268 datetime_node = i[date_node]
269 self.
disp.log_debug(f
"Did not convert {i[date_node]}.", title)
270 if datetime_node < current_time:
272 table=CONST.TAB_VERIFICATION,
273 where=f
"id='{i['id']}'"
275 self.
disp.log_debug(f
"Removed {i}.", title)
276 self.
disp.log_debug(
"Cleaned expired lines", title)
280 Function in charge of renewing the oath tokens that are about to expire.
282 title =
"renew_oaths"
284 "Checking for oaths that need to be renewed", title
292 "Boilerplate Non Http class not present, aborting check."
301 "Oauth Authentication class not present, aborting check."
304 oath_connections: Union[List[Dict[str, Any]], int] = self.
database_link.get_data_from_table(
305 table=CONST.TAB_ACTIVE_OAUTHS,
310 if isinstance(oath_connections, int)
or len(oath_connections) == 0:
312 current_time: datetime = datetime.now()
313 for oath
in oath_connections:
314 if oath[
"token_lifespan"] == 0:
316 f
"Token for {oath['id']} does not need to be renewed.", title
319 node_id: str = oath[
'id']
320 expiration_raw = oath[
"token_expiration"]
321 if isinstance(expiration_raw, datetime):
322 token_expiration: datetime = expiration_raw
325 expiration_raw,
False
327 if current_time > token_expiration:
328 renew_link: str = oath[
"refresh_link"]
329 lifespan: int = int(oath[
"token_lifespan"])
330 provider_name: Union[int, List[Dict[str, Any]]] = self.
database_link.get_data_from_table(
331 table=CONST.TAB_SERVICES,
333 where=f
"id='{oath['service_id']}'",
336 if isinstance(provider_name, int):
338 f
"Could not find provider name for {node_id}", title
342 provider_list = provider_name
343 if not provider_list:
345 f
"Empty provider list for {node_id}", title
348 provider_node = provider_list[0]
350 provider_node.get(
'name',
''),
353 if new_token
is None:
355 "Refresh token failed to generate a new token.", title)
361 token_expiration_str: str = self.
database_link.datetime_to_string(
362 datetime_instance=new_expiration_dt,
367 f
"token expiration = {token_expiration_str}", title
371 table=CONST.TAB_ACTIVE_OAUTHS,
380 where=f
"id='{node_id}'"
383 f
"token {new_token} updated for {node_id}"
386 self.
disp.log_error(f
"Could not renew token for {node_id}")
389 f
"Token for {node_id} does not need to be renewed.", title
391 self.
disp.log_debug(
"Checked for oath that need to be renewed", title)