[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Pulp-list] Simple ping service - solution



Hello

here comes my solution. It pings the database, increments the counter, measures the lag and also shows the version number.

Example output:

# curl -k -u admin:admin https://xxx/pulp/api/services/ping/
{
  "db_version": 2,
  "ping_duration_ms": "1.68",
  "ping_count": 35
}

The code:

class PingAction(JSONController):

    @JSONController.error_handler
    def GET(self):
        """
        Dummy call that just prints time.
        @return: db_version - current DB version number
        """
        _props_db = get_object_db('properties', ['key'])

        # insert the first entry if there is no such document
        if _props_db.find_one({"key": "ping_service_count"}) == None:
            _props_db.insert({"key": "ping_service_count", "value": 0})

        # increment the counter and return
        _start_time = time.time()
        _props_db.update({"key": "ping_service_count"},
            {"$inc": {"value": 1}})
        count = _props_db.find_one(
                  {"key": "ping_service_count"})['value']

        # return the response
        return self.ok({
          "db_version": VERSION,
          "ping_count": count,
          "ping_duration_ms": str(round((time.time() -
                  _start_time) * 1000, 2)),
          })

Attaching the patch.

Comments?

--
Later,
  Lukas "lzap" Zapletal
>From d3673af35d67576e63dd025a497191b41bb8be82 Mon Sep 17 00:00:00 2001
From: Lukas Zapletal <lzap+git redhat com>
Date: Thu, 10 Feb 2011 16:40:49 +0100
Subject: [PATCH] New dummy webservice - ping

---
 .../server/webservices/controllers/services.py     |   31 +++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/src/pulp/server/webservices/controllers/services.py b/src/pulp/server/webservices/controllers/services.py
index af618cd..43db15f 100644
--- a/src/pulp/server/webservices/controllers/services.py
+++ b/src/pulp/server/webservices/controllers/services.py
@@ -26,6 +26,8 @@ from pulp.server.api.upload import ImportUploadContent
 from pulp.server.auth.authorization import READ, EXECUTE
 from pulp.server.webservices import mongo
 from pulp.server.webservices.controllers.base import JSONController
+from pulp.server.db.connection import get_object_db
+from pulp.server.db.version import VERSION
 
 # globals ---------------------------------------------------------------------
 
@@ -188,6 +190,33 @@ class FileSearch(JSONController):
     def PUT(self):
         log.debug('deprecated Users.PUT method called')
         return self.POST()
+
+class PingAction(JSONController):
+
+    @JSONController.error_handler
+    def GET(self):
+        """
+        Dummy call that just prints time.
+        @return: db_version - current DB version number
+        """
+        _props_db = get_object_db('properties', ['key'])
+
+        # insert the first entry if there is no such document
+        if _props_db.find_one({"key": "ping_service_count"}) == None:
+            _props_db.insert({"key": "ping_service_count", "value": 0})
+
+        # increment the counter and return
+        _start_time = time.time()
+        _props_db.update({"key": "ping_service_count"}, {"$inc": {"value": 1}})
+        count = _props_db.find_one({"key": "ping_service_count"})['value']
+
+        # return the response
+        return self.ok({
+          "db_version": VERSION,
+          "ping_count": count,
+          "ping_duration_ms": str(round((time.time() - _start_time) * 1000, 2)),
+          })
+
 # web.py application ----------------------------------------------------------
 
 URLS = (
@@ -197,7 +226,7 @@ URLS = (
     '/upload/$', 'StartUpload',
     '/upload/append/([^/]+)/$', 'AppendUpload',
     '/upload/import/$',  'ImportUpload',
-    
+    '/ping/$', 'PingAction',
 )
 
 application = web.application(URLS, globals())
-- 
1.7.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]