Example #1
0
class Context(object):
    def __init__(self):
        self.service = TaskProxyService(self)
        self.datastore = None
        self.conn = None
        self.instance = None
        self.running = Event()

    def put_status(self, state, result=None, exception=None):
        obj = {'status': state, 'result': None}

        if result:
            obj['result'] = result

        if exception:
            obj['error'] = serialize_error(exception)

        self.conn.call_sync('task.put_status', obj)

    def main(self):
        if len(sys.argv) != 2:
            print("Invalid number of arguments", file=sys.stderr)
            sys.exit(errno.EINVAL)

        key = sys.argv[1]
        logging.basicConfig(file=sys.stdout, level=logging.DEBUG)
        try:
            self.datastore = get_default_datastore()
            self.conn = Client()
            self.conn.connect('127.0.0.1')
            self.conn.login_service('task.{0}'.format(os.getpid()))
            self.conn.enable_server()
            self.conn.rpc.register_service_instance('taskproxy', self.service)
            task = self.conn.call_sync('task.checkin', key)

            module = imp.load_source('plugin', task['filename'])
            setproctitle.setproctitle('task executor (tid {0})'.format(
                task['id']))

            try:
                self.instance = getattr(module, task['class'])(
                    DispatcherWrapper(self.conn), self.datastore)
                self.instance.configstore = ConfigStore(self.datastore)
                self.running.set()
                result = self.instance.run(*task['args'])
            except BaseException, err:
                print("Task exception: {0}".format(str(err)), file=sys.stderr)
                traceback.print_exc(file=sys.stderr)
                self.put_status('FAILED', exception=err)
            else:
Example #2
0
class Context(object):
    def __init__(self):
        self.service = TaskProxyService(self)
        self.datastore = None
        self.conn = None
        self.instance = None
        self.running = Event()

    def put_status(self, state, result=None, exception=None):
        obj = {"status": state, "result": None}

        if result:
            obj["result"] = result

        if exception:
            obj["error"] = serialize_error(exception)

        self.conn.call_sync("task.put_status", obj)

    def main(self):
        if len(sys.argv) != 2:
            print("Invalid number of arguments", file=sys.stderr)
            sys.exit(errno.EINVAL)

        key = sys.argv[1]
        logging.basicConfig(file=sys.stdout, level=logging.DEBUG)
        try:
            self.datastore = get_default_datastore()
            self.conn = Client()
            self.conn.connect("127.0.0.1")
            self.conn.login_service("task.{0}".format(os.getpid()))
            self.conn.enable_server()
            self.conn.rpc.register_service_instance("taskproxy", self.service)
            task = self.conn.call_sync("task.checkin", key)

            module = imp.load_source("plugin", task["filename"])
            setproctitle.setproctitle("task executor (tid {0})".format(task["id"]))

            try:
                self.instance = getattr(module, task["class"])(DispatcherWrapper(self.conn), self.datastore)
                self.instance.configstore = ConfigStore(self.datastore)
                self.running.set()
                result = self.instance.run(*task["args"])
            except BaseException, err:
                print("Task exception: {0}".format(str(err)), file=sys.stderr)
                traceback.print_exc(file=sys.stderr)
                self.put_status("FAILED", exception=err)
            else:
Example #3
0
class BaseTestCase(unittest.TestCase):
    class TaskState(object):
        def __init__(self):
            self.tid = None
            self.state = None
            self.message = None
            self.result = None
            self.ended = Event()

    def __init__(self, methodName):
        super(BaseTestCase, self).__init__(methodName)
        self.tasks = {}
        self.tasks_lock = Lock()
        self.conn = None
        self.task_timeout = 20

    def setUp(self):
        try:
            self.conn = Client()
            self.conn.event_callback = self.on_event
            self.conn.connect(os.getenv('TESTHOST', '127.0.0.1'))
            self.conn.login_service('tests')
            self.conn.subscribe_events('*')
        except:
            raise

    def tearDown(self):
        self.conn.disconnect()

    def submitTask(self, name, *args):
        self.tasks_lock.acquire()
        try:
            tid = self.conn.call_sync('task.submit', name, args)
        except RpcException:
            self.tasks_lock.release()
            raise

        self.tasks[tid] = self.TaskState()
        self.tasks[tid].tid = tid
        self.tasks_lock.release()
        return tid

    def assertTaskCompletion(self, tid):
        t = self.tasks[tid]
        if not t.ended.wait(self.task_timeout):
            self.fail('Task {0} timed out'.format(tid))

        self.assertEqual(t.state, 'FINISHED', msg=t.message)

    def assertTaskFailure(self, tid):
        t = self.tasks[tid]
        if not t.ended.wait(self.task_timeout):
            self.fail('Task {0} timed out'.format(tid))

        self.assertNotEqual(t.state, 'FINISHED', msg=t.message)

    def assertSeenEvent(self, name, func=None):
        pass

    def getTaskResult(self, tid):
        t = self.tasks[tid]
        return t.result

    def on_event(self, name, args):
        self.tasks_lock.acquire()

        if name == 'task.updated':
            t = self.tasks[args['id']]
            t.state = args['state']
            if t.state in ('FINISHED', 'FAILED'):
                t.result = args['result'] if 'result' in args else None
                t.ended.set()

        elif name == 'task.progress':
            t = self.tasks[args['id']]
            t.message = args['message']

        self.tasks_lock.release()