Commits

Ronny Pfannschmidt committed 8dfdb45

turn storing results into a PUT

  • Participants
  • Parent commits c9b05d1

Comments (0)

Files changed (5)

File tests/mocksupport.py

         self._data = ""
         return x
 
-class MockConnGet:
+class MockConn:
     hostspec = '<testing>'
-    def __init__(self, url, data):
-        self._url = url
-        self._data = data
+    status = 200
 
-    def GET(self, url, body=None, headers={}):
-        assert url == self._url
-        return MockResponse(200, self._data)
+    def __init__(self, url, data, status=None):
+        self.url = url
+        self.data = data
+        if status:
+            self.status = status
 
-class MockConnPost:
-    hostspec = '<testing>'
-    def __init__(self, url, responsedata, status=201):
-        self._url = url
-        self._responsedata = responsedata
-        self._status = status
+    def action(self, url, body=None, headers={}):
+        assert url == self.url
+        self.body = body
+        return MockResponse(self.status, self.data)
 
-    def POST(self, url, body=None, headers={}):
-        assert url == self._url
-        self._body = body
-        return MockResponse(self._status, self._responsedata)
+    def __repr__(self):
+        return '<%s %s %s>' % (type(self).__name__, self.url, self.status)
+
+class MockConnGet(MockConn):
+    GET = MockConn.action
+
+class MockConnPost(MockConn):
+    POST = MockConn.action
+    status = 201
+
+class MockConnPut(MockConn):
+    PUT = MockConn.action

File tests/test_server.py

 def test_result_submit_and_get(app):
     xjobid = test_registerxjob(app)
     result = XResult(xjobid, "out", "err", 0)
-    rv = app.post("/result", data=json_encode(result))
+    rv = app.put("/%s/result" % xjobid, data=json_encode(result))
     assert rv.data == "Thanks."
     r = app.get("/%s/result" % xjobid)
     result2 = XResult.from_json(json.loads(r.data)['result'])

File tests/test_slave.py

 from xnet.xjob import XJobSpec, XResult
 from xnet.helpers import json_encode
 from xnet import slave
-from mocksupport import MockConnPost, MockConnGet
+from mocksupport import MockConnPost, MockConnGet, MockConnPut
         
 def test_pullxjob_nojob():
     conn = MockConnPost("/pull", json_encode({'xjobid':'no'}))
 
 def test_xjobfinished():
     xresult = XResult(1, out="1", err="2", retcode=0)
-    conn = MockConnPost("/result", "ok")
+    conn = MockConnPut("/1/result", "ok")
     r = slave.xjobfinished(conn, xresult)
-    assert conn._body == json_encode(xresult)
+    assert conn.body == json_encode(xresult)
     assert r == "ok"
 
 

File xnet/server.py

 app.streamh = StreamHandler()
 app.debug = True
 app.debug_log_format = "[server:%(levelname)s] %(message)s"
-   
+
+
+@app.route("/", methods=["GET"])
+def listxjobs():
+    xjobs = {
+        'queued': app.xjobstorage.queued,
+        'pulled': app.xjobstorage.pulled,
+        'active': app.xjobstorage.active,
+        'finished': app.xjobstorage.finished,
+    }
+    return json_response(app, **xjobs)
+
+
 @app.route("/", methods=["POST"])
 def submitxjob():
     xjobdata = request.data
         response = "job with id %s already registered" % jobinfo.xjobid
         return text_response(app, response)
 
-@app.route("/", methods=["GET"])
-def listxjobs():
-    xjobs = {
-        'queued': app.xjobstorage.queued,
-        'pulled': app.xjobstorage.pulled,
-        'active': app.xjobstorage.active,
-        'finished': app.xjobstorage.finished,
-    }
-    return json_response(app, **xjobs)
 
 @app.route("/pull", methods=["POST"])
 def pullxjob():
         return json_response(app, xjobid="no")
     return json_response(app, xjobid=xjobid, spec=xjob)
 
-@app.route("/result", methods=['POST'])
-def xjobfinished(): 
+
+@app.route("/<int:xjobid>/result", methods=['GET'])
+def get_result(xjobid):
+    result = app.resultstorage.get(xjobid=xjobid)
+    app.logger.info("returning result %s" % result)
+    xjobspec = app.xjobstorage.get(xjobid=xjobid)
+    return json_encode({'result': result, 'xjobspec': xjobspec})
+
+
+@app.route("/<int:xjobid>/result", methods=['PUT'])
+def put_result(xjobid):
     resultdata = json.loads(request.data)
     result = XResult.from_json(resultdata)
     xresultid = app.resultstorage.add(result)
     return text_response(app, "Thanks.")
 
 @app.route("/<int:xjobid>/stdout", methods=['GET'])
-def getstdout(xjobid):
+def get_stdout(xjobid):
     stdout = app.streamh.get_stdout(xjobid)
     if stdout is None:
         return text_response(app, "Job with id %s is not active" % xjobid)
                     response[key] = value.rstrip('\n')
         return json_response(app, xjobid=xjobid, stdout=response)
 
-@app.route("/<int:xjobid>/result", methods=['GET'])
-def getresult(xjobid):
-    result = app.resultstorage.get(xjobid=xjobid)
-    app.logger.info("returning result %s" % result)
-    xjobspec = app.xjobstorage.get(xjobid=xjobid)
-    return json_encode({'result': result, 'xjobspec': xjobspec})
-
 @app.route("/<int:xjobid>/stdout", methods=['PUT'])
-def putpartial():
+def put_partial_stdout():
     s_token = request.headers['content-range']
     token = int(s_token)
     if not app.streamh.isactive(int(xjobid)):

File xnet/slave.py

     log("pullxjob: initiating request")
     try:
         response = conn.POST("/pull")
-    except:
-        log("[WARN] server %s does not seem to be online" % conn.hostspec)
+    except Exceptionm e:
+        log("[WARN] server %s does not seem to be online - %s" % (conn.hostspec, e))
         return None
     content = response.read()
     data = json.loads(content)
 def xjobfinished(conn, result):
     log("submitting result %s", result)
     data = json_encode(result)
-    response = conn.POST("/result", data)
+    response = conn.PUT("/%s/result" % result.xjobid, data)
     ack = response.read()
     log("server accepted, says: %s", ack)
     return ack