Search in sources :

Example 1 with Task

use of org.structr.agent.Task in project structr by structr.

the class MaintenanceResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if ((securityContext != null) && isSuperUser()) {
        if (this.taskOrCommand != null) {
            try {
                final App app = StructrApp.getInstance(securityContext);
                if (Task.class.isAssignableFrom(taskOrCommand)) {
                    Task task = (Task) taskOrCommand.newInstance();
                    app.processTasks(task);
                } else if (MaintenanceCommand.class.isAssignableFrom(taskOrCommand)) {
                    MaintenanceCommand cmd = (MaintenanceCommand) StructrApp.getInstance(securityContext).command(taskOrCommand);
                    // flush caches if required
                    if (cmd.requiresFlushingOfCaches()) {
                        app.command(FlushCachesCommand.class).execute(Collections.EMPTY_MAP);
                    }
                    // create enclosing transaction if required
                    if (cmd.requiresEnclosingTransaction()) {
                        try (final Tx tx = app.tx()) {
                            cmd.execute(propertySet);
                            tx.success();
                        }
                    } else {
                        cmd.execute(propertySet);
                    }
                    final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_OK);
                    cmd.getCustomHeaders().forEach((final String headerName, final String headerValue) -> {
                        result.addHeader(headerName, headerValue);
                    });
                    cmd.getCustomHeaders().clear();
                    return result;
                } else {
                    return new RestMethodResult(HttpServletResponse.SC_NOT_FOUND);
                }
                // return 200 OK
                return new RestMethodResult(HttpServletResponse.SC_OK);
            } catch (InstantiationException iex) {
                throw new SystemException(iex.getMessage());
            } catch (IllegalAccessException iaex) {
                throw new SystemException(iaex.getMessage());
            }
        } else {
            if (taskOrCommandName != null) {
                throw new NotFoundException("No such task or command: " + this.taskOrCommandName);
            } else {
                throw new IllegalPathException("Maintenance resource needs parameter");
            }
        }
    } else {
        throw new NotAllowedException("Use of the maintenance endpoint is restricted to admin users");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Task(org.structr.agent.Task) IllegalPathException(org.structr.rest.exception.IllegalPathException) Tx(org.structr.core.graph.Tx) SystemException(org.structr.rest.exception.SystemException) NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) MaintenanceCommand(org.structr.core.graph.MaintenanceCommand) RestMethodResult(org.structr.rest.RestMethodResult)

Example 2 with Task

use of org.structr.agent.Task in project structr by structr.

the class CronService method run.

@Override
public void run() {
    final Services servicesInstance = Services.getInstance();
    // wait for service layer to be initialized
    while (!servicesInstance.isInitialized()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException iex) {
        }
    }
    // sleep 5 seconds more
    try {
        Thread.sleep(5000);
    } catch (InterruptedException iex) {
    }
    while (doRun) {
        // sleep for some time
        try {
            Thread.sleep(GRANULARITY_UNIT.toMillis(GRANULARITY));
        } catch (InterruptedException iex) {
        }
        for (CronEntry entry : cronEntries) {
            if (entry.getDelayToNextExecutionInMillis() < GRANULARITY_UNIT.toMillis(GRANULARITY)) {
                final String taskClassName = entry.getName();
                final Class taskClass = instantiate(taskClassName);
                try {
                    if (taskClass != null) {
                        Task task = (Task) taskClass.newInstance();
                        logger.debug("Starting task {}", taskClassName);
                        StructrApp.getInstance().processTasks(task);
                    } else {
                        try (final Tx tx = StructrApp.getInstance().tx()) {
                            // check for schema method with the given name
                            Actions.callAsSuperUser(taskClassName, Collections.EMPTY_MAP);
                            tx.success();
                        }
                    }
                } catch (Throwable t) {
                    logger.warn("Exception while executing cron task {}: {}", taskClassName, t.getMessage());
                }
            }
        }
    }
}
Also used : StructrServices(org.structr.api.service.StructrServices) Services(org.structr.core.Services) Task(org.structr.agent.Task) Tx(org.structr.core.graph.Tx)

Aggregations

Task (org.structr.agent.Task)2 Tx (org.structr.core.graph.Tx)2 StructrServices (org.structr.api.service.StructrServices)1 Services (org.structr.core.Services)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 MaintenanceCommand (org.structr.core.graph.MaintenanceCommand)1 RestMethodResult (org.structr.rest.RestMethodResult)1 IllegalPathException (org.structr.rest.exception.IllegalPathException)1 NotAllowedException (org.structr.rest.exception.NotAllowedException)1 NotFoundException (org.structr.rest.exception.NotFoundException)1 SystemException (org.structr.rest.exception.SystemException)1