brynq-sdk-task-scheduler 2.0.1__tar.gz → 3.0.0__tar.gz
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.
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/PKG-INFO +1 -1
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/brynq_sdk_task_scheduler.egg-info/PKG-INFO +1 -1
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/brynq_sdk_task_scheduler.egg-info/SOURCES.txt +0 -2
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/brynq_sdk_task_scheduler.egg-info/requires.txt +1 -1
- brynq_sdk_task_scheduler-3.0.0/brynq_sdk_task_scheduler.egg-info/top_level.txt +1 -0
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/setup.py +4 -5
- brynq_sdk_task_scheduler-2.0.1/brynq_sdk/task_scheduler/__init__.py +0 -1
- brynq_sdk_task_scheduler-2.0.1/brynq_sdk/task_scheduler/task_scheduler.py +0 -447
- brynq_sdk_task_scheduler-2.0.1/brynq_sdk_task_scheduler.egg-info/top_level.txt +0 -1
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/brynq_sdk_task_scheduler.egg-info/dependency_links.txt +0 -0
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/brynq_sdk_task_scheduler.egg-info/not-zip-safe +0 -0
- {brynq_sdk_task_scheduler-2.0.1 → brynq_sdk_task_scheduler-3.0.0}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
from setuptools import setup
|
|
2
|
-
|
|
1
|
+
from setuptools import setup, find_namespace_packages
|
|
3
2
|
|
|
4
3
|
setup(
|
|
5
4
|
name='brynq_sdk_task_scheduler',
|
|
6
|
-
version='
|
|
5
|
+
version='3.0.0',
|
|
7
6
|
description='Code to execute tasks in BrynQ.com with the task scheduler',
|
|
8
7
|
long_description='Code to execute tasks in the BrynQ.com platform with the task scheduler',
|
|
9
8
|
author='BrynQ',
|
|
10
9
|
author_email='support@brynq.com',
|
|
11
|
-
packages=[
|
|
10
|
+
packages=find_namespace_packages(include=['brynq_sdk*']),
|
|
12
11
|
license='BrynQ License',
|
|
13
12
|
install_requires=[
|
|
14
13
|
'brynq-sdk-brynq>=1',
|
|
15
14
|
'brynq-sdk-functions>=1',
|
|
16
15
|
'brynq-sdk-mysql>=1',
|
|
17
16
|
'brynq-sdk-mandrill>=1',
|
|
18
|
-
'brynq-sdk-elastic>=
|
|
17
|
+
'brynq-sdk-elastic>=3'
|
|
19
18
|
],
|
|
20
19
|
zip_safe=False,
|
|
21
20
|
)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from brynq_sdk.task_scheduler.task_scheduler import TaskScheduler
|
|
@@ -1,447 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
import os
|
|
3
|
-
import datetime
|
|
4
|
-
import inspect
|
|
5
|
-
import time
|
|
6
|
-
import typing
|
|
7
|
-
import traceback
|
|
8
|
-
import pandas as pd
|
|
9
|
-
import json
|
|
10
|
-
import requests
|
|
11
|
-
from brynq_sdk.mandrill import MailClient
|
|
12
|
-
from brynq_sdk.functions import Functions
|
|
13
|
-
from brynq_sdk.brynq import BrynQ
|
|
14
|
-
from brynq_sdk.mysql import MySQL
|
|
15
|
-
from brynq_sdk.elastic import Elastic
|
|
16
|
-
import warnings
|
|
17
|
-
import re
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class TaskScheduler(BrynQ):
|
|
21
|
-
|
|
22
|
-
def __init__(self, task_id: int = None, loglevel: str = 'INFO', email_after_errors: bool = False):
|
|
23
|
-
"""
|
|
24
|
-
The TaskScheduler is responsible for the logging to the database. Based on this logging, the next reload will
|
|
25
|
-
start or not and warning will be given or not
|
|
26
|
-
:param task_id: The ID from the task as saved in the task_scheduler table in the customer database
|
|
27
|
-
:param email_after_errors: a True or False value. When True, there will be send an email to a contactperson of the customer (as given in the database) with the number of errors
|
|
28
|
-
:param loglevel: Chose on which level you want to store the logs. Default is INFO. that means that a logline
|
|
29
|
-
with level DEBUG not is stored
|
|
30
|
-
"""
|
|
31
|
-
super().__init__()
|
|
32
|
-
self.mysql = MySQL()
|
|
33
|
-
try:
|
|
34
|
-
self.es = Elastic()
|
|
35
|
-
self.email_after_errors = email_after_errors
|
|
36
|
-
self.customer_db = self.mysql.database
|
|
37
|
-
self.customer_id = self.mysql.raw_query(f'SELECT id FROM sc.customers WHERE dbname = \'{self.customer_db}\'')[0][0]
|
|
38
|
-
self.customer = os.getenv('BRYNQ_SUBDOMAIN').lower().replace(' ', '_')
|
|
39
|
-
self.partner_id = os.getenv('PARTNER_ID').lower().replace(' ', '_') if os.getenv('PARTNER_ID') else 'brynq'
|
|
40
|
-
self.write_logs_to_mysql = os.getenv('WRITE_LOGS_TO_MSYQL') if os.getenv('WRITE_LOGS_TO_MSYQL') else False
|
|
41
|
-
self.task_id = task_id
|
|
42
|
-
self.loglevel = loglevel
|
|
43
|
-
self.started_at = datetime.datetime.now()
|
|
44
|
-
# If the task is started via the task_scheduler, the following 3 parameters will be passed by the scheduler
|
|
45
|
-
if len(sys.argv[1:4]) > 0:
|
|
46
|
-
self.started_local = False
|
|
47
|
-
self.customer_db, self.task_id, self.run_id = sys.argv[1:4]
|
|
48
|
-
# If the task is started locally, the parameters should be set locally
|
|
49
|
-
else:
|
|
50
|
-
self.started_local = True
|
|
51
|
-
self.run_id = int(round(time.time() * 100000))
|
|
52
|
-
print(self.task_id, self.run_id)
|
|
53
|
-
self.error_count = 0
|
|
54
|
-
|
|
55
|
-
# Check if the log tables exists in the customer database. If not, create them
|
|
56
|
-
# Mysql throws a warning when a table already exists. We don't care so we ignore warnings. (not exceptions!)
|
|
57
|
-
warnings.filterwarnings('ignore')
|
|
58
|
-
|
|
59
|
-
# Check if the task is started on schedule or manual. store in a variable to use later in the script
|
|
60
|
-
self.task_manual_started = self.check_if_task_manual_started()
|
|
61
|
-
|
|
62
|
-
# Creates Elasticsearch index and data view if not exists
|
|
63
|
-
self.es_index = f"task_execution_log_{self.customer_db}"
|
|
64
|
-
self.es.create_index(index_name=self.es_index)
|
|
65
|
-
self.es.create_data_view(space_name='interfaces', view_name=f'task_execution_log_{self.customer_db}', name=f'Task execution log {self.customer_db}', time_field='started_at')
|
|
66
|
-
|
|
67
|
-
# Start the task and setup the data in the database
|
|
68
|
-
self.start_task()
|
|
69
|
-
except Exception as e:
|
|
70
|
-
self.error_handling(e)
|
|
71
|
-
|
|
72
|
-
def __count_keys(self, json_obj):
|
|
73
|
-
if not isinstance(json_obj, dict):
|
|
74
|
-
return 0
|
|
75
|
-
key_count = 0
|
|
76
|
-
for key, value in json_obj.items():
|
|
77
|
-
if not isinstance(value, dict):
|
|
78
|
-
key_count += 1 # Count the current key
|
|
79
|
-
else:
|
|
80
|
-
key_count += self.__count_keys(value) # Recursively count keys in nested dictionaries
|
|
81
|
-
return key_count
|
|
82
|
-
|
|
83
|
-
def __get_caller_info(self):
|
|
84
|
-
stack = inspect.stack()
|
|
85
|
-
caller_frame = stack[2][0]
|
|
86
|
-
file_name = caller_frame.f_code.co_filename
|
|
87
|
-
line_number = caller_frame.f_lineno
|
|
88
|
-
function_name = stack[2][3]
|
|
89
|
-
return file_name, line_number, function_name
|
|
90
|
-
|
|
91
|
-
def create_task_execution_steps(self, step_details: list):
|
|
92
|
-
"""
|
|
93
|
-
Check if the given steps already exists in the task_execution_steps table. If not, update or insert the values in the table
|
|
94
|
-
:param step_details: list of dicts. Each dict must contain task details according to required_fields.
|
|
95
|
-
Example: step_details = [
|
|
96
|
-
{'nr': 1, 'description': 'test'},
|
|
97
|
-
{'nr': 2, 'description': 'test2'}
|
|
98
|
-
]
|
|
99
|
-
:return: error (str) or response of mysql
|
|
100
|
-
"""
|
|
101
|
-
# Check if the required fields are available in the given list
|
|
102
|
-
required_fields = ['nr', 'description']
|
|
103
|
-
for step in step_details:
|
|
104
|
-
for field in required_fields:
|
|
105
|
-
if field not in step.keys():
|
|
106
|
-
return 'Field {field} is required in step {step}. Required fields are: {required_fields}'.format(
|
|
107
|
-
field=field, step=step, required_fields=tuple(required_fields))
|
|
108
|
-
|
|
109
|
-
# Reformat the list of dictionaries to a valid MySQL query
|
|
110
|
-
values = ','.join(str((self.task_id, step['nr'], step['description'])) for step in step_details)
|
|
111
|
-
response = self.mysql.raw_query("INSERT INTO task_execution_steps (`task_id`, `nr`, `description`) "
|
|
112
|
-
"VALUES {step_values} ON DUPLICATE KEY UPDATE `description` = VALUES(description)".format(
|
|
113
|
-
step_values=values), insert=True)
|
|
114
|
-
return response
|
|
115
|
-
|
|
116
|
-
def check_if_task_manual_started(self):
|
|
117
|
-
"""
|
|
118
|
-
Check if the task manual is started of on schedule. If it's manual started, that's important for the variables in the db_variables function.
|
|
119
|
-
In that case the dynamic variables should be used instead of the static ones
|
|
120
|
-
:return: True of False
|
|
121
|
-
"""
|
|
122
|
-
response = self.mysql.select('task_scheduler', 'run_instant', f'WHERE id = {self.task_id}')[0][0]
|
|
123
|
-
if response == 1:
|
|
124
|
-
# Reset the 1 back to 0 before sending the result
|
|
125
|
-
self.mysql.update('task_scheduler', ['run_instant'], [0], 'WHERE `id` = {}'.format(self.task_id))
|
|
126
|
-
return True
|
|
127
|
-
else:
|
|
128
|
-
return False
|
|
129
|
-
|
|
130
|
-
def start_task(self):
|
|
131
|
-
"""
|
|
132
|
-
Start the task and write this to the database. While the status is running, the task will not start again
|
|
133
|
-
:return: if the update to the database is successful or not
|
|
134
|
-
"""
|
|
135
|
-
# If the task is started from a local instance (not the task_scheduler), create a start log row in the task_scheduler_log
|
|
136
|
-
if self.started_local:
|
|
137
|
-
self.mysql.raw_query(f"INSERT INTO `task_scheduler_log` (reload_id, task_id, reload_status, started_at, finished_at) VALUES ({self.run_id}, {self.task_id}, 'Running', '{self.started_at}', null)", insert=True)
|
|
138
|
-
return self.mysql.update('task_scheduler', ['status', 'step_nr'], ['RUNNING', 1], 'WHERE `id` = {}'.format(self.task_id))
|
|
139
|
-
|
|
140
|
-
def db_variable(self, variable_name: str, default_value_if_temp_is_empty: bool = False):
|
|
141
|
-
"""
|
|
142
|
-
Get a value from the task_variables table corresponding with the given name. If the task is manually started
|
|
143
|
-
(run_instant = 1), then the temp_value will be returned. This is to give the possibility for users in the frontend to run
|
|
144
|
-
a task once manual with other values then normal without overwriting the normal values.
|
|
145
|
-
:param variable_name: the name of the variable
|
|
146
|
-
:param default_value_if_temp_is_empty: bool to determine whether default value should be used if temp value is empty when manually started
|
|
147
|
-
:return: the value of the given variable.
|
|
148
|
-
"""
|
|
149
|
-
if self.task_manual_started is True:
|
|
150
|
-
response = self.mysql.select('task_variables', 'temp_value, value',
|
|
151
|
-
f'WHERE name = \'{variable_name}\' AND task_id = {self.task_id}')
|
|
152
|
-
else:
|
|
153
|
-
response = self.mysql.select('task_variables', 'value',
|
|
154
|
-
f'WHERE name = \'{variable_name}\' AND task_id = {self.task_id}')
|
|
155
|
-
if len(response) == 0:
|
|
156
|
-
raise Exception(f'Variable with name \'{variable_name}\' does not exist')
|
|
157
|
-
else:
|
|
158
|
-
value = response[0][0]
|
|
159
|
-
if value is None and default_value_if_temp_is_empty is True and len(response[0]) > 0:
|
|
160
|
-
value = response[0][1]
|
|
161
|
-
return value
|
|
162
|
-
|
|
163
|
-
def write_execution_log(self, message: str, data, loglevel: str = 'INFO', full_extract: bool = False):
|
|
164
|
-
"""
|
|
165
|
-
Writes messages to the database. Give the message and the level of the log
|
|
166
|
-
:param message: A string with a message for the log
|
|
167
|
-
:param loglevel: You can choose between DEBUG, INFO, ERROR or CRITICAL (DEBUG is most granulated, CRITICAL the less)
|
|
168
|
-
:param data: Uploaded data by the interface that has to be logged in ElasticSearch, if you have nothing to log, use None
|
|
169
|
-
:param full_extract: If the data is a full load, set this to True. This will prevent the payload from being logged in ElasticSearch
|
|
170
|
-
:return: If writing to the database is successful or not
|
|
171
|
-
"""
|
|
172
|
-
|
|
173
|
-
# Validate if the provided loglevel is valid
|
|
174
|
-
allowed_loglevels = ['DEBUG', 'INFO', 'ERROR', 'CRITICAL']
|
|
175
|
-
if loglevel not in allowed_loglevels:
|
|
176
|
-
raise Exception('You\'ve entered a not allowed loglevel. Choose one of: {}'.format(allowed_loglevels))
|
|
177
|
-
|
|
178
|
-
# Count the errors for relevant log levels
|
|
179
|
-
if loglevel == 'ERROR' or loglevel == 'CRITICAL':
|
|
180
|
-
self.error_count += 1
|
|
181
|
-
|
|
182
|
-
# For Elastic, we need to have the data in JSON format. Handling different data types and preparing extra payload information based on the data type
|
|
183
|
-
# If the data is just a series, count rows, columns and cells
|
|
184
|
-
if isinstance(data, pd.Series):
|
|
185
|
-
dataframe = pd.DataFrame(data).T
|
|
186
|
-
extra_payload = {
|
|
187
|
-
'rows': len(dataframe),
|
|
188
|
-
'columns': len(dataframe.columns),
|
|
189
|
-
'cells': len(dataframe) * len(dataframe.columns),
|
|
190
|
-
}
|
|
191
|
-
if not full_extract:
|
|
192
|
-
extra_payload['payload'] = dataframe.to_json(orient='records')
|
|
193
|
-
# If the data is a list, count rows, columns and cells
|
|
194
|
-
elif isinstance(data, dict):
|
|
195
|
-
records = self.__count_keys(data)
|
|
196
|
-
extra_payload = {
|
|
197
|
-
'rows': 1,
|
|
198
|
-
'columns': records,
|
|
199
|
-
'cells': records,
|
|
200
|
-
}
|
|
201
|
-
if not full_extract:
|
|
202
|
-
extra_payload['payload'] = data
|
|
203
|
-
elif isinstance(data, pd.DataFrame):
|
|
204
|
-
extra_payload = {
|
|
205
|
-
'rows': len(data),
|
|
206
|
-
'columns': len(data.columns),
|
|
207
|
-
'cells': len(data) * len(data.columns),
|
|
208
|
-
}
|
|
209
|
-
if not full_extract:
|
|
210
|
-
extra_payload['payload'] = data.to_json(orient='records')
|
|
211
|
-
# If the data is a response from an URL request, also store all the information about the URL request.
|
|
212
|
-
elif isinstance(data, requests.Response):
|
|
213
|
-
records = 1
|
|
214
|
-
if data.request.body is not None:
|
|
215
|
-
records = self.__count_keys(json.loads(data.request.body))
|
|
216
|
-
if isinstance(data.request.body, bytes):
|
|
217
|
-
data.request.body = data.request.body.decode('utf-8')
|
|
218
|
-
extra_payload = {
|
|
219
|
-
'response': data.text,
|
|
220
|
-
'status_code': data.status_code,
|
|
221
|
-
'url': data.url,
|
|
222
|
-
'method': data.request.method,
|
|
223
|
-
'rows': 1,
|
|
224
|
-
'columns': records,
|
|
225
|
-
'cells': records,
|
|
226
|
-
}
|
|
227
|
-
if not full_extract:
|
|
228
|
-
extra_payload['payload'] = data.request.body
|
|
229
|
-
elif data is None:
|
|
230
|
-
extra_payload = {}
|
|
231
|
-
else:
|
|
232
|
-
extra_payload = {
|
|
233
|
-
'data_type': str(type(data)),
|
|
234
|
-
}
|
|
235
|
-
if not full_extract:
|
|
236
|
-
extra_payload['payload'] = data
|
|
237
|
-
|
|
238
|
-
# Modify payload based on 'full_load' flag
|
|
239
|
-
if data is not None and full_extract is True:
|
|
240
|
-
extra_payload['full_load'] = True
|
|
241
|
-
elif data is not None and full_extract is False:
|
|
242
|
-
extra_payload['full_load'] = False
|
|
243
|
-
|
|
244
|
-
# Get the linenumber from where the logline is executed.
|
|
245
|
-
file_name, line_number, function_name = self.__get_caller_info()
|
|
246
|
-
|
|
247
|
-
# Put everything together in the payload for ElasticSearch and send it
|
|
248
|
-
payload = {
|
|
249
|
-
'task_id': self.task_id,
|
|
250
|
-
'reload_id': self.run_id,
|
|
251
|
-
'started_at': datetime.datetime.now().isoformat(),
|
|
252
|
-
'partner_id': self.partner_id,
|
|
253
|
-
'customer_id': self.customer_id,
|
|
254
|
-
'customer': self.customer,
|
|
255
|
-
'file_name': file_name,
|
|
256
|
-
'function_name': function_name,
|
|
257
|
-
'line_number': line_number,
|
|
258
|
-
'task_loglevel': self.loglevel,
|
|
259
|
-
'line_loglevel': loglevel,
|
|
260
|
-
'message': message
|
|
261
|
-
}
|
|
262
|
-
payload.update(extra_payload)
|
|
263
|
-
self.es.post_document(index_name=self.es_index, document=payload)
|
|
264
|
-
|
|
265
|
-
# Write the logline to the MYSQL database, depends on the chosen loglevel in the task
|
|
266
|
-
print('{} at line: {}'.format(message, line_number))
|
|
267
|
-
|
|
268
|
-
if self.write_logs_to_mysql:
|
|
269
|
-
# Remove quotes from message since these break the query
|
|
270
|
-
message = re.sub("[']", '', message)
|
|
271
|
-
query = "INSERT INTO `task_execution_log` (reload_id, task_id, log_level, created_at, line_number, message) VALUES ({}, {}, '{}', '{}', {}, '{}')".format(self.run_id, self.task_id, loglevel, datetime.datetime.now(), line_number, message)
|
|
272
|
-
if self.loglevel == 'DEBUG' or (self.loglevel == 'INFO' and loglevel != 'DEBUG') or (self.loglevel == 'ERROR' and loglevel in ['ERROR', 'CRITICAL']) or (self.loglevel == 'CRITICAL' and loglevel == 'CRITICAL'):
|
|
273
|
-
return self.mysql.raw_query(query, insert=True)
|
|
274
|
-
|
|
275
|
-
def update_execution_step(self, step_number: int):
|
|
276
|
-
"""
|
|
277
|
-
Update the current step number in the task_scheduler table so that user's in the frontend of BrynQ can see where a task is at any moment
|
|
278
|
-
:param step_number: Give only a number
|
|
279
|
-
:return: nothing
|
|
280
|
-
"""
|
|
281
|
-
# Update the step number in the task_scheduler table
|
|
282
|
-
return self.mysql.update('task_scheduler', ['step_nr'], [step_number], 'WHERE `id` = {}'.format(self.task_id))
|
|
283
|
-
|
|
284
|
-
def error_handling(self, e: Exception, breaking=True, send_to_teams=False):
|
|
285
|
-
"""
|
|
286
|
-
This function handles errors that occur in the scheduler. Logs the traceback, updates run statuses and notifies users
|
|
287
|
-
:param e: the Exception that is to be handled
|
|
288
|
-
:param task_id: The scheduler task id
|
|
289
|
-
:param mysql_con: The connection which is used to update the scheduler task status
|
|
290
|
-
:param logger: The logger that is used to write the logging status to
|
|
291
|
-
:param breaking: Determines if the error is breaking or code will continue
|
|
292
|
-
:param started_at: Give the time the task is started
|
|
293
|
-
:return: nothing
|
|
294
|
-
"""
|
|
295
|
-
# Get the linenumber from where the logline is executed.
|
|
296
|
-
file_name, line_number, function_name = self.__get_caller_info()
|
|
297
|
-
|
|
298
|
-
# Preparing the primary payload with error details for upload to elastic and send it
|
|
299
|
-
payload = {
|
|
300
|
-
'task_id': self.task_id,
|
|
301
|
-
'reload_id': self.run_id,
|
|
302
|
-
'started_at': datetime.datetime.now().isoformat(),
|
|
303
|
-
'partner_id': self.partner_id,
|
|
304
|
-
'customer_id': self.customer_id,
|
|
305
|
-
'customer': self.customer,
|
|
306
|
-
'file_name': file_name,
|
|
307
|
-
'function_name': function_name,
|
|
308
|
-
'line_number': line_number,
|
|
309
|
-
'task_loglevel': self.loglevel,
|
|
310
|
-
'line_loglevel': 'CRITICAL',
|
|
311
|
-
'message': str(e),
|
|
312
|
-
'traceback': traceback.format_exc()
|
|
313
|
-
}
|
|
314
|
-
self.es.post_document(index_name=self.es_index, document=payload)
|
|
315
|
-
|
|
316
|
-
self.error_count += 1
|
|
317
|
-
# Get scheduler task details for logging
|
|
318
|
-
task_details = \
|
|
319
|
-
self.mysql.select('task_scheduler, data_interfaces', 'data_interfaces.docker_image, data_interfaces.runfile_path', 'WHERE task_scheduler.data_interface_id = data_interfaces.id AND task_scheduler.id = {}'.format(self.task_id))[0]
|
|
320
|
-
taskname = task_details[0]
|
|
321
|
-
customer = task_details[1].split('/')[-1].split('.')[0]
|
|
322
|
-
now = datetime.datetime.now()
|
|
323
|
-
|
|
324
|
-
# Format error to a somewhat readable format
|
|
325
|
-
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
326
|
-
error = str(e)[:400].replace('\'', '').replace('\"', '') + ' | Line: {}'.format(exc_tb.tb_lineno)
|
|
327
|
-
|
|
328
|
-
# Log to log table in the database
|
|
329
|
-
if self.write_logs_to_mysql:
|
|
330
|
-
query = "INSERT INTO `task_execution_log` (reload_id, task_id, log_level, created_at, line_number, message) VALUES ({}, {}, 'CRITICAL', '{}', {}, '{}')".format(self.run_id, self.task_id, now, exc_tb.tb_lineno, error)
|
|
331
|
-
self.mysql.raw_query(query, insert=True)
|
|
332
|
-
if send_to_teams:
|
|
333
|
-
Functions.send_error_to_teams(database=customer, task_number=self.task_id, task_title=taskname)
|
|
334
|
-
if breaking:
|
|
335
|
-
# Set scheduler status to failed
|
|
336
|
-
self.mysql.update('task_scheduler', ['status', 'last_reload', 'last_error_message', 'step_nr'],
|
|
337
|
-
['IDLE', now, 'Failed', 0],
|
|
338
|
-
'WHERE `id` = {}'.format(self.task_id))
|
|
339
|
-
|
|
340
|
-
self.mysql.update(table='task_scheduler_log',
|
|
341
|
-
columns=['reload_status', 'finished_at'],
|
|
342
|
-
values=['Failed', f'{now}'],
|
|
343
|
-
filter=f'WHERE `reload_id` = {self.run_id}')
|
|
344
|
-
if self.email_after_errors:
|
|
345
|
-
self.email_errors(failed=True)
|
|
346
|
-
# Remove the temp values from the variables table
|
|
347
|
-
self.mysql.raw_query(f'UPDATE `task_variables` SET temp_value = null WHERE task_id = {self.task_id}', insert=True)
|
|
348
|
-
|
|
349
|
-
# Start the chained tasks if it there are tasks which should start if this one is failed
|
|
350
|
-
self.start_chained_tasks(finished_task_status='FAILED')
|
|
351
|
-
|
|
352
|
-
raise Exception(error)
|
|
353
|
-
|
|
354
|
-
def finish_task(self, reload_instant=False, log_limit: typing.Optional[int] = 10000, log_date_limit: datetime.date = None):
|
|
355
|
-
"""
|
|
356
|
-
At the end of the script, write the outcome to the database. Write if the task is finished with or without errors, Email to a contactperson if this variable is given in the
|
|
357
|
-
variables table. Also clean up the execution_log table when the number of lines is more than 1000
|
|
358
|
-
:param reload_instant: If the task should start again after it's finished
|
|
359
|
-
:param log_limit: The maximum number of logs to keep in the database. If the number of logs exceeds this limit, the oldest logs will be deleted.
|
|
360
|
-
:param log_date_limit: The date from which logs should be kept. If this is set, logs older than this date will be deleted.
|
|
361
|
-
:return:
|
|
362
|
-
"""
|
|
363
|
-
# If reload instant is true, this adds an extra field 'run_instant' to the update query, and sets the value to 1. This makes the task reload immediately after it's finished
|
|
364
|
-
field = ['run_instant', 'next_reload'] if reload_instant else []
|
|
365
|
-
value = ['1', datetime.datetime.now()] if reload_instant else []
|
|
366
|
-
if self.error_count > 0:
|
|
367
|
-
self.mysql.update('task_scheduler', ['status', 'last_reload', 'last_error_message', 'step_nr'],
|
|
368
|
-
['IDLE', datetime.datetime.now(), 'FinishedWithErrors', 0],
|
|
369
|
-
'WHERE `id` = {}'.format(self.task_id))
|
|
370
|
-
self.mysql.update(table='task_scheduler_log',
|
|
371
|
-
columns=['reload_status', 'finished_at'],
|
|
372
|
-
values=['FinishedWithErrors', f'{datetime.datetime.now()}'],
|
|
373
|
-
filter=f'WHERE `reload_id` = {self.run_id}')
|
|
374
|
-
# If the variable self.send_mail_after_errors is set to True, send an email with the number of errors to the given user
|
|
375
|
-
if self.email_after_errors:
|
|
376
|
-
self.email_errors(failed=False)
|
|
377
|
-
else:
|
|
378
|
-
self.mysql.update(table='task_scheduler',
|
|
379
|
-
columns=['status', 'last_reload', 'last_error_message', 'step_nr', 'stopped_by_user'] + field,
|
|
380
|
-
values=['IDLE', datetime.datetime.now(), 'FinishedSucces', 0, 0] + value,
|
|
381
|
-
filter='WHERE `id` = {}'.format(self.task_id))
|
|
382
|
-
|
|
383
|
-
self.mysql.update(table='task_scheduler_log',
|
|
384
|
-
columns=['reload_status', 'finished_at'],
|
|
385
|
-
values=['FinishedSuccess', f'{datetime.datetime.now()}'],
|
|
386
|
-
filter=f'WHERE `reload_id` = {self.run_id}')
|
|
387
|
-
|
|
388
|
-
# Remove the temp values from the variables table
|
|
389
|
-
self.mysql.raw_query(f'UPDATE `task_variables` SET temp_value = null WHERE task_id = {self.task_id}', insert=True)
|
|
390
|
-
|
|
391
|
-
# Start the new task if it there is a task which should start if this one is finished
|
|
392
|
-
self.start_chained_tasks(finished_task_status='SUCCESS')
|
|
393
|
-
|
|
394
|
-
if self.write_logs_to_mysql:
|
|
395
|
-
# Clean up execution log
|
|
396
|
-
# set this date filter above the actual delete filter because of the many uncooperative quotation marks involved in the whole filter
|
|
397
|
-
log_date_limit_filter = f"AND created_at >= \'{log_date_limit.strftime('%Y-%m-%d')}\'" if log_date_limit is not None else None
|
|
398
|
-
delete_filter = f"WHERE task_id = {self.task_id} " \
|
|
399
|
-
f"AND reload_id NOT IN (SELECT reload_id FROM (SELECT reload_id FROM `task_execution_log` WHERE task_id = {self.task_id} " \
|
|
400
|
-
f"AND log_level != 'CRITICAL' " \
|
|
401
|
-
f"AND log_level != 'ERROR' " \
|
|
402
|
-
f"{log_date_limit_filter if log_date_limit_filter is not None else ''} " \
|
|
403
|
-
f"ORDER BY created_at DESC {f' LIMIT {log_limit} ' if log_limit is not None else ''}) temp)"
|
|
404
|
-
|
|
405
|
-
resp = self.mysql.delete(table="task_execution_log",
|
|
406
|
-
filter=delete_filter)
|
|
407
|
-
print(resp)
|
|
408
|
-
|
|
409
|
-
def start_chained_tasks(self, finished_task_status: str):
|
|
410
|
-
filter = f'WHERE start_after_task_id = \'{self.task_id}\' AND start_after_preceding_task = \'{finished_task_status}\''
|
|
411
|
-
response = self.mysql.select(table='task_scheduler', selection='id', filter=filter)
|
|
412
|
-
if len(response) > 0:
|
|
413
|
-
tasks_to_run = [str(task[0]) for task in response]
|
|
414
|
-
self.mysql.update(table='task_scheduler', columns=['run_instant'], values=['1'], filter=f'WHERE id IN({",".join(tasks_to_run)})')
|
|
415
|
-
|
|
416
|
-
def email_errors(self, failed):
|
|
417
|
-
# The mails to email to should be stored in the task_variables table with the variable email_errors_to
|
|
418
|
-
email_variable = self.db_variable('email_errors_to')
|
|
419
|
-
if email_variable is not None:
|
|
420
|
-
email_to = email_variable.split(',')
|
|
421
|
-
if isinstance(email_to, list):
|
|
422
|
-
# The email_errors_to variable is a simple string. Convert it to a list and add a name because mandrill is asking for it
|
|
423
|
-
email_list = []
|
|
424
|
-
for i in email_to:
|
|
425
|
-
email_list.append({'name': 'BrynQ User', 'mail': i.strip()})
|
|
426
|
-
|
|
427
|
-
# Recieve the task name and the finished_at time from the task_scheduler table joined with the data_interfaces table
|
|
428
|
-
response = self.mysql.select(
|
|
429
|
-
table='task_scheduler LEFT JOIN data_interfaces ON task_scheduler.data_interface_id = data_interfaces.id ',
|
|
430
|
-
selection="title, last_reload",
|
|
431
|
-
filter=f'WHERE task_scheduler.id = {self.task_id}'
|
|
432
|
-
)
|
|
433
|
-
task = response[0][0]
|
|
434
|
-
finished_at = response[0][1]
|
|
435
|
-
|
|
436
|
-
# Set the content of the mail and all other stuff
|
|
437
|
-
if failed:
|
|
438
|
-
subject = f'Task \'{task}\' has failed'
|
|
439
|
-
content = f'Task \'{task}\' with task ID \'{self.task_id}\' failed during its last run and was stopped at {finished_at}. ' \
|
|
440
|
-
f'The task is failed. ' \
|
|
441
|
-
f'to visit the BrynQ scheduler, click here: <a href="https://app.brynq.com/interfaces/">here</a>. Here you can find the logs and find more information on why this task had failed.'
|
|
442
|
-
else:
|
|
443
|
-
subject = f'Task \'{task}\' is finished with errors'
|
|
444
|
-
content = f'Task \'{task}\' with ID \'{self.task_id}\' has runned and is finished at {finished_at}. ' \
|
|
445
|
-
f'The task is finished with {self.error_count} errors. ' \
|
|
446
|
-
f'to visit the BrynQ scheduler, click here: <a href="https://app.brynq.com/interfaces/">here</a>. Here you can find the logs and find more information on why this task had some errors.'
|
|
447
|
-
MailClient().send_mail(email_to=email_list, subject=subject, content=content, language='EN')
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
brynq_sdk
|
|
File without changes
|
|
File without changes
|
|
File without changes
|