Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import sys
- #sys.path.append(os.path.join(sys.path[0], '..'))
- from twisted.internet import reactor, protocol, defer, task
- from twisted.application import service
- sys.path.append("/usr/sbin")
- import twistedbeanstalk
- import logging
- logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', filename='/var/log/ocapi.log', level=logging.DEBUG)
- class OCService(service.Service):
- def startService(self):
- service.Service.startService(self)
- def executor(bs, jobid, jobdata):
- logging.debug("Running job #%d: %s" % (jobid, jobdata))
- # TODO: handle job...
- bs.touch(jobid).addCallback(lambda x: bs.delete(jobid))
- def errorHandler(e):
- logging.debug("Got an error", e)
- def executionGenerator(bs):
- while True:
- logging.debug("Waiting fo
- def worker(bs):
- bs.watch("ocqueue")
- bs.ignore("default")
- coop = task.Cooperator()
- coop.coiterate(executionGenerator(bs))
- top_service = service.MultiService()
- oc_service = OCService()
- oc_service.setServiceParent(top_service)
- # this variable has to be named 'application'
- application = service.Application("ocapi")
- # this hooks the collection we made to the application
- top_service.setServiceParent(application)
- # at this point, the application is ready to go. when started by
- # twistd it will start the child services, thus starting up the
- # ocapi service
- d = protocol.ClientCreator(reactor, twistedbeanstalk.Beanstalk).connectTCP(
- "localhost", 11300)
- d.addCallback(worker)
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment