diff options
Diffstat (limited to 'distbuild/initiator_connection.py')
-rw-r--r-- | distbuild/initiator_connection.py | 77 |
1 files changed, 59 insertions, 18 deletions
diff --git a/distbuild/initiator_connection.py b/distbuild/initiator_connection.py index 54322f6a..c566e548 100644 --- a/distbuild/initiator_connection.py +++ b/distbuild/initiator_connection.py @@ -97,29 +97,34 @@ class InitiatorConnection(distbuild.StateMachine): logging.debug('InitiatorConnection: from %s: %r', self.initiator_name, event.msg) + msg_handler = { + 'build-request': self._handle_build_request, + 'list-requests': self._handle_list_requests, + 'build-cancel': self._handle_build_cancel, + 'build-status': self._handle_build_status, + } try: - if event.msg['type'] == 'build-request': - self._handle_build_request(event) - elif event.msg['type'] == 'list-requests': - self._handle_list_requests(event) - else: - logging.error('Invalid message type: %s', event.msg) + if event.msg.get('protocol_version') != distbuild.protocol.VERSION: + msg = distbuild.message('build-failed', + # use build-failed as it is understood by older versions of + # morph; if morph is old enough (protocol_version < 1) it + # won't understand a new message and ignore it or cause the + # request to hang + id=event.msg['id'], + reason=('Protocol version mismatch between server & ' + 'initiator: distbuild network uses distbuild ' + 'protocol version %i, but client uses version %i.' + % (distbuild.protocol.VERSION, + event.msg.get('protocol_version')))) + self.jm.send(msg) + self._log_send(msg) + return + msg_handler[event.msg['type']](event) except (KeyError, ValueError) as ex: logging.error('Invalid message from initiator: %s: exception %s', event.msg, ex) def _handle_build_request(self, event): - if event.msg.get('protocol_version') != distbuild.protocol.VERSION: - msg = distbuild.message('build-failed', - id=event.msg['id'], - reason=('Protocol version mismatch between server & initiator:' - ' distbuild network uses distbuild protocol version %i' - ', but client uses version %i.' % ( - distbuild.protocol.VERSION, - event.msg.get('protocol_version')))) - self.jm.send(msg) - self._log_send(msg) - return new_id = self._idgen.next() self.our_ids.add(new_id) self._route_map.add(event.msg['id'], new_id) @@ -128,6 +133,7 @@ class InitiatorConnection(distbuild.StateMachine): self, event.msg, self.artifact_cache_server, self.morph_instance) self.mainloop.add_state_machine(build_controller) + self.mainloop.store_state_machine(build_controller) def _handle_list_requests(self, event): requests = self.mainloop.state_machines_of_type( @@ -143,10 +149,45 @@ class InitiatorConnection(distbuild.StateMachine): build.get_request()['repo'], build.get_request()['ref'], build.get_request()['morphology'])) - msg = distbuild.message('list-request-output', + msg = distbuild.message('request-output', message=('\n\n'.join(output_msg))) self.jm.send(msg) + def _handle_build_cancel(self, event): + requests = self.mainloop.state_machines_of_type( + distbuild.BuildController) + for build in requests: + if build.get_request()['id'] == event.msg['id']: + self.mainloop.queue_event(InitiatorConnection, + InitiatorDisconnect(event.msg['id'])) + self.mainloop.queue_event(distbuild.BuildController, + build.build_cancel(event.msg['id'])) + msg = distbuild.message('request-output', message=( + 'Cancelling build request with ID %s' % + event.msg['id'])) + self.jm.send(msg) + break + else: + msg = distbuild.message('request-output', message=('Given ' + 'build-request ID does not match any ' + 'running build IDs.')) + self.jm.send(msg) + + def _handle_build_status(self, event): + requests = self.mainloop.state_stored_requests( + distbuild.BuildController) + for build in requests: + if build.get_request()['id'] == event.msg['id']: + msg = distbuild.message('request-output', message=( + build.get_status_message())) + self.jm.send(msg) + break + else: + msg = distbuild.message('request-output', message=('Given ' + 'build-request ID does not match any ' + 'recent build IDs.')) + self.jm.send(msg) + def _disconnect(self, event_source, event): for id in self.our_ids: logging.debug('InitiatorConnection: %s: InitiatorDisconnect(%s)', |