Skip to content

Commit

Permalink
Merge pull request #2022 from scireum/feature/ymo/SIRI-990-jobs
Browse files Browse the repository at this point in the history
BREAKING: change the way saving processTypes.
  • Loading branch information
ymo-sci authored Jul 22, 2024
2 parents 7d377e3 + 963de29 commit 11354a3
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
148 changes: 148 additions & 0 deletions src/main/java/sirius/biz/jobs/FixProcessTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/

package sirius.biz.jobs;

import sirius.biz.jobs.batch.SimpleBatchProcessJobFactory;
import sirius.biz.jobs.params.BooleanParameter;
import sirius.biz.jobs.params.Parameter;
import sirius.biz.process.Process;
import sirius.biz.process.ProcessContext;
import sirius.biz.process.Processes;
import sirius.biz.process.logs.ProcessLog;
import sirius.biz.tenants.TenantUserManager;
import sirius.db.es.Elastic;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Watch;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.PriorityParts;
import sirius.kernel.di.std.Register;
import sirius.kernel.nls.NLS;
import sirius.web.security.Permission;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
* Provides a job which fixes process types to now used getName() instead of the NLS-key as processType.
*/
@Register(framework = Processes.FRAMEWORK_PROCESSES)
@Permission(TenantUserManager.PERMISSION_SYSTEM_ADMINISTRATOR)
public class FixProcessTypes extends SimpleBatchProcessJobFactory {

@PriorityParts(JobFactory.class)
private List<JobFactory> factories;

@Part
private Elastic elastic;

private static final Parameter<Boolean> SIMULATION =
new BooleanParameter("simulation", "Simulation").withDescription(
"Simulation mode will only output which keys are missing and how many processes would have their type changed.")
.withDefaultTrue()
.build();

@Override
protected void collectParameters(Consumer<Parameter<?>> parameterCollector) {
parameterCollector.accept(SIMULATION);
}

@Override
protected void execute(ProcessContext process) throws Exception {
boolean simulation = process.require(SIMULATION);

Map<String, String> jobTypesAndKeys = factories.stream()
.filter(JobFactory::canStartInBackground)
.distinct()
.collect(HashMap::new,
(map, job) -> map.put(job.getName(),
job.getClass().getSimpleName()
+ ".label"),
HashMap::putAll);

process.log(ProcessLog.info().withMessage(Strings.apply("Found %s jobs", jobTypesAndKeys.size())));
jobTypesAndKeys.forEach((type, nlsKey) -> {
AtomicInteger counter = new AtomicInteger();
if (!NLS.exists(nlsKey, null)) {
process.log(ProcessLog.warn()
.withMessage(Strings.apply("No NLS key found for type %s (%s)", type, nlsKey)));
}

elastic.select(Process.class)
.eq(Process.PROCESS_TYPE, nlsKey)
.streamBlockwise()
.forEach(executedProcess -> {
Watch watch = Watch.start();
if (!simulation) {
try {
executedProcess.setProcessType(type);
elastic.update(executedProcess);
} catch (Exception e) {
process.log(ProcessLog.error()
.withMessage(Strings.apply(
"Failed to update processType on Process with id %s: %s",
executedProcess.getId(),
e.getMessage())));
}
}
process.addTiming("processes", watch.elapsedMillis());
counter.getAndIncrement();
});

if (counter.get() > 0) {
if (simulation) {
process.log(ProcessLog.info()
.withMessage(Strings.apply(
"Would update the processType on %s Processes from '%s' to '%s'",
counter.get(),
nlsKey,
type)));
} else {
process.log(ProcessLog.success()
.withMessage(Strings.apply(
"Updated the processType on %s Processes from '%s' to '%s'",
counter.get(),
nlsKey,
type)));
}
}
});
}

@Override
protected String createProcessTitle(Map<String, String> context) {
return "Fix process types";
}

@Nonnull
@Override
public String getName() {
return "fix-process-types";
}

@Nullable
@Override
public String getDescription() {
return "Fixes process-types to now use getName() instead of the NLS-key as processType.";
}

@Override
public String getLabel() {
return "Fix process types";
}

@Override
public String getCategory() {
return StandardCategories.SYSTEM_ADMINISTRATION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Map<String, String> buildAndVerifyContext(Function<String, Value> paramet
* @return the id of the newly created process
*/
protected String startWithContext(Map<String, String> context) {
String processId = processes.createProcess(getClass().getSimpleName() + ".label",
String processId = processes.createProcess(getName(),
createProcessTitle(context),
getIcon(),
getCurrentOrRootUser(),
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/sirius/biz/process/ProcessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import sirius.biz.cluster.work.DistributedTasks;
import sirius.biz.jobs.JobFactory;
import sirius.biz.process.logs.ProcessLog;
import sirius.biz.process.logs.ProcessLogHandler;
import sirius.biz.process.logs.ProcessLogState;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void processes(WebContext webContext) {
Process.REFERENCES,
NLS.get("ProcessController.reference"),
null);
pageHelper.addTermAggregation(Process.PROCESS_TYPE, value -> NLS.getIfExists(value, null).orElse(null));
pageHelper.addTermAggregation(Process.PROCESS_TYPE, this::findJobLabel);
pageHelper.addTimeAggregation(Process.STARTED,
false,
DateRange.LAST_FIVE_MINUTES,
Expand All @@ -121,6 +122,11 @@ public void processes(WebContext webContext) {
webContext.respondWith().template("/templates/biz/process/processes.html.pasta", pageHelper.asPage());
}

private String findJobLabel(String value) {
JobFactory result = context.getPart(value, JobFactory.class);
return result != null ? result.getLabel() : null;
}

private Process findAccessibleProcess(String processId) {
Process process = processes.fetchProcessForUser(processId).orElse(null);
assertNotNull(process);
Expand Down

0 comments on commit 11354a3

Please sign in to comment.