Path: blob/master/contrib/sagecell-client/sagecell-service.py
447 views
#! /usr/bin/env python312from datetime import datetime3import random4import requests5import sys6import time789retries = 31011def message(s):12print('{}: {} attempts left. {}'.format(datetime.now(), retries, s))1314while retries:15retries -= 116a, b = random.randint(-2**31, 2**31), random.randint(-2**31, 2**31)17# The handling of temporary files in Sage 9.7 does not allow SageMathCell to18# function properly if there are no regular requests producing temporary19# files. To fight it, we'll generate one during health checks. See20# https://groups.google.com/g/sage-devel/c/jpwUb8OCVVc/m/R4r5bnOkBQAJ21code = 'show(plot(sin)); print({} + {})'.format(a, b)22try:23r = requests.post(sys.argv[1] + '/service',24data={"code": code, "accepted_tos": "true"},25timeout=5)26reply = r.json()27# Every few hours we have a request that comes back as executed, but the28# stdout is not in the dictionary. It seems that the compute message29# never actually gets sent to the kernel and it appears the problem is30# in the zmq connection between the webserver and the kernel.31#32# Also sometimes reply is unsuccessful, yet the server keeps running33# and other requests are serviced. Since a restart breaks all active34# interacts, better not to restart the server that "mostly works" and35# instead we'll just accumulate statistics on these random errors to36# help resolve them.37if (reply['success']38and 'stdout' in reply39and int(reply['stdout'].strip()) == a + b):40exit(0)41message(reply)42except Exception as e:43message(e)44time.sleep(0.5)45message('The server is not working!')46exit(1)474849