Search in sources :

Example 1 with HandledException

use of sirius.kernel.health.HandledException in project sirius-db by scireum.

the class LowLevelClient method resolveIndexForAlias.

/**
 * Returns the actual index for the given alias
 *
 * @param aliasName the alias the resolve
 * @return the index name to which this alias points or an empty optional if the alias is unknown
 * @throws HandledException if the alias points to more than one index as this pattern is unused by
 *                          out schema evolution tools (which always redirects an index access via an alias).
 */
public Optional<String> resolveIndexForAlias(String aliasName) {
    List<String> indexNames = new ArrayList<>();
    performGet().withCustomErrorHandler(error -> {
        // JSON object, otherwise the error and its status code will be reported as indices...
        if (error.getResponse().getStatusLine().getStatusCode() == 404) {
            return new StringEntity("{}", StandardCharsets.UTF_8);
        } else {
            return null;
        }
    }).execute(API_ALIAS + "/" + aliasName).response().forEach((indexName, info) -> indexNames.add(indexName));
    if (indexNames.isEmpty()) {
        return Optional.empty();
    }
    if (indexNames.size() > 1) {
        throw Exceptions.handle().to(Elastic.LOG).withSystemErrorMessage("The alias %s points to more than one index: %s", aliasName, Strings.join(indexNames, ", ")).handle();
    }
    return Optional.of(indexNames.get(0));
}
Also used : ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) OptimisticLockException(sirius.db.mixing.OptimisticLockException) Strings(sirius.kernel.commons.Strings) HttpEntity(org.apache.http.HttpEntity) Exceptions(sirius.kernel.health.Exceptions) HandledException(sirius.kernel.health.HandledException) StringEntity(org.apache.http.entity.StringEntity) IOException(java.io.IOException) Request(org.elasticsearch.client.Request) Collectors(java.util.stream.Collectors) Explain(sirius.kernel.commons.Explain) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) Response(org.elasticsearch.client.Response) Optional(java.util.Optional) JSONObject(com.alibaba.fastjson.JSONObject) Nullable(javax.annotation.Nullable) StringEntity(org.apache.http.entity.StringEntity) ArrayList(java.util.ArrayList)

Example 2 with HandledException

use of sirius.kernel.health.HandledException in project sirius-biz by scireum.

the class EventRecorder method process.

/**
 * Processes the queued events by creating a {@link BatchContext} and batch-inserting all events.
 *
 * @return the number of inserted events
 */
protected int process() {
    lastProcessed = LocalDateTime.now();
    int processedEvents = 0;
    try (BatchContext ctx = new BatchContext(() -> "Process recorded events.", Duration.ofMinutes(1))) {
        Map<Class<? extends Event>, InsertQuery<Event>> queries = new HashMap<>();
        Event nextEvent = fetchBufferedEvent();
        while (nextEvent != null) {
            processEvent(ctx, queries, nextEvent);
            if (++processedEvents >= MAX_EVENTS_PER_PROCESS) {
                return processedEvents;
            }
            nextEvent = fetchBufferedEvent();
        }
    } catch (HandledException e) {
        Exceptions.ignore(e);
    } catch (Exception e) {
        Exceptions.handle(Log.BACKGROUND, e);
    }
    return processedEvents;
}
Also used : HandledException(sirius.kernel.health.HandledException) InsertQuery(sirius.db.jdbc.batch.InsertQuery) HashMap(java.util.HashMap) BatchContext(sirius.db.jdbc.batch.BatchContext) HandledException(sirius.kernel.health.HandledException)

Example 3 with HandledException

use of sirius.kernel.health.HandledException in project sirius-biz by scireum.

the class JobSchedulerLoop method executeJobInProcess.

private <J extends SchedulerEntry> void executeJobInProcess(SchedulerEntryProvider<J> provider, J entry, LocalDateTime now, ProcessContext ctx) {
    if (ctx.isDebugging()) {
        ctx.debug(ProcessLog.info().withFormattedMessage("Starting scheduled job %s (%s) for user %s.", entry, entry.getJobConfigData().getJobName(), UserContext.getCurrentUser().getUserName()));
    }
    try {
        String processId = entry.getJobConfigData().getJobFactory().startInBackground(entry.getJobConfigData()::fetchParameter);
        if (processId != null) {
            processes.log(processId, ProcessLog.info().withNLSKey("JobSchedulerLoop.scheduledExecutionInfo").withContext("entry", entry.toString()));
            processes.addLink(processId, new ProcessLink().withLabel("$JobSchedulerLoop.jobLink").withUri("/jobs/scheduler/entry/" + entry.getIdAsString()));
            processes.addReference(processId, entry.getUniqueName());
        }
        provider.markExecuted(entry, now);
    } catch (HandledException exception) {
        ctx.log(ProcessLog.error().withFormattedMessage("Failed to start scheduled job %s (%s) for user %s: %s", entry, entry.getJobConfigData().getJobName(), UserContext.getCurrentUser().getUserName(), exception.getMessage()));
    }
}
Also used : HandledException(sirius.kernel.health.HandledException) ProcessLink(sirius.biz.process.ProcessLink)

Example 4 with HandledException

use of sirius.kernel.health.HandledException in project sirius-biz by scireum.

the class ProcessEnvironment method handle.

@Override
public HandledException handle(Exception e) {
    HandledException handledException = Exceptions.handle(Log.BACKGROUND, e);
    log(ProcessLog.error().withHandledException(handledException));
    return handledException;
}
Also used : HandledException(sirius.kernel.health.HandledException)

Example 5 with HandledException

use of sirius.kernel.health.HandledException in project sirius-biz by scireum.

the class Scripting method handleExecTask.

private void handleExecTask(JSONObject event) {
    String nodeName = event.getString(TASK_NODE);
    String jobNumber = event.getString(TASK_JOB);
    if (!ALL_NODES.equals(nodeName) && !Strings.areEqual(CallContext.getNodeName(), nodeName)) {
        return;
    }
    Watch watch = Watch.start();
    logInTranscript(jobNumber, Strings.apply("Starting execution on %s (Thread Id: %s / Thread Name: %s)", CallContext.getNodeName(), Thread.currentThread().getId(), Thread.currentThread().getName()));
    try {
        Callable callable = compileScript(event);
        TaskContext.get().setAdapter(new JobTaskContextAdapter(this, jobNumber));
        callable.call(new SimpleEnvironment());
    } catch (CompileException | ScriptingException | HandledException e) {
        logInTranscript(jobNumber, e.getMessage());
    } catch (Exception e) {
        logInTranscript(jobNumber, Exceptions.handle(Pasta.LOG, e).getMessage());
    }
    logInTranscript(jobNumber, Strings.apply("Execution completed (%s)", watch.duration()));
}
Also used : HandledException(sirius.kernel.health.HandledException) SimpleEnvironment(sirius.pasta.noodle.SimpleEnvironment) Watch(sirius.kernel.commons.Watch) CompileException(sirius.pasta.noodle.compiler.CompileException) Callable(sirius.pasta.noodle.Callable) HandledException(sirius.kernel.health.HandledException) ScriptingException(sirius.pasta.noodle.ScriptingException) CompileException(sirius.pasta.noodle.compiler.CompileException) ScriptingException(sirius.pasta.noodle.ScriptingException)

Aggregations

HandledException (sirius.kernel.health.HandledException)8 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Request (org.elasticsearch.client.Request)2 Response (org.elasticsearch.client.Response)2 ResponseException (org.elasticsearch.client.ResponseException)2 OptimisticLockException (sirius.db.mixing.OptimisticLockException)2 Watch (sirius.kernel.commons.Watch)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 StandardCharsets (java.nio.charset.StandardCharsets)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1