| 36 | |
| 37 | |
| 38 | class ExecutionsCallbackFeature: |
| 39 | def __init__(self, |
| 40 | execution_service: ExecutionService, |
| 41 | config, |
| 42 | process_invoker: ProcessInvoker): |
| 43 | self._execution_service = execution_service |
| 44 | |
| 45 | if config is None: |
| 46 | self.notify_on_start = False |
| 47 | self.notify_on_finish = False |
| 48 | return |
| 49 | |
| 50 | self.notify_on_start = read_bool_from_config('notify_on_start', config, default=True) |
| 51 | self.notify_on_finish = read_bool_from_config('notify_on_finish', config, default=True) |
| 52 | |
| 53 | destinations_config = read_list(config, 'destinations', []) |
| 54 | if not destinations_config: |
| 55 | LOGGER.warning('Execution callback destinations are missing! Please specify any') |
| 56 | self.notify_on_start = False |
| 57 | self.notify_on_finish = False |
| 58 | return |
| 59 | |
| 60 | destinations = _init_destinations(destinations_config, process_invoker) |
| 61 | self._communication_service = CommunicationsService(destinations) |
| 62 | |
| 63 | self.notification_fields = read_list(config, 'notification_fields', default=_DEFAULT_NOTIFICATION_FIELDS) |
| 64 | |
| 65 | def _subscribe_execution_listener(self): |
| 66 | execution_service = self._execution_service |
| 67 | |
| 68 | if self.notify_on_start: |
| 69 | def started(execution_id, user): |
| 70 | notification_object = self.prepare_notification_object(execution_id, 'execution_started', user) |
| 71 | if _EXIT_CODE_FIELD in notification_object: |
| 72 | del notification_object[_EXIT_CODE_FIELD] |
| 73 | title = 'Execution ' + str(execution_id) + ' started' |
| 74 | |
| 75 | self._communication_service.send(title, notification_object) |
| 76 | |
| 77 | execution_service.add_start_listener(started) |
| 78 | |
| 79 | if self.notify_on_finish: |
| 80 | def finished(execution_id, user): |
| 81 | notification_object = self.prepare_notification_object(execution_id, 'execution_finished', user) |
| 82 | |
| 83 | title = 'Execution ' + str(execution_id) + ' finished' |
| 84 | self._communication_service.send(title, notification_object) |
| 85 | |
| 86 | execution_service.add_finish_listener(finished) |
| 87 | |
| 88 | def start(self): |
| 89 | self._subscribe_execution_listener() |
| 90 | |
| 91 | def prepare_notification_object(self, execution_id, event_type, user): |
| 92 | execution_service = self._execution_service |
| 93 | pid = execution_service.get_process_id(execution_id) |
| 94 | script_name = execution_service.get_config(execution_id, user).name |
| 95 |
no outgoing calls