diff options
Diffstat (limited to 'gitautodeploy')
-rw-r--r-- | gitautodeploy/events.py | 35 | ||||
-rw-r--r-- | gitautodeploy/gitautodeploy.py | 26 | ||||
-rw-r--r-- | gitautodeploy/httpserver.py | 11 |
3 files changed, 37 insertions, 35 deletions
diff --git a/gitautodeploy/events.py b/gitautodeploy/events.py index 8bbaaa1..5e177a0 100644 --- a/gitautodeploy/events.py +++ b/gitautodeploy/events.py @@ -33,10 +33,7 @@ class SystemEvent(object): def register_message(self, message, level="INFO"): self.messages.append(message) - self.hub.update_action(self, message) - - def notify(self): - self.hub.notify(self) + self.hub.notify_observers(type="event-updated", event=self.dict_repr()) def set_id(self, id): self.id = id @@ -46,9 +43,12 @@ class SystemEvent(object): def set_waiting(self, value): self.waiting = value + self.hub.notify_observers(type="event-updated", event=self.dict_repr()) def set_success(self, value): self.success = value + self.hub.notify_observers(type="event-updated", event=self.dict_repr()) + self.hub.notify_observers(type="event-success", id=self.id, success=value) def log_debug(self, message): self.logger.debug(message) @@ -70,8 +70,8 @@ class SystemEvent(object): self.logger.critical(message) self.register_message(message, "CRITICAL") - def update(self): - self.hub.update_action(self) + #def update(self): + # self.hub.notify_observers(type="event-updated", event=self.dict_repr()) class WebhookAction(SystemEvent): @@ -119,6 +119,19 @@ class StartupEvent(SystemEvent): data['ws-started'] = self.ws_started return data + def set_http_started(self, value): + self.http_started = value + self.hub.notify_observers(type="event-updated", event=self.dict_repr()) + self.validate_success() + + def set_ws_started(self, value): + self.ws_started = value + self.hub.notify_observers(type="event-updated", event=self.dict_repr()) + self.validate_success() + + def validate_success(self): + if self.http_started and self.ws_started: + self.set_success(True) class EventStore(object): @@ -134,7 +147,7 @@ class EventStore(object): if observer in self.observers: self.observers.remove(observer) - def update_observers(self, *args, **kwargs): + def notify_observers(self, *args, **kwargs): for observer in self.observers: observer.update(*args, **kwargs) @@ -143,18 +156,12 @@ class EventStore(object): event.register_hub(self) self.next_id = self.next_id + 1 self.actions.append(event) - self.update_observers(event=event) + self.notify_observers(type="new-event", event=event.dict_repr()) # Store max 100 actions if len(self.actions) > 100: self.actions.pop(0) - def notify(self, event): - self.update_observers(event=event) - - def update_action(self, event, message=None): - self.update_observers(event=event, message=message) - def dict_repr(self): action_repr = [] for action in self.actions: diff --git a/gitautodeploy/gitautodeploy.py b/gitautodeploy/gitautodeploy.py index 784f662..5484019 100644 --- a/gitautodeploy/gitautodeploy.py +++ b/gitautodeploy/gitautodeploy.py @@ -158,11 +158,11 @@ class GitAutoDeploy(object): def update(self, *args, **kwargs): import json - message = { - 'type': 'unknown' - } + #message = { + # 'type': 'unknown' + #} - if 'event' in kwargs: + #if 'event' in kwargs: #if 'message' in kwargs: # message = { # 'type': 'event-message', @@ -170,13 +170,13 @@ class GitAutoDeploy(object): # 'message': kwargs['message'] # } #else: - message = { - 'type': 'event-update', - 'event-id': kwargs['event'].id, - 'event': kwargs['event'].dict_repr() - } + #message = { + # 'type': 'event-update', + # 'event-id': kwargs['event'].id, + # 'event': kwargs['event'].dict_repr() + #} - data = json.dumps(message).encode('utf-8') + data = json.dumps(kwargs).encode('utf-8') for client in self._ws_clients: client.sendMessage(data) @@ -300,8 +300,7 @@ class GitAutoDeploy(object): self._startup_event.log_info("Listening for http connections on %s port %s" % (sa[0], sa[1])) self._startup_event.http_address = sa[0] self._startup_event.http_port = sa[1] - self._startup_event.http_started = True - self._startup_event.notify() + self._startup_event.set_http_started(True) except socket.error as e: self._startup_event.log_critical("Error on socket: %s" % e) @@ -352,8 +351,7 @@ class GitAutoDeploy(object): self._startup_event.log_info("Listening for web socket connections on %s port %s" % (self._config['web-ui']['ws-host'], self._config['web-ui']['ws-port'])) self._startup_event.ws_address = self._config['web-ui']['ws-host'] self._startup_event.ws_port = self._config['web-ui']['ws-port'] - self._startup_event.ws_started = True - self._startup_event.notify() + self._startup_event.set_ws_started(True) # Serve forever (until reactor.stop()) reactor.run(installSignalHandlers=False) diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py index 1ed03a8..b5cd801 100644 --- a/gitautodeploy/httpserver.py +++ b/gitautodeploy/httpserver.py @@ -146,9 +146,8 @@ class WebbhookRequestProcessor(object): result.append(repo_result) action.log_info("Deploy commands were executed") - action.set_success(True) action.set_waiting(False) - action.update() + action.set_success(True) return result @@ -321,8 +320,8 @@ def WebhookRequestHandlerFactory(config, event_store): request_headers = dict((k.lower(), v) for k, v in request_headers.items()) action = WebhookAction(self.client_address, request_headers, request_body) - action.set_waiting(True) event_store.register_action(action) + action.set_waiting(True) action.log_info('Incoming request from %s:%s' % (self.client_address[0], self.client_address[1])) @@ -400,9 +399,8 @@ def WebhookRequestHandlerFactory(config, event_store): self.send_error(400, 'Unprocessable request') action.log_warning('Unable to process incoming request from %s:%s' % (self.client_address[0], self.client_address[1])) test_case['expected']['status'] = 400 - action.set_success(False) action.set_waiting(False) - action.update() + action.set_success(False) return except Exception as e: @@ -412,9 +410,8 @@ def WebhookRequestHandlerFactory(config, event_store): test_case['expected']['status'] = 500 action.log_warning("Unable to process request") - action.set_success(False) action.set_waiting(False) - action.update() + action.set_success(False) raise e |