Beispiel #1
0
def run(args
        ,**_ 
        ):
    
    def dp(msg):
        def _(state):
            logging.debug(msg % state)
        return _
        
    
    tmctx={ 
            "worker": { "ch": dp("Worker is: %s"), "up": dp("Worker is: %s") }
           #,"state":  { "up": dp, "down": dp }
           }
    
    ctx=dict(args)
    ctx["_tm"]=transition_manager(tmctx)
         
    ppid=os.getppid()    
    logging.info("Process pid: %s" % os.getpid())
    logging.info("Parent pid : %s" % ppid)
    logging.info("Starting loop...")
    while True:
        if os.getppid()!=ppid:
            logging.warning("Parent terminated... exiting")
            break

        try:
            iline=sys.stdin.readline().strip(" \n")
        except KeyboardInterrupt:
            raise
        except:
            raise BrokenPipe("Broken Pipe...")

        #logging.debug("Received: %s" % iline)

        if iline=="":
            continue

        try:
            jso=json.loads(iline)
        except:
            try:    logging.debug("Can't JSON decode: %s" % iline)
            except: pass
            continue            
        
        #logging.debug("Received: %s" % jso)

        try:
            topic=jso["topic"]
        except:
            logging.warning("Can't find 'topic' member in json object: %s" % jso)
            continue
        
        code, msg=process(ctx, topic, jso)
        hcode(code, msg)
Beispiel #2
0
def run( path=None
        ,polling_interval=None
        ,topic=None
        ,always=None
        ,**_
        ):

    code, path=resolve_path(path)
    if not code.startswith("ok"):
        raise Exception("Can't resolve path...: %s" % path)

    def loginfo(path, state, *_):
        logging.info("Path state '%s': %s" % (path, state))

    ctx={"topic": topic, "path": path, "always":always}
    ctx["_path"]={
                  "previous": "ok"
                  ,"ch": partial(loginfo, path)
                  }
    tm=transition_manager(ctx)
    

    ppid=os.getppid()
    logging.info("Process pid: %s" % os.getpid())
    logging.info("Parent pid : %s" % ppid)
    logging.info("Starting loop...")    
    while True:
        if os.getppid()!=ppid:
            logging.warning("Parent terminated... exiting")
            break

        code, maybe_subdirs=getsubdirs(path)
        tm.send(("_path", code))
        if code.startswith("ok"):
            process(ctx, maybe_subdirs)
        
        start_time=time.time()
        while True:
            ir, _w, _e=select.select([sys.stdin], [], [], polling_interval)
            if len(ir):
                iline=sys.stdin.readline()
                sys.stdout.write(iline)
                
            elapsed_time = time.time() - start_time
            if elapsed_time > polling_interval:
                break
Beispiel #3
0
def run(primary_path=None, compare_path=None, 
        dest_path=None,
        status_filename=None, check_path=None
        ,just_basename=None
        ,topic_name=None
        ,exts=None
        ,wait_status=None, polling_interval=None
        ,just_zppp=None, just_ppzp=None, just_com=None
        ,**_):

    if check_path is not None:
        ct=check_transition()

    if dest_path:
        code, dest_path=resolve_path(dest_path)
        if not code.startswith("ok"):
            raise Exception("can't destination path '%s'" % dest_path)
        
        logging.info("Creating (if necessary) destination path: %s" % dest_path)
        code, msg=mkdir_p(dest_path)
        if code!="ok":
            raise Exception("Can't create path: %s" % dest_path)

    code, primary_path=resolve_path(primary_path)
    if not code.startswith("ok"):
        raise Exception("can't resolve primary path '%s'" % primary_path)
    
    logging.info("Creating (if necessary) primary path: %s" % primary_path)
    mkdir_p(primary_path)
    
    code, compare_path=resolve_path(compare_path)
    if not code.startswith("ok"):
        raise Exception("can't resolve compare path '%s'" % compare_path)

    logging.info("Creating (if necessary) compare path: %s" % compare_path)
    mkdir_p(compare_path)
            
    if wait_status:
        status_path=os.path.join(primary_path, status_filename)
        logging.info("Using status file path: %s" % status_path)
    else: 
        status_path=None

    ### context for logging etc.
    ctx={
          "just_zppp": just_zppp
         ,"just_ppzp": just_ppzp
         ,"just_com":  just_com
         ,"just_list": just_zppp or just_ppzp or just_com
         
         ,"pp": primary_path
         ,"zp": compare_path
         ,"sp": status_path
         
         ,"pp_log" :{"up":    partial(ilog, primary_path)
                     ,"down":  partial(wlog, primary_path)
                     }
         ,"zp_log" :{"up":    partial(ilog, compare_path)
                     ,"down":  partial(wlog, compare_path)
                     }
         ,"topic_name": topic_name
         ,"exts": exts
         }

    ctx["tm"]=transition_manager(ctx)
    
    ppid=os.getppid()        
    logging.info("Process pid: %s" % os.getpid())
    logging.info("Parent  pid: %s" % ppid)
    logging.info("Starting loop...")
    while True:
        if os.getppid()!=ppid:
            logging.warning("Parent terminated... exiting")
            break
            
        if check_path is not None:
            try:    exists=os.path.exists(check_path)
            except: exists=False
            
            maybe_tr, _=ct.send(exists)
            if maybe_tr=="tr" and exists:
                logging.info("Check path: passed")
            if maybe_tr=="tr" and not exists:
                logging.info("Check path: failed - skipping")
        else:
            ## fake 'exists'
            exists=True

        if exists:            
            code, msg=check_if_ok(status_path, default="ok")
            maybe_process(ctx, code, msg, primary_path, compare_path, just_basename, dest_path)
        
        logging.debug("...sleeping for %s seconds" % polling_interval)
        sleep(polling_interval)