def get_task_logger(worker, task, subtask=None, workunit=None): """ Initializes a logger for tasks and subtasks. Logs for tasks are stored as in separate files and aggregated. This allow workunits to be viewed in a single log. Otherwise a combined log could contain messages from many different workunits making it much harder to grok. @param worker: there may be more than one Worker per Node. Logs are stored per worker. @param task: ID of the task instance. Each task instance receives its own log. @param subtask: (optional) subtask_key. see workunit_id @param workunit: (optional) ID of workunit. workunits receive their own log file so that the log can be read separately. This is separate from the task instance log. """ directory, filename = task_log_path(task, subtask, workunit, worker) makedirs(directory) logger_name = 'task.%s' % task if workunit: logger_name += '.%s' % workunit logger = logging.getLogger(logger_name) handler = FileHandler(filename) formatter = logging.Formatter(LOG_FORMAT % ("[%s]" % worker)) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(settings.LOG_LEVEL) return logger
def create_package(self, package='test'): """ Creates a package we can place test files into. by default this will stick a single file in the package with a basic task. """ dir = os.path.join(self.task_manager.tasks_dir, package) makedirs(dir) self.packages.append(package)
def create_cache_entry(self, hash='FAKE_HASH'): """ Creates an entry in the task_cache_internal """ internal_folder = os.path.join(self.manager.tasks_dir_internal, self.package, hash) pkg_dir = '%s/%s' % (pydra_settings.TASKS_DIR, self.package) makedirs(pkg_dir) shutil.copytree(pkg_dir, internal_folder)
def __init__(self, scan_interval=20, lazy_init=False): """ @param scan_interval - interval at which TASKS_DIR is scanned for changes. No scanning when set to None @param lazy_init - lazy init causes tasks to only be loaded when requested. Assumes scan_interval=None """ self._interfaces = [ self.list_tasks, self.task_history, self.task_history_detail, self.task_log, ] self._listeners = { 'TASK_RELOAD':self.init_package, 'TASK_STARTED':self._task_started, 'TASK_STARTED':self._task_stopped, } if lazy_init: self.lazy_init = True else: self._listeners['MANAGER_INIT'] = self.init_task_cache self.tasks_dir = pydra_settings.TASKS_DIR self.tasks_dir_internal = pydra_settings.TASKS_DIR_INTERNAL makedirs(self.tasks_dir_internal) # full_task_key or pkg_name: pkg_object # preserved for both compatibility and efficiency self.registry = {} self.package_dependency = graph.DirectedGraph() self._task_callbacks = {} # task_key : callback list self._lock = RLock() self._callback_lock = Lock() self.__initialized = False # in seconds, None causes no updates self.scan_interval = scan_interval
def receive_log(self, response, worker, task, subtask=None, workunit=None): """ Callback from a successful call to Node.send_log. Saves the log locally, updates state, and deletes the remote log. @param worker - id of worker sending the log @param task - id of task @param subtask - task path for subtask, default=None @param workunit - id of workunit, default=None """ logger.debug('Receiving Log: %s %s %s' % (task, subtask, workunit)) if not response: logger.debug("Bogus log: %s %s %s" % (task, subtask, workunit)) return log = zlib.decompress(response) d, path = task_log_path(task, subtask, workunit) try: makedirs(d) except OSError: # Couldn't make our path to our log; give up. logger.debug("Couldn't recieve log: %s %s %s" % (task, subtask, workunit)) return out = open(path, 'w') out.write(log) # mark log as received if workunit: item = WorkUnit.objects.get(task_instance__id=task, \ workunit=workunit) else: item = TaskInstance.objects.get(id=task) item.log_retrieved = True item.save() # delete remote log remote = self.workers[worker] remote.callRemote('delete_log', task, subtask, workunit)
def __init__(self, scan_interval=20, lazy_init=False): """ Constructor. If `lazy_init` is True, `scan_interval` is forced to 0, disabling autodiscovery of tasks. :Parameters: scan_interval : int If non-zero, the manager will automatically scan TASKS_DIR at this interval for new tasks. lazy_init : bool Whether tasks should be lazily loaded. When False, tasks will be loaded on discovery; when True, tasks will be loaded on demand. """ self._interfaces = [ self.list_tasks, self.task_history, self.task_history_detail, self.task_log, ] self._listeners = { 'TASK_RELOAD':self.init_package, 'TASK_STARTED':self._task_started, 'TASK_STARTED':self._task_stopped, } self.lazy_init = lazy_init # Interval, in seconds, between scans of the task folders for new # tasks. None disables scanning. self.scan_interval = scan_interval if self.lazy_init: self.scan_interval = 0 else: self._listeners['MANAGER_INIT'] = self.init_task_cache self.tasks_dir = pydra_settings.TASKS_DIR self.tasks_dir_internal = pydra_settings.TASKS_DIR_INTERNAL makedirs(self.tasks_dir_internal) # full_task_key or pkg_name: pkg_object # preserved for both compatibility and efficiency self.registry = {} self.package_dependency = graph.DirectedGraph() self._task_callbacks = defaultdict(list) """ Dictionary mapping task keys to a list of `Deferred`s waiting to be fired. """ self._lock = RLock() self.__initialized = False if self.scan_interval: self.autodiscover_call = LoopingCall(self.autodiscover)