Exemplo n.º 1
0
def manage_manual_action():
    logid = {'logid': "%s" % ('manual_action')}
    logger.debug("manage_manual_action", extra=logid)

    def getfirstmanualaction():
        manual_selects = DMFileStat.objects.filter(action_state__in=['SA', 'SE', 'SD']).order_by('created')
        for item in manual_selects:
            try:
                dmactions.action_validation(item, item.dmfileset.auto_action)
                return item
            except(DMExceptions.FilesInUse, DMExceptions.FilesMarkedKeep):
                logger.debug("%s Failed action_validation.  Try next fileset" %
                             item.result.resultsName, extra=logid)
            except DMExceptions.BaseInputLinked:
                # want to allow Basecalling Input delete if all results are expired
                related_objs = DMFileStat.objects.filter(
                    result__experiment=item.result.experiment, dmfileset__type=dmactions_types.BASE)
                if related_objs.count() == related_objs.filter(created__lt=threshdate).count():
                    item.allow_delete = True
                    return item
                else:
                    logger.debug("%s Failed action_validation.  Try next fileset" %
                                 item.result.resultsName, extra=logid)
            except:
                logger.error(traceback.format_exc(), extra=logid)
        raise DMExceptions.NoDMFileStat("NONE FOUND")

    try:
        # Create lock file to prevent more than one celery task for manual action (export or archive)
        lock_id = 'manual_action_lock_id'
        logid = {'logid': "%s" % (lock_id)}
        applock = TaskLock(lock_id)

        if not applock.lock():
            logger.info("Currently processing: %s(%s)" % (lock_id, applock.get()), extra=logid)
            return

        logger.debug("lock file created: %s(%s)" % (lock_id, applock.get()), extra=logid)

    except Exception as e:
        logger.error(traceback.format_exc(), extra=logid)

    try:
        #
        # Check for manually selected Archive and Export actions - action_state == 'SA' or 'SE' or 'SD'
        # Manual Delete actions do not get processed here.  Only suspended Delete actions get processed here.
        # These jobs should execute even when auto action is disabled.
        # Note: manual actions will not be executed in the order they are selected, but by age.
        #
        actiondmfilestat = getfirstmanualaction()

        user = actiondmfilestat.user_comment.get('user', 'dm_agent')
        user_comment = actiondmfilestat.user_comment.get('user_comment', 'Manual Action')
        logger.info("Picked: %s" % (actiondmfilestat.result.resultsName), extra=logid)

        dmfileset = actiondmfilestat.dmfileset
        applock.update(actiondmfilestat.result.resultsName)
        if actiondmfilestat.action_state == 'SA':
            logger.info("Manual Archive Action: %s from %s" %
                        (dmfileset.type, actiondmfilestat.result.resultsName), extra=logid)
            archive_action(user, user_comment, actiondmfilestat, lock_id, msg_banner=True)
        elif actiondmfilestat.action_state == 'SE':
            logger.info("Manual Export Action: %s from %s" %
                        (dmfileset.type, actiondmfilestat.result.resultsName), extra=logid)
            export_action(user, user_comment, actiondmfilestat, lock_id, msg_banner=True)
        elif actiondmfilestat.action_state == 'SD':
            logger.info("Delete Action: %s from %s" %
                        (dmfileset.type, actiondmfilestat.result.resultsName), extra=logid)
            delete_action(user, "Continuing delete action after being suspended",
                          actiondmfilestat, lock_id, msg_banner=True)
        else:
            logger.warn("Dev Error: we don't handle this '%s' here" %
                        actiondmfilestat.action_state, extra=logid)

    except DMExceptions.NoDMFileStat:
        applock.unlock()
        logger.debug("Worker PID %d lock_id destroyed on exit %s" % (os.getpid(), lock_id), extra=logid)
    # NOTE: all these exceptions are also handled in manage_data() below.  beaucoup de duplication de code
    except (DMExceptions.FilePermission,
            DMExceptions.InsufficientDiskSpace,
            DMExceptions.MediaNotSet,
            DMExceptions.MediaNotAvailable,
            DMExceptions.FilesInUse) as e:
        applock.unlock()
        message = Message.objects.filter(tags__contains=e.tag)
        if not message:
            Message.error(e.message, tags=e.tag)
            # TODO: TS-6525: This logentry will repeat for an Archive action every 30 seconds until the cause of the exception is fixed.
            # at least, while the message banner is raised, suppress additional Log Entries.
            dmactions.add_eventlog(actiondmfilestat, "%s - %s" %
                                   (dmfileset.type, e.message), username='******')
        # State needs to be Error since we do not know previous state (ie, do NOT
        # set to Local, it might get deleted automatically)
        actiondmfilestat.setactionstate('E')
    except DMExceptions.SrcDirDoesNotExist as e:
        applock.unlock()
        msg = "Src Dir not found: %s. Setting action_state to Deleted" % e.message
        EventLog.objects.add_entry(actiondmfilestat.result, msg, username='******')
        actiondmfilestat.setactionstate('DD')
        logger.info(msg, extra=logid)
    except Exception as e:
        applock.unlock()
        msg = "action error on %s " % actiondmfilestat.result.resultsName
        msg += " Error: %s" % str(e)
        logger.error("%s - %s" % (dmfileset.type, msg), extra=logid)
        logger.error(traceback.format_exc(), extra=logid)
        dmactions.add_eventlog(actiondmfilestat, "%s - %s" % (dmfileset.type, msg), username='******')
        # State needs to be Error since we do not know previous state (ie, do NOT
        # set to Local, it might get deleted automatically)
        actiondmfilestat.setactionstate('E')

    return
Exemplo n.º 2
0
def manage_manual_action():
    logger.debug("manage_manual_action")
    try:
        #Create lock file to prevent more than one celery task for manual action (export or archive)
        lock_id = 'manual_action_lock_id'
        applock = TaskLock(lock_id)
        if not (applock.lock()):
            logger.debug("failed to acquire lock file: %d" % os.getpid())
            logger.info("manage_manual_action task still executing")
            return

        logger.debug("Worker PID %d lock_id created %s" %
                     (os.getpid(), lock_id))

    except Exception as e:
        logger.exception(e)

    try:
        #
        # Check for manually selected Archive and Export actions - action_state == 'SA' or 'SE' or 'SD'
        # Manual Delete actions do not get processed here.  Only suspended Delete actions get processed here.
        # These jobs should execute even when auto action is disabled.
        # Note: manual actions will not be executed in the order they are selected, but by age.
        #
        manualSelects = DMFileStat.objects.filter(
            action_state__in=['SA', 'SE', 'SD']).order_by('created')
        if manualSelects.exists():
            actiondmfilestat = manualSelects[0]
            user = actiondmfilestat.user_comment.get('user', 'dm_agent')
            user_comment = actiondmfilestat.user_comment.get(
                'user_comment', 'Manual Action')

            dmfileset = actiondmfilestat.dmfileset
            if actiondmfilestat.action_state == 'SA':
                logger.info(
                    "Manual Archive Action: %s from %s" %
                    (dmfileset.type, actiondmfilestat.result.resultsName))
                archive_action(user,
                               user_comment,
                               actiondmfilestat,
                               lock_id,
                               msg_banner=True)
            elif actiondmfilestat.action_state == 'SE':
                logger.info(
                    "Manual Export Action: %s from %s" %
                    (dmfileset.type, actiondmfilestat.result.resultsName))
                export_action(user,
                              user_comment,
                              actiondmfilestat,
                              lock_id,
                              msg_banner=True)
            elif actiondmfilestat.action_state == 'SD':
                logger.info(
                    "Delete Action: %s from %s" %
                    (dmfileset.type, actiondmfilestat.result.resultsName))
                delete_action(user,
                              "Continuing delete action after being suspended",
                              actiondmfilestat,
                              lock_id,
                              msg_banner=True)
            else:
                logger.warn("Dev Error: we don't handle this '%s' here" %
                            actiondmfilestat.action_state)

        else:
            applock.unlock()
            logger.debug("Worker PID %d lock_id destroyed on exit %s" %
                         (os.getpid(), lock_id))


#NOTE: all these exceptions are also handled in manage_data() below.  beaucoup de duplication de code
    except (DMExceptions.FilePermission, DMExceptions.InsufficientDiskSpace,
            DMExceptions.MediaNotSet, DMExceptions.MediaNotAvailable,
            DMExceptions.FilesInUse) as e:
        applock.unlock()
        message = Message.objects.filter(tags__contains=e.tag)
        if not message:
            Message.error(e.message, tags=e.tag)
            #TODO: TS-6525: This logentry will repeat for an Archive action every 30 seconds until the cause of the exception is fixed.
            #at least, while the message banner is raised, suppress additional Log Entries.
            dmactions.add_eventlog(actiondmfilestat,
                                   "%s - %s" % (dmfileset.type, e.message),
                                   username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')
    except DMExceptions.SrcDirDoesNotExist as e:
        applock.unlock()
        msg = "Src Dir not found: %s. Setting action_state to Deleted" % e.message
        EventLog.objects.add_entry(actiondmfilestat.result,
                                   msg,
                                   username='******')
        actiondmfilestat.setactionstate('DD')
        logger.info(msg)
    except Exception as e:
        applock.unlock()
        msg = "action error on %s " % actiondmfilestat.result.resultsName
        msg += " Error: %s" % str(e)
        logger.exception("%s - %s" % (dmfileset.type, msg))
        dmactions.add_eventlog(actiondmfilestat,
                               "%s - %s" % (dmfileset.type, msg),
                               username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')

    return
Exemplo n.º 3
0
def manage_manual_action():
    logger.debug("manage_manual_action")
    try:
        #Create lock file to prevent more than one celery task for manual action (export or archive)
        lockfile = '/var/run/celery/manual_action.lock'
        applock = ApplicationLock(lockfile)
        if not(applock.lock()):
            logger.debug("failed to acquire lock file: %d" % os.getpid())
            logger.info("manage_manual_action task still executing")
            return

        logger.info("Worker PID %d lockfile created %s" % (os.getpid(),lockfile))

    except Exception as e:
        logger.exception(e)

    try:
        #
        # Check for manually selected Archive and Export actions - action_state == 'SA' or 'SE'
        # These jobs should execute even when auto action is disabled.
        # Note: manual actions will not be executed in the order they are selected, but by age.
        #
        user_comment = "Manual Action"
        manualSelects = DMFileStat.objects.filter(action_state__in=['SA','SE']).order_by('created')
        if manualSelects.exists():
            actiondmfilestat = manualSelects[0]
            dmfileset = actiondmfilestat.dmfileset
            project_msg = {}
            msg_dict = {}
            msg_dict[dmfileset.type] = "Success"
            project_msg[actiondmfilestat.result_id] = msg_dict
            if actiondmfilestat.action_state == 'SA':
                logger.info("Manual Archive Action: %s from %s" % (dmfileset.type,actiondmfilestat.result.resultsName))
                archive_action('dm_agent', user_comment, actiondmfilestat)
                datatasks.project_msg_banner('', project_msg, dmactions.ARCHIVE)
            elif actiondmfilestat.action_state == 'SE':
                logger.info("Manual Export Action: %s from %s" % (dmfileset.type,actiondmfilestat.result.resultsName))
                export_action('dm_agent', user_comment, actiondmfilestat)
                datatasks.project_msg_banner('', project_msg, dmactions.EXPORT)
            else:
                logger.warn("Dev Error: we don't handle this '%s' here" % actiondmfilestat.action_state)
            return
#NOTE: all these exceptions are also handled in manage_data() below.  beaucoup de duplication de code
    except (DMExceptions.FilePermission,
            DMExceptions.InsufficientDiskSpace,
            DMExceptions.MediaNotSet,
            DMExceptions.MediaNotAvailable,
            DMExceptions.FilesInUse) as e:
        message  = Message.objects.filter(tags__contains=e.tag)
        if not message:
            Message.error(e.message,tags=e.tag)
            #TODO: TS-6525: This logentry will repeat for an Archive action every 30 seconds until the cause of the exception is fixed.
            #at least, while the message banner is raised, suppress additional Log Entries.
            dmactions.add_eventlog(actiondmfilestat, "%s - %s" % (dmfileset.type, e.message), username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')
    except DMExceptions.SrcDirDoesNotExist as e:
        if actiondmfilestat.dmfileset.type == dmactions_types.SIG:
            msg = "Src Dir not found: %s. Setting action_state to Deleted" % e.message
            EventLog.objects.add_entry(actiondmfilestat.result,msg,username='******')
            actiondmfilestat.setactionstate('DD')
            logger.info(msg)
    except Exception as e:
        msg = "action error on %s " % actiondmfilestat.result.resultsName
        msg += " Error: %s" % str(e)
        logger.exception("%s - %s" % (dmfileset.type, msg))
        dmactions.add_eventlog(actiondmfilestat,"%s - %s" % (dmfileset.type, msg),username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')
    finally:
        applock.unlock()
        logger.debug("(%05d,%05d) Release lock file" % (os.getppid(),os.getpid()))

    return
Exemplo n.º 4
0
def manage_manual_action():
    logger.debug("manage_manual_action", extra = {'logid':"%s" % ('manual_action')})
    try:
        #Create lock file to prevent more than one celery task for manual action (export or archive)
        lock_id = 'manual_action_lock_id'
        d = {'logid':"%s" % (lock_id)}
        applock = TaskLock(lock_id)

        if not(applock.lock()):
            logger.debug("lock file exists: %s(%s)" % (lock_id, applock.get()), extra = d)
            return

        logger.debug("lock file created: %s(%s)" % (lock_id, applock.get()), extra = d)

    except Exception as e:
        logger.error(traceback.format_exc(), extra = d)


    try:
        #
        # Check for manually selected Archive and Export actions - action_state == 'SA' or 'SE' or 'SD'
        # Manual Delete actions do not get processed here.  Only suspended Delete actions get processed here.
        # These jobs should execute even when auto action is disabled.
        # Note: manual actions will not be executed in the order they are selected, but by age.
        #
        manualSelects = DMFileStat.objects.filter(action_state__in=['SA','SE','SD']).order_by('created')
        if manualSelects.exists():
            actiondmfilestat = manualSelects[0]
            user = actiondmfilestat.user_comment.get('user', 'dm_agent')
            user_comment = actiondmfilestat.user_comment.get('user_comment', 'Manual Action')

            dmfileset = actiondmfilestat.dmfileset
            applock.update(actiondmfilestat.result.resultsName)
            if actiondmfilestat.action_state == 'SA':
                logger.info("Manual Archive Action: %s from %s" % (dmfileset.type,actiondmfilestat.result.resultsName), extra = d)
                archive_action(user, user_comment, actiondmfilestat, lock_id, msg_banner = True)
            elif actiondmfilestat.action_state == 'SE':
                logger.info("Manual Export Action: %s from %s" % (dmfileset.type,actiondmfilestat.result.resultsName), extra = d)
                export_action(user, user_comment, actiondmfilestat, lock_id, msg_banner = True)
            elif actiondmfilestat.action_state == 'SD':
                logger.info("Delete Action: %s from %s" % (dmfileset.type,actiondmfilestat.result.resultsName), extra = d)
                delete_action(user, "Continuing delete action after being suspended", actiondmfilestat, lock_id, msg_banner = True)
            else:
                logger.warn("Dev Error: we don't handle this '%s' here" % actiondmfilestat.action_state, extra = d)

        else:
            applock.unlock()
            logger.debug("Worker PID %d lock_id destroyed on exit %s" % (os.getpid(),lock_id), extra = d)
#NOTE: all these exceptions are also handled in manage_data() below.  beaucoup de duplication de code
    except (DMExceptions.FilePermission,
            DMExceptions.InsufficientDiskSpace,
            DMExceptions.MediaNotSet,
            DMExceptions.MediaNotAvailable,
            DMExceptions.FilesInUse) as e:
        applock.unlock()
        message  = Message.objects.filter(tags__contains=e.tag)
        if not message:
            Message.error(e.message,tags=e.tag)
            #TODO: TS-6525: This logentry will repeat for an Archive action every 30 seconds until the cause of the exception is fixed.
            #at least, while the message banner is raised, suppress additional Log Entries.
            dmactions.add_eventlog(actiondmfilestat, "%s - %s" % (dmfileset.type, e.message), username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')
    except DMExceptions.SrcDirDoesNotExist as e:
        applock.unlock()
        msg = "Src Dir not found: %s. Setting action_state to Deleted" % e.message
        EventLog.objects.add_entry(actiondmfilestat.result,msg,username='******')
        actiondmfilestat.setactionstate('DD')
        logger.info(msg, extra = d)
    except Exception as e:
        applock.unlock()
        msg = "action error on %s " % actiondmfilestat.result.resultsName
        msg += " Error: %s" % str(e)
        logger.error("%s - %s" % (dmfileset.type, msg), extra = d)
        logger.error(traceback.format_exc(), extra = d)
        dmactions.add_eventlog(actiondmfilestat,"%s - %s" % (dmfileset.type, msg),username='******')
        # Revert this dmfilestat object action-state to Local
        actiondmfilestat.setactionstate('L')

    return