schenette

OC Service

Oct 6th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5.  
  6. #sys.path.append(os.path.join(sys.path[0], '..'))
  7.  
  8. from twisted.internet import reactor, protocol, defer, task
  9. from twisted.application import service
  10.  
  11. sys.path.append("/usr/sbin")
  12. import twistedbeanstalk
  13.  
  14. import logging
  15.  
  16. logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', filename='/var/log/ocapi.log', level=logging.DEBUG)
  17.  
  18. class OCService(service.Service):
  19.  
  20. def startService(self):
  21. service.Service.startService(self)
  22.  
  23. def executor(bs, jobid, jobdata):
  24. logging.debug("Running job #%d: %s" % (jobid, jobdata))
  25. # TODO: handle job...
  26. bs.touch(jobid).addCallback(lambda x: bs.delete(jobid))
  27.  
  28. def errorHandler(e):
  29. logging.debug("Got an error", e)
  30.  
  31. def executionGenerator(bs):
  32. while True:
  33. logging.debug("Waiting fo
  34. def worker(bs):
  35. bs.watch("ocqueue")
  36. bs.ignore("default")
  37.  
  38. coop = task.Cooperator()
  39. coop.coiterate(executionGenerator(bs))
  40.  
  41. top_service = service.MultiService()
  42.  
  43. oc_service = OCService()
  44. oc_service.setServiceParent(top_service)
  45.  
  46. # this variable has to be named 'application'
  47. application = service.Application("ocapi")
  48.  
  49. # this hooks the collection we made to the application
  50. top_service.setServiceParent(application)
  51.  
  52. # at this point, the application is ready to go. when started by
  53. # twistd it will start the child services, thus starting up the
  54. # ocapi service
  55.  
  56. d = protocol.ClientCreator(reactor, twistedbeanstalk.Beanstalk).connectTCP(
  57. "localhost", 11300)
  58. d.addCallback(worker)
  59.  
  60. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment