/cellar/ion/services/coi/host_status.py

https://github.com/tgiguere/ioncore-python
Python | 90 lines | 51 code | 25 blank | 14 comment | 3 complexity | 0aa0da211faf7c038ea03cabb0c74318 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. @file ion/services/coi/host_status.py
  4. @author Brian Fox
  5. @brief service for messaging local host status at intervals
  6. """
  7. import ion.util.ionlog
  8. log = ion.util.ionlog.getLogger(__name__)
  9. try:
  10. import json
  11. except:
  12. import simplejson as json
  13. from twisted.internet import defer, task
  14. from twisted.web import xmlrpc
  15. from ion.core.process.process import ProcessFactory
  16. from ion.core.process.service_process import ServiceProcess, ServiceClient
  17. class HostStatusService(ServiceProcess):
  18. """
  19. Host status interface
  20. """
  21. # Declaration of service
  22. declare = ServiceProcess.service_declare(
  23. name='host_status',
  24. version='0.1.0',
  25. dependencies=[]
  26. )
  27. def slc_init(self):
  28. self.INTERVAL = 1 # seconds
  29. self.COUNT = 1
  30. self.count = self.COUNT
  31. self.client = xmlrpc.Proxy('http://localhost:9010')
  32. self.lc = task.LoopingCall(self.report)
  33. self.lc.start(self.INTERVAL)
  34. @defer.inlineCallbacks
  35. def report(self):
  36. self.count -= 1
  37. if self.count < 0:
  38. log.debug('Shutting down host status looping call')
  39. self.lc.stop()
  40. return
  41. log.debug('Starting report query')
  42. status = yield self.client.callRemote("getStatusString","all")
  43. log.debug('Received report')
  44. log.debug(status)
  45. def isRunning(self):
  46. return self.lc.running
  47. def op_config(self, content, headers, msg):
  48. pass
  49. @defer.inlineCallbacks
  50. def op_sendstatus(self, content, headers, msg):
  51. yield self.reply_ok(msg)
  52. class HostStatusClient(ServiceClient):
  53. """
  54. Class for client to sent log message to service
  55. """
  56. def __init__(self, proc=None, **kwargs):
  57. if not 'targetname' in kwargs:
  58. kwargs['targetname'] = "host_status"
  59. ServiceClient.__init__(self, proc, **kwargs)
  60. @defer.inlineCallbacks
  61. def logmsg(self, level, msg, sender, logtime):
  62. yield self._check_init()
  63. defer.returnValue(0)
  64. # Spawn of the process using the module name
  65. factory = ProcessFactory(HostStatusService)