2010-05-31 20:57:39 +00:00
|
|
|
from . import base
|
|
|
|
import logging
|
|
|
|
from xml.etree import cElementTree as ET
|
2010-08-19 23:09:00 +00:00
|
|
|
import types
|
2010-05-31 20:57:39 +00:00
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-05-31 20:57:39 +00:00
|
|
|
class jobs(base.base_plugin):
|
|
|
|
def plugin_init(self):
|
|
|
|
self.xep = 'pubsubjob'
|
|
|
|
self.description = "Job distribution over Pubsub"
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-05-31 20:57:39 +00:00
|
|
|
def post_init(self):
|
|
|
|
pass
|
|
|
|
#TODO add event
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-05-31 20:57:39 +00:00
|
|
|
def createJobNode(self, host, jid, node, config=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def createJob(self, host, node, jobid=None, payload=None):
|
|
|
|
return self.xmpp.plugin['xep_0060'].setItem(host, node, ((jobid, payload),))
|
|
|
|
|
|
|
|
def claimJob(self, host, node, jobid, ifrom=None):
|
|
|
|
return self._setState(host, node, jobid, ET.Element('{http://andyet.net/protocol/pubsubjob}claimed'))
|
|
|
|
|
2010-08-19 23:09:00 +00:00
|
|
|
def unclaimJob(self, host, node, jobid):
|
2010-05-31 20:57:39 +00:00
|
|
|
return self._setState(host, node, jobid, ET.Element('{http://andyet.net/protocol/pubsubjob}unclaimed'))
|
|
|
|
|
|
|
|
def finishJob(self, host, node, jobid, payload=None):
|
|
|
|
finished = ET.Element('{http://andyet.net/protocol/pubsubjob}finished')
|
|
|
|
if payload is not None:
|
|
|
|
finished.append(payload)
|
|
|
|
return self._setState(host, node, jobid, finished)
|
|
|
|
|
|
|
|
def _setState(self, host, node, jobid, state, ifrom=None):
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['to'] = host
|
|
|
|
if ifrom: iq['from'] = ifrom
|
|
|
|
iq['type'] = 'set'
|
|
|
|
iq['psstate']['node'] = node
|
|
|
|
iq['psstate']['item'] = jobid
|
|
|
|
iq['psstate']['payload'] = state
|
|
|
|
result = iq.send()
|
2010-08-19 23:09:00 +00:00
|
|
|
if result is None or type(result) == types.BooleanType or result['type'] != 'result':
|
2010-11-06 05:28:59 +00:00
|
|
|
log.error("Unable to change %s:%s to %s" % (node, jobid, state))
|
2010-05-31 20:57:39 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|