Example #1
0
def shw_status(main_p, command_data, web=False, recursive=False):
    task_list_obj = main_p.thread_daemon_list['Core'].all_task_object
    group_list_obj = main_p.thread_daemon_list['Core'].all_group
    try:
        return_hash = {}
        return_hash['STATUS'] = []
        if recursive:
            command_string = command_data
        else:
            command_string = ' '.join(command_data[2:])

        if type(command_string) == type('') and len(command_string.strip()) == 0: command_string = 'all'

        for command in map(lambda x:x.strip(), command_string.split(',')):
            if command not in task_list_obj:
                if command.lower() == 'all':
                    for _task_name in task_list_obj.keys():
                        ret = shw_status(main_p, _task_name, recursive=True)
                        for _key in ret['STATUS']:
                            return_hash['STATUS'].append(_key)
                elif command in group_list_obj:
                    for _task_name in group_list_obj[command]:
                        ret = shw_status(main_p, _task_name, recursive=True)
                        for _key in ret['STATUS']:
                            return_hash['STATUS'].append(_key)
                else:
                    temp_hash = {}
                    temp_hash['name'] = command
                    temp_hash['error'] = 'NOK : [%s] task not exists' % command
                    return_hash['STATUS'].append(temp_hash)
            else:
                return_hash['STATUS'].append(task_list_obj[command].status)
        if recursive:
            return return_hash
        if not web:
            return_message = []
            prefix = '%10s | %86s'
            return_hash['STATUS'] = sorted(return_hash['STATUS'], key=lambda value:value['name'])
            for _item in return_hash['STATUS']:
                return_message.append('=' * 100)
                return_message.append(_item['name'])
                return_message.append('-' * 100)
                if 'error' in _item:
                    return_message.append( prefix % ('error', _item['error']) )
                    continue
                for _key in [ 'type', 'status', 'act-status', 'act-time', 'act-count', 'pid', 'command' ]:
                    return_message.append( prefix % ( _key, str(_item[_key]) ))
                return_message.append('-' * 100)
                return_message.append('last-status')
                return_message.append('-' * 100)
                for _key in [ 'std-in', 'std-out', 'std-err' ]:
                    return_message.append( prefix % ( _key, str(_item['last-status']['last-' + _key]) ) )
            return MF.make_message(return_message)
        else:
            return MF.make_json(return_hash)
    except Exception, e:
        error_msg = "NOK : Exception occured in shw status [%s]" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #2
0
def trm_task(main_p, command_data, web=False):
    try:
        return_message = []
        command_string = ' '.join(command_data[1:])
        for task_name in map(lambda x:x.strip(), command_string.split(',')):
            ret = main_p.thread_daemon_list['Core'].trm_task(task_name)
            for line in ret:
                return_message.append(line)
        if not web: return MF.make_message(return_message)
        else: return MF.simple_result_json(return_message)
    except Exception, e:
        error_msg = "NOK : Exception occured in trm task [%s]" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #3
0
def set_recovery(main_p, command_data, web=False):
    try:
        return_message = []
        command_string = " ".join(command_data[2:])
        for command in map(lambda x:x.strip(), command_string.split(',')):
            _task_name, _message = map(lambda x:x.strip(), command.split(':'))
            ret = main_p.thread_daemon_list['Core'].add_recovery(_task_name, _message)
            for line in ret:
                return_message.append(line)
        if not web: return MF.make_message(return_message)
        else: return MF.simple_result_json(return_message)
    except Exception, e:
        error_msg = "NOK : Exception occured in set recovery [%s]" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #4
0
def shw_global_queue(main_p, command_data, web=False):
    try:
        return_value = []
        for x in main_p.mon_deque:
            return_value.append('[%s] : %s : %s' % x)
        if not web:
            return MF.make_message(return_value)
        else:
            return MF.simple_result_json(
                return_value,
                header='GLOBAL_QUEUE',
                column=['DATETIME', 'DIRECTION', 'MESSAGE'])
    except Exception, e:
        error_msg = "NOK : Exception occured in shw global_queue [%s]" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #5
0
def shw_stdin(main_p, command_data, web=False, recursive=False):
    core_obj = main_p.thread_daemon_list['Core']
    if recursive:
        command_string = command_data
    else:
        command_string = ' '.join(command_data[2:])
    if command_string.strip() == '': command_string = 'all'
    result_hash = {}
    result_hash['QUEUES'] = []
    for task_name in map(lambda x: x.strip(), command_string.split(',')):
        if task_name not in core_obj.all_task_object:
            if task_name == 'all':
                for sub_task_name in core_obj.all_task_object:
                    sub_return_message = shw_stdin(main_p,
                                                   sub_task_name,
                                                   recursive=True)
                    for item in sub_return_message['QUEUES']:
                        result_hash['QUEUES'].append(item)
            else:
                temp_hash = {}
                temp_hash['NAME'] = task_name
                temp_hash['ERROR'] = []
                temp_hash['BROADCASTING STDIN'] = []
                temp_hash['SHARING STDIN'] = []
                temp_hash['ERROR'].append('NOK : task not exists')
                result_hash['QUEUES'].append(temp_hash)
        else:
            temp_hash = {}
            temp_hash['NAME'] = task_name
            temp_hash['ERROR'] = []
            temp_hash['BROADCASTING STDIN'] = []
            for msg in core_obj.all_task_object[
                    task_name].broadcast_deq.__copy__():
                temp_hash['BROADCASTING STDIN'].append(msg)
            temp_hash['SHARING STDIN'] = []
            for dst_task in core_obj.all_task_object[
                    task_name].sharing_task_list:
                for msg in dst_task.sharing_deq.__copy__():
                    temp_hash['SHARING STDIN'].append(msg)
            result_hash['QUEUES'].append(temp_hash)
    if recursive:
        return result_hash
    if not web:
        temp_list = result_hash['QUEUES'][:]
        sorted(temp_list, key=lambda queue_data: queue_data['NAME'])
        result_message = []
        for item in temp_list:
            result_message.append('=' * 100)
            result_message.append(item['NAME'])
            result_message.append('-' * 100)
            for _key in item:
                if _key == 'NAME': continue
                if len(item[_key]) == 0: continue
                result_message.append(_key)
                result_message.append('-' * 100)
                for line in item[_key]:
                    result_message.append('  ' + line)
        return MF.make_message(result_message)
    else:
        return MF.make_json(result_hash)
Example #6
0
def shw_schedule(main_p, command_data, web=False):
    try:
        parser = OptionParser()
        parser.add_option('-f', '--time-format', dest='time_format')
        parser.add_option('-t', '--task', dest='task')
        options, _ = parser.parse_args(command_data)
    except OptionParsingError, e:
        error_msg = 'NOK : shw schedule option error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #7
0
def set_flow(main_p, command_data, web=False):
    core_obj = main_p.thread_daemon_list['Core']
    try:
        return_message = []
        command_string = ' '.join(command_data[2:])
        for command in map(lambda x:x.strip(), command_string.split(',')):
            flow_type, flow_from, flow_to = map(lambda x:x.strip(), command.split(':'))
            for flow_from_item in map(lambda x:x.strip(), flow_from.split('|')):
                for flow_to_item in map(lambda x:x.strip(), flow_to.split('|')):
                    ret = core_obj.add_flow(flow_type.lower(), flow_from_item, flow_to_item)
                    for item in ret:
                        return_message.append(item)
                    try: main_p.flow_conn.set(flow_type.lower(), flow_from_item, flow_to_item)
                    except Exception, e: return_message.append( 'NOK : Exception occured in set flow save : %s' % str(e) )
        if not web: return MF.make_message(return_message)
        else: return MF.simple_result_json(return_message)
    except Exception, e:
        error_msg = "NOK : Exception occured in set flow [%s]" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #8
0
def del_schedule(main_p, command_data, web=False):
    command_string = ' '.join(command_data[2:])
    try:
        for command in map(lambda x:x.strip(), command_string.split(',')):
            time_info, dst_task = map(lambda x:x.strip(), command_string.split(':'))
            if len(time_info.split()) != 5: raise Exception, "time format follow cron expression"
            main_p.schedule_conn.rmv(time_info, dst_task)
    except Exception, e:
        error_msg = 'NOK : Exception occured in del schedule [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #9
0
def shw_task(main_p, command_data, web=False):
    try:
        parser = OptionParser()
        parser.add_option('-t', '--task', dest='task')
        parser.add_option('-y', '--task-type', dest='task_type')
        parser.add_option('-s', '--status', dest='status')
        parser.add_option('-a', '--act-status', dest='act_status')
        parser.add_option('-p', '--pid', dest='pid')
        options, _ = parser.parse_args(command_data)
    except OptionParsingError, e:
        error_msg = 'NOK : shw task option error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #10
0
def set_schedule(main_p, command_data, web=False):
    core_obj = main_p.thread_daemon_list['Core']
    command_string = ' '.join(command_data[2:])
    try:
        for command in map(lambda x:x.strip(), command_string.split(',')):
            time_info, dst_task, message = map(lambda x:x.strip(), command_string.split(':'))
            if len(time_info.split()) != 5: raise Exception, 'time format follow cron expression'
            if dst_task not in core_obj.all_task_object: raise Exception, '[%s] task not exists' % dst_task
            main_p.schedule_conn.set(time_info, dst_task, message)
    except Exception, e:
        error_msg = 'NOK : Exception occured in set schedule [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #11
0
def set_stdin(main_p, command_data, web=False):
    return_message = []
    command_string = ' '.join(command_data[2:])
    for command in map(lambda x: x.strip(), command_string.split(',')):
        dst_task, message = command.split(':')
        if dst_task not in main_p.thread_daemon_list['Core'].all_task_object:
            return_message.append("NOK : [%s] task not exists" % dst_task)
            continue
        main_p.thread_daemon_list['Core'].all_task_object[dst_task].put_queue(
            message)
        return_message.append("OK : set stdin [%s] to %s" %
                              (message, dst_task))
    if not web: return MF.make_message(return_message)
    else: return MF.simple_result_json(return_message)
Example #12
0
def shw_ready(main_p, command_data, web=False):
    return_message = []
    task_list_obj = main_p.thread_daemon_list['Core'].all_task_object
    add_list_obj = main_p.thread_daemon_list['Core'].add_list
    command_string = ' '.join(command_data[2:])
    for task_name in map(lambda x:x.strip(), command_string.split(',')):
        if task_name in task_list_obj:
            return_message.append("OK : [%s] task ready" % task_name)
            continue
        message_data = "NOK : [%s] task not exists" % task_name
        for item in add_list_obj:
            if item[1] == task_name: message_data = "OK : [%s] task not ready" % task_name
        return_message.append(message_data)
    if not web: return MF.make_message(return_message)
    else: return MF.simple_result_json(return_message)
Example #13
0
def lad_task_queue(main_p, command_data, web=False):
    tasks = main_p.thread_daemon_list['Core'].all_task_object
    command_string = ' '.join(command_data[1:])
    result_message = []
    try:
        if command_string.strip() == '': command_string = 'all'
        task_list = map(lambda x: x.strip(), command_string.split(','))
        if 'all' in task_list:
            for task_name in tasks.keys():
                if task_name not in task_list: task_list.append(task_name)

        for task_name in task_list:
            if task_name not in tasks:
                result_message.append('NOK : [%s] task not exist')
                continue
            ret = tasks[task_name].load_queue()
            for line in ret:
                result_message.append(line)
        if not web: return MF.make_message(result_message)
        else: return MF.simple_result_json(result_message)
    except Exception, e:
        error_msg = "NOK : Exception occured in del task queue : %s" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])
Example #14
0
def del_task(main_p, command_data, web=False):
    try:
        return_message = []
        command_string = ' '.join(command_data[2:])
        for task_name in map(lambda x:x.strip(), command_string.split(',')):
            ret = main_p.thread_daemon_list['Core'].del_task(task_name)
            for line in ret:
                return_message.append(line)
            try:
                main_p.node_conn.rmv(task_name)
            except Exception, e:
                return_message.append( 'NOK : Exception occured in del task db : %s' % str(e))
        if not web:
            return MF.make_message(return_message)
        else:
            return_data = {}
            return_data['RESULTS'] = []
            for message in return_message:
                result, msg = map(lambda x:x.strip(), message.strip().split(':', 1))
                temp_hash = {}
                temp_hash['RESULT'] = result
                temp_hash['MESSAGE'] = msg
                return_data['RESULTS'].append(temp_hash)
            return MF.make_json(return_data)
Example #15
0
        parser.add_option('-f', '--flow-type', dest='flow_type')
        parser.add_option('-t', '--task', dest='task')
        options, _ = parser.parse_args(command_data)
    except OptionParsingError, e:
        error_msg = 'NOK : shw flow option error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])

    try:
        if options.flow_type != None:
            options.flow_type = map(lambda x:x.strip(), options.flow_type.strip().split(','))
        if options.task != None:
            options.task = map(lambda x:x.strip(), options.task.strip().split(','))
    except Exception, e:
        error_msg = 'NOK : shw flow option split error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])

    def view_check(options, data, message):
        if options.flow_type != None:
            if data[0] not in options.flow_type: return message

        if options.task != None:
            if data[1] not in options.task:
                add_check = False
                for item in data[2]:
                    if item in options.task: add_check = True
                if not add_check: return message
        message.append(data)
        return message
                
Example #16
0
        parser.add_option('-p', '--pid', dest='pid')
        options, _ = parser.parse_args(command_data)
    except OptionParsingError, e:
        error_msg = 'NOK : shw task option error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])

    try:
        if options.task: options.task = map(lambda x:x.strip(), options.task.split(','))
        if options.task_type: options.task_type = map(lambda x:x.strip(), options.task_type.split(','))
        if options.status: options.status = map(lambda x:x.strip(), options.status.split(','))
        if options.act_status: options.act_status = map(lambda x:x.strip(), options.act_status.split(','))
        if options.pid: options.pid = map(lambda x:x.strip(), options.pid.split(','))
    except Exception, e:
        error_msg = 'NOK : shw task option split error [%s]' % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])

    def view_filter(options, data, message):
        if options.task != None and data[1] not in options.task: return message
        if options.task_type != None and data[0] not in options.task_type: return message
        if options.status != None and data[2] not in options.status: return message
        if options.act_status != None and data[3] not in options.act_status: return message
        if options.pid != None and data[6] not in options.pid: return message
        message.append(data)
        return message

    try:
        task_name_list = main_p.thread_daemon_list['Core'].all_task_object.keys()
        result_list = []
        for task_name in task_name_list:
Example #17
0
                                    'Core'].all_task_object[
                                        _task_name].clear_queue():
                                return_value.append(line)
                        except Exception, e:
                            return_value.append(
                                "NOK : Exception occured in clear queue [%s]" %
                                str(e))
                else:
                    return_value.append("NOK : [%s] task not exists" %
                                        task_name)
            else:
                for line in main_p.thread_daemon_list['Core'].all_task_object[
                        task_name].clear_queue():
                    return_value.append(line)
        if not web:
            return MF.make_message(return_value)
        else:
            return MF.simple_result_json(return_value)
    except Exception, e:
        error_msg = "NOK : Exception occured in del task queue : %s" % str(e)
        if not web: return MF.make_message(error_msg)
        else: return MF.simple_result_json([error_msg])


def sav_task_queue(main_p, command_data, web=False):
    tasks = main_p.thread_daemon_list['Core'].all_task_object
    command_string = ' '.join(command_data[1:])
    result_message = []
    try:
        if command_string.strip() == '': command_string = 'all'
        task_list = map(lambda x: x.strip(), command_string.split(','))
Example #18
0
    def command(self, line):
        command_data = line.strip().split()


        if command_data[-1].lower() == 'hlp':
            try: 
                if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), command_data[1].lower()))
            except:
                try:
                    if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), None))
                except:
                    if not self.web: return MF.make_message(HLPMessage.help_msg('hlp', None))

        if len(command_data) == 1 and command_data[0].lower() in [ 'shw', 'set', 'del' ]:
            if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), None))

        if command_data[0].lower() == 'shw':
            if command_data[1].lower() == 'conf':
                return ConfigInterface.shw_conf(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'task':
                return TaskInterface.shw_task(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'flow':
                return FlowInterface.shw_flow(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'group':
                return GroupInterface.shw_group(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'stdin':
                return StdInInterface.shw_stdin(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'schedule':
                return ScheduleInterface.shw_schedule(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'global_queue':
                return QueueInterface.shw_global_queue(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'status':
                return TaskInterface.shw_status(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'recovery':
                return TaskInterface.shw_recovery(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'ready':
                return TaskInterface.shw_ready(self.main_p, command_data, web=self.web)
            else:
                if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), None))

        elif command_data[0].lower() == 'set':
            if command_data[1].lower() == 'conf':
                return ConfigInterface.set_conf(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'task':
                return TaskInterface.set_task(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'flow':
                return FlowInterface.set_flow(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'group':
                return GroupInterface.set_group(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'stdin':
                return StdInInterface.set_stdin(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'schedule':
                return ScheduleInterface.set_schedule(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'recovery':
                return TaskInterface.set_recovery(self.main_p, command_data, web=self.web)
            else:
                if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), None))

        elif command_data[0].lower() == 'del':
            if command_data[1].lower() == 'task':
                return TaskInterface.del_task(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'flow':
                return FlowInterface.del_flow(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'group':
                return GroupInterface.del_group(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'schedule':
                return ScheduleInterface.del_schedule(self.main_p, command_data, web=self.web)
            elif command_data[1].lower() == 'task_queue':
                return QueueInterface.del_task_queue(self.main_p, command_data, web=self.web)
            else:
                if not self.web: return MF.make_message(HLPMessage.help_msg(command_data[0].lower(), None))

        elif command_data[0].lower() == 'act':
            return TaskInterface.act_task(self.main_p, command_data, web=self.web)
        elif command_data[0].lower() == 'trm':
            return TaskInterface.trm_task(self.main_p, command_data, web=self.web)

        elif command_data[0].lower() == 'lad':
            return QueueInterface.lad_task_queue(self.main_p, command_data, web=self.web)
        elif command_data[0].lower() == 'sav':
            return QueueInterface.sav_task_queue(self.main_p, command_data, web=self.web)

        if not self.web: return MF.make_message(HLPMessage.help_msg('hlp', None))
        else: return MF.make_json( { 'RESULT' : 'NOK', 'MESSAGE' : 'unknown command [%s]' % repr(line) } )