def get_client(self, coord, auto_retry=None): """Obtain a new RemoteServiceClient to connect to a server. Instantiate a RemoteServiceClient, spawn its greenlet and add it to self.clients. It will try to connect to the service at the given coordinates. coord (ServiceCoord): the (name, shard) of the service auto_retry (float|None): how long to wait after a disconnection before trying to reconnect return (RemoteServiceClient): a client """ client = RemoteServiceClient(coord, auto_retry) client.connect() self.clients.append(client) return client
def maybe_send_notification(submission_id): """Non-blocking attempt to notify a running ES of the submission""" rs = RemoteServiceClient(ServiceCoord("EvaluationService", 0), auto_retry=10) rs.connect() result = rs.new_submission(submission_id=submission_id) rs.disconnect() print("ID:{}".format(submission_id))
def kill_service(self, service): """Restart the service. Note that after calling successfully this method, get_resource could still report the service running untile we call _store_resources again. service (string): format: name,shard. """ logger.info("Killing %s as asked." % service) try: idx = service.rindex(",") except ValueError: logger.error("Unable to decode service string.") name = service[:idx] try: shard = int(service[idx + 1:]) except ValueError: logger.error("Unable to decode service shard.") remote_service = RemoteServiceClient(ServiceCoord(name, shard)) remote_service.quit(reason="Asked by ResourceService")
def get_client(self, coord, block=True, auto_retry=None): """Obtain a new RemoteServiceClient to connect to a server. Instantiate a RemoteServiceClient, spawn its greenlet and add it to self.clients. It will try to connect to the service at the given coordinates. coord (ServiceCoord): the (name, shard) of the service block (bool): whether to wait for the connection to be established before returning auto_retry (float|None): how long to wait after a disconnection before trying to reconnect return (RemoteServiceClient): a client """ client = RemoteServiceClient(coord, auto_retry) client.connect() if block: client._connection_event.wait() self.clients.append(client) return client
def add_submissions(contest_name, task_name, username, items): """ Add submissions from the given user to the given task in the given contest. Each item corresponds to a submission, and should contain a dictionary which maps formatted file names to paths. For example, in batch tasks the format is "Task.%l", so one submission would be {"Task.%l": "path/to/task.cpp"}. """ # We connect to evaluation service to try and notify it about # the new submissions. Otherwise, it will pick it up only on # the next sweep for missed operations. rs = RemoteServiceClient(ServiceCoord("EvaluationService", 0)) rs.connect() with SessionGen() as session: user = get_user(session, username) contest = get_contest(session, contest_name) participation = get_participation(session, contest, user) task = get_task(session, task_name, contest) elements = set(format_element.filename for format_element in task.submission_format) file_cacher = FileCacher() # We go over all submissions twice. First we validate the # submission format. for submission_dict in items: for (format_file_name, path) in submission_dict.iteritems(): if format_file_name not in elements: raise Exception("Unexpected submission file: %s. " "Expected elements: %s" % (format_file_name, elements)) if not os.path.isfile(path): raise Exception("File not found: %s" % path) # Now add to database. for submission_dict in items: if not submission_dict: continue timestamp = time.time() file_digests = {} language_name = None for (format_file_name, path) in submission_dict.iteritems(): digest = file_cacher.put_file_from_path( path, "Submission file %s sent by %s at %d." % (path, username, timestamp)) file_digests[format_file_name] = digest current_language = filename_to_language(path) if current_language is not None: language_name = current_language.name submission = Submission(make_datetime(timestamp), language_name, participation=participation, task=task) for filename, digest in file_digests.items(): session.add(File(filename, digest, submission=submission)) session.add(submission) session.commit() rs.new_submission(submission_id=submission.id) rs.disconnect()
def maybe_send_notification(submission_id): """Non-blocking attempt to notify a running ES of the submission""" rs = RemoteServiceClient(ServiceCoord("EvaluationService", 0)) rs.connect() rs.new_submission(submission_id=submission_id) rs.disconnect()