Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes scheduled entries directly executable from overview page #1985

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/sirius/biz/jobs/scheduler/SchedulerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
import sirius.biz.jobs.JobConfigData;
import sirius.biz.jobs.JobFactory;
import sirius.biz.jobs.Jobs;
import sirius.biz.process.Process;
import sirius.biz.process.Processes;
import sirius.biz.web.BasePageHelper;
import sirius.biz.web.BizController;
import sirius.biz.web.TenantAware;
import sirius.db.es.ElasticQuery;
import sirius.db.mixing.BaseEntity;
import sirius.db.mixing.query.QueryField;
import sirius.kernel.commons.Strings;
import sirius.kernel.di.PartCollection;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Parts;
import sirius.kernel.health.Exceptions;
import sirius.kernel.nls.NLS;
import sirius.web.controller.AutocompleteHelper;
import sirius.web.controller.Routed;
Expand All @@ -26,6 +32,8 @@
import sirius.web.security.UserContext;
import sirius.web.security.UserInfo;

import java.time.LocalDateTime;

/**
* Provides a base class to create the management UI for the job scheduler.
* <p>
Expand All @@ -41,6 +49,15 @@ public abstract class SchedulerController<J extends BaseEntity<?> & SchedulerEnt
@Part
private Jobs jobs;

@Part
private Processes processes;

@Parts(SchedulerEntryProvider.class)
private PartCollection<SchedulerEntryProvider<J>> providers;

@Part
private ScheduledEntryExecution entryExecution;

/**
* Returns the entity class being used by this controller.
*
Expand Down Expand Up @@ -103,6 +120,53 @@ public void schedulerEntry(WebContext webContext, String entryId) {
}
}

/**
* Executes the given scheduler entry immediately and redirects to the process detail view.
*
* @param webContext the current request
* @param entryId the id of the entry to display or <tt>new</tt> to create a new one
*/
@Permission(PERMISSION_MANAGE_SCHEDULER)
mkeckmkeck marked this conversation as resolved.
Show resolved Hide resolved
@Routed("/jobs/scheduler/execute-entry/:1")
mkeckmkeck marked this conversation as resolved.
Show resolved Hide resolved
public void executeSchedulerEntry(WebContext webContext, String entryId) {
J entry = findForTenant(getEntryType(), entryId);

if (entry.isNew()) {
schedulerEntries(webContext);
return;
}

executeInBelongingProvider(entry);

webContext.respondWith().redirectToGet("/ps/" + fetchLatestProcess(entry).getIdAsString());
}

private Process fetchLatestProcess(J entry) {
ElasticQuery<Process> query = processes.queryProcessesForCurrentUser();
query.eqIgnoreNull(Process.REFERENCES, entry.getUniqueName());
query.orderDesc(Process.STARTED);
return query.first()
.orElseThrow(() -> Exceptions.createHandled()
.withNLSKey("SchedulerController.noProcessFound")
.handle());
}

private void executeInBelongingProvider(J entry) {
for (SchedulerEntryProvider<J> provider : providers) {
J entryFromProvider = provider.fetchFullInformation(entry);
if (entryBelongsToProvider(entry, entryFromProvider)) {
entryExecution.executeJob(provider, entryFromProvider, LocalDateTime.now());
return;
}
}
}

private static <J extends BaseEntity<?> & SchedulerEntry & TenantAware> boolean entryBelongsToProvider(J entry,
J providersEntry) {
// The belonging provided must have refreshed data, so the object identity is different.
return System.identityHashCode(providersEntry) != System.identityHashCode(entry);
}

protected void loadUser(WebContext webContext, J entry) {
UserInfo user = UserContext.get()
.getUserManager()
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/biz_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ SchedulerEntry.invalidPatternInField = Ungültiger Wert im Feld ${field}: ${msg}
SchedulerEntry.lastExecution = Letzte Ausführung
SchedulerEntry.minute = Minute
SchedulerEntry.month = Monat
SchedulerEntry.executeNow = Jetzt ausführen
SchedulerEntry.numberOfExecutions = Anzahl Ausführungen
SchedulerEntry.performedExecutions = Ausführungen
SchedulerEntry.plural = Geplante Ausführungen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
url="@apply('/ps?reference=%s&reference-label=%s', entry.getUniqueName(), urlEncode(entry.toString()))"
icon="fa-solid fa-list"
labelKey="SchedulerEntry.performedExecutions"/>
<t:dropdownItem
url="@apply('/jobs/scheduler/execute-entry/%s', entry.getIdAsString())"
icon="fa-solid fa-play"
labelKey="SchedulerEntry.executeNow"/>
<t:dropdownDeleteItem
url="@apply('/jobs/scheduler/entry/%s/delete', entry.getIdAsString())"/>
</i:block>
Expand Down