Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/contrib/sagecell-client/sagecell-service.py
447 views
1
#! /usr/bin/env python3
2
3
from datetime import datetime
4
import random
5
import requests
6
import sys
7
import time
8
9
10
retries = 3
11
12
def message(s):
13
print('{}: {} attempts left. {}'.format(datetime.now(), retries, s))
14
15
while retries:
16
retries -= 1
17
a, b = random.randint(-2**31, 2**31), random.randint(-2**31, 2**31)
18
# The handling of temporary files in Sage 9.7 does not allow SageMathCell to
19
# function properly if there are no regular requests producing temporary
20
# files. To fight it, we'll generate one during health checks. See
21
# https://groups.google.com/g/sage-devel/c/jpwUb8OCVVc/m/R4r5bnOkBQAJ
22
code = 'show(plot(sin)); print({} + {})'.format(a, b)
23
try:
24
r = requests.post(sys.argv[1] + '/service',
25
data={"code": code, "accepted_tos": "true"},
26
timeout=5)
27
reply = r.json()
28
# Every few hours we have a request that comes back as executed, but the
29
# stdout is not in the dictionary. It seems that the compute message
30
# never actually gets sent to the kernel and it appears the problem is
31
# in the zmq connection between the webserver and the kernel.
32
#
33
# Also sometimes reply is unsuccessful, yet the server keeps running
34
# and other requests are serviced. Since a restart breaks all active
35
# interacts, better not to restart the server that "mostly works" and
36
# instead we'll just accumulate statistics on these random errors to
37
# help resolve them.
38
if (reply['success']
39
and 'stdout' in reply
40
and int(reply['stdout'].strip()) == a + b):
41
exit(0)
42
message(reply)
43
except Exception as e:
44
message(e)
45
time.sleep(0.5)
46
message('The server is not working!')
47
exit(1)
48
49