コード例 #1
0
    def innerMethodWrapper(resourceInstanceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.debug(SchedulerMethod,
                  f'''wrapping {resourceInstanceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(
            resourceInstanceMethod)
        methodName = ReflectionHelper.getName(resourceInstanceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.scheduler.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            methodReturn = None
            try:
                FlaskManager.validateArgs(args, requestClass,
                                          innerResourceInstanceMethod)
                methodReturn = resourceInstanceMethod(*args, **kwargs)
            except Exception as exception:
                FlaskManager.raiseGlobalException(exception, resourceInstance,
                                                  resourceInstanceMethod)
                log.log(innerResourceInstanceMethod,
                        f'Not possible to run {shedulerId} properly',
                        exception=exception)
            return methodReturn

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceInstanceMethod)
        return innerResourceInstanceMethod
コード例 #2
0
    def innerMethodWrapper(resourceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.wrapper(SchedulerMethod, f'''wrapping {resourceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(resourceMethod)
        methodName = ReflectionHelper.getName(resourceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        resourceMethod.disabled = disable
        resourceMethod.shedulerId = methodKwargs['id']
        resourceMethod.muteLogs = muteLogs or ConverterStatic.getValueOrDefault(
            apiInstance.globals.getApiSetting(
                ConfigurationKeyConstant.API_SCHEDULER_MUTE_LOGS),
            DEFAUTL_MUTE_LOGS)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.schedulerManager.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            muteLogs = resourceInstance.muteLogs or resourceMethod.muteLogs
            if resourceInstance.enabled and not resourceInstance.disabled and not resourceMethod.disabled:
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler started with args={methodArgs} and kwargs={methodKwargs}'
                    )
                methodReturn = None
                try:
                    FlaskManager.validateArgs(args, requestClass,
                                              innerResourceInstanceMethod)
                    methodReturn = resourceMethod(*args, **kwargs)
                except Exception as exception:
                    if not muteLogs:
                        log.warning(
                            resourceMethod,
                            f'Not possible to run {resourceMethod.shedulerId} properly',
                            exception=exception,
                            muteStackTrace=True)
                    FlaskManager.raiseAndPersistGlobalException(
                        exception, resourceInstance, resourceMethod)
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler finished')
                return methodReturn
            if not muteLogs:
                log.warning(
                    resourceMethod,
                    f'{resourceMethod.shedulerId} scheduler didn{c.SINGLE_QUOTE}t started. {"Schedulers are disabled" if not resourceInstance.enabled else "This scheduler is disabled" if resourceInstance.disabled else "This scheduler method is disabled"}'
                )

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceMethod)
        resourceMethod.shedulerId = methodKwargs.get('id')
        innerResourceInstanceMethod.disable = resourceMethodDisable
        innerResourceInstanceMethod.muteLogs = resourceMethodMuteLogs
        return innerResourceInstanceMethod