43 Add the cron functions to the cron loop.
46 int: _description_: The overall status of the injection.
48 self.
runtime_data.background_tasks_initialised.safe_add_task(
52 seconds=CONST.CHECK_ACTIONS_INTERVAL
54 if CONST.ENABLE_TEST_CRONS
is True:
55 self.
runtime_data.background_tasks_initialised.safe_add_task(
59 seconds=CONST.TEST_CRONS_INTERVAL
61 self.
runtime_data.background_tasks_initialised.safe_add_task(
65 seconds=CONST.TEST_CRONS_INTERVAL
67 self.
runtime_data.background_tasks_initialised.safe_add_task(
71 seconds=CONST.TEST_CRONS_INTERVAL
73 if CONST.CLEAN_TOKENS
is True:
74 self.
runtime_data.background_tasks_initialised.safe_add_task(
78 seconds=CONST.CLEAN_TOKENS_INTERVAL
80 if CONST.CLEAN_VERIFICATION
is True:
81 self.
runtime_data.background_tasks_initialised.safe_add_task(
85 seconds=CONST.CLEAN_VERIFICATION_INTERVAL
87 if CONST.RENEW_OATH_TOKENS
is True:
88 self.
runtime_data.background_tasks_initialised.safe_add_task(
92 seconds=CONST.RENEW_OATH_TOKENS_INTERVAL
124 This is a test function that will print the current date.
126 date (datetime): _description_
128 title =
"_harass_database"
129 self.
disp.log_info(f
"In {title}", title)
131 connection = self.
runtime_data.database_link.is_connected()
133 f
"Connection status: {connection}",
136 if connection
is False:
138 connection = self.
runtime_data.database_link.is_connected()
140 f
"Connection status: {connection}",
143 connection = self.
runtime_data.database_link.is_connected()
145 f
"Connection status: {connection}",
148 if connection
is True:
149 data = self.
runtime_data.database_link.get_table_names()
151 f
"Data from {CONST.TAB_CONNECTIONS}: {data}",
155 CONST.TAB_CONNECTIONS
158 f
"Data from {CONST.TAB_CONNECTIONS}: {data}",
161 data = self.
runtime_data.database_link.get_data_from_table(
162 CONST.TAB_CONNECTIONS,
"*",
"",
True
165 f
"Data from {CONST.TAB_CONNECTIONS}: {data}",
168 data = self.
runtime_data.database_link.get_table_column_names(
169 CONST.TAB_CONNECTIONS
172 f
"Data from {CONST.TAB_CONNECTIONS}: {data}",
176 CONST.TAB_CONNECTIONS,
"*",
""
179 f
"Data from {CONST.TAB_CONNECTIONS}: {data}",
182 self.
disp.log_info(f
"Out of {title}.", title)
186 Remove the tokens that have passed their lifespan.
188 title =
"clean_expired_tokens"
189 date_node =
"expiration_date"
190 current_time = datetime.now()
191 self.
disp.log_info(
"Cleaning expired tokens", title)
192 current_tokens = self.
runtime_data.database_link.get_data_from_table(
193 table=CONST.TAB_CONNECTIONS,
198 if isinstance(current_tokens, int)
is True:
199 msg =
"There is no data to be cleared in "
200 msg += f
"{CONST.TAB_CONNECTIONS} table."
201 self.
disp.log_warning(msg, title)
203 self.
disp.log_debug(f
"current tokens = {current_tokens}", title)
204 for i
in current_tokens:
205 if i[date_node]
is not None and i[date_node] !=
"" and isinstance(i[date_node], str)
is True:
206 datetime_node = self.
runtime_data.database_link.string_to_datetime(
209 msg = f
"Converted {i[date_node]} to a datetime instance"
210 msg += f
" ({datetime_node})."
211 self.
disp.log_debug(msg, title)
213 datetime_node = i[date_node]
214 self.
disp.log_debug(f
"Did not convert {i[date_node]}.", title)
215 if datetime_node < current_time:
217 table=CONST.TAB_CONNECTIONS,
218 where=f
"id='{i['id']}'"
220 self.
disp.log_debug(f
"Removed {i}.", title)
223 f
"Did not remove {i} because it is not yet time.", title
225 self.
disp.log_debug(
"Cleaned expired tokens", title)
229 Remove the nodes in the verification table that have passed their lifespan.
231 title =
"clean_expired_verification_nodes"
232 date_node =
"expiration"
233 current_time = datetime.now()
235 f
"Cleaning expired lines in the {CONST.TAB_VERIFICATION} table.",
238 current_lines = self.
runtime_data.database_link.get_data_from_table(
239 table=CONST.TAB_VERIFICATION,
244 if isinstance(current_lines, int)
is True:
245 msg =
"There is no data to be cleared in "
246 msg += f
"{CONST.TAB_VERIFICATION} table."
247 self.
disp.log_warning(
252 self.
disp.log_debug(f
"current lines = {current_lines}", title)
253 for i
in current_lines:
254 if i[date_node]
is not None and i[date_node] !=
"" and isinstance(i[date_node], str)
is True:
255 datetime_node = self.
runtime_data.database_link.string_to_datetime(
258 msg = f
"Converted {i[date_node]} to a datetime instance"
259 msg += f
" ({datetime_node})."
260 self.
disp.log_debug(msg, title)
262 datetime_node = i[date_node]
263 self.
disp.log_debug(f
"Did not convert {i[date_node]}.", title)
264 if datetime_node < current_time:
266 table=CONST.TAB_VERIFICATION,
267 where=f
"id='{i['id']}'"
269 self.
disp.log_debug(f
"Removed {i}.", title)
270 self.
disp.log_debug(
"Cleaned expired lines", title)
274 Function in charge of renewing the oath tokens that are about to expire.
276 title =
"renew_oaths"
278 "Checking for oaths that need to be renewed", title
280 oath_connections: Union[List[Dict[str]], int] = self.
runtime_data.database_link.get_data_from_table(
281 table=CONST.TAB_ACTIVE_OAUTHS,
286 if isinstance(oath_connections, int)
or len(oath_connections) == 0:
288 current_time: datetime = datetime.now()
289 for oath
in oath_connections:
290 if oath[
"token_lifespan"] == 0:
291 self.
disp.log_debug(f
"Token for {oath['id']} does not need to be renewed.", title)
293 node_id: str = oath[
'id']
294 token_expiration: datetime = oath[
"token_expiration"]
295 if current_time > token_expiration:
296 renew_link: str = oath[
"refresh_link"]
297 lifespan: int = int(oath[
"token_lifespan"])
300 ] = self.
runtime_data.database_link.get_data_from_table(
301 table=CONST.TAB_SERVICES,
303 where=f
"id='{oath['service_id']}'",
306 if isinstance(provider_name, int)
is True:
308 f
"Could not find provider name for {node_id}", title
311 new_token: Union[str,
None] = self.
runtime_data.oauth_authentication_initialised.refresh_token(
312 provider_name[0][
'name'],
315 if new_token
is None:
316 self.
disp.log_debug(
"Refresh token failed to generate a new token.", title)
318 token_expiration: str = self.
runtime_data.database_link.datetime_to_string(
319 datetime_instance=self.
runtime_data.boilerplate_non_http_initialised.set_lifespan(
326 f
"token expiration = {token_expiration}", title
330 table=CONST.TAB_ACTIVE_OAUTHS,
339 where=f
"id='{node_id}'"
342 f
"token {new_token} updated for {node_id}"
345 self.
disp.log_error(f
"Could not renew token for {node_id}")
348 f
"Token for {node_id} does not need to be renewed.", title
350 self.
disp.log_debug(
"Checked for oath that need to be renewed", title)