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