Skip to content

Commit

Permalink
fix rule execution and pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed May 7, 2024
1 parent 22c2845 commit 2d2462b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/cryostat/rules/RuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ private void scheduleArchival(Rule rule, Target target, ActiveRecording recordin
}

Map<String, Object> data = jobDetail.getJobDataMap();
data.put("rule", rule);
data.put("target", target);
data.put("recording", recording);
data.put("rule", rule.id);
data.put("target", target.id);
data.put("recording", recording.id);

Trigger trigger =
TriggerBuilder.newTrigger()
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/io/cryostat/rules/ScheduledArchiveJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import io.cryostat.recordings.RecordingHelper;
import io.cryostat.targets.Target;

import io.quarkus.narayana.jta.QuarkusTransaction;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

class ScheduledArchiveJob implements Job {

Expand All @@ -46,25 +47,27 @@ class ScheduledArchiveJob implements Job {
String archiveBucket;

@Override
public void execute(JobExecutionContext ctx) {
var rule = (Rule) ctx.getJobDetail().getJobDataMap().get("rule");
var target = (Target) ctx.getJobDetail().getJobDataMap().get("target");
var recording = (ActiveRecording) ctx.getJobDetail().getJobDataMap().get("recording");
@Transactional
public void execute(JobExecutionContext ctx) throws JobExecutionException {
long ruleId = (long) ctx.getJobDetail().getJobDataMap().get("rule");
Rule rule = Rule.find("id", ruleId).singleResult();
long targetId = (long) ctx.getJobDetail().getJobDataMap().get("target");
Target target = Target.find("id", id).singleResult();
long recordingId = (long) ctx.getJobDetail().getJobDataMap().get("recording");
ActiveRecording recording = ActiveRecording.find("id", recordingId).singleResult();

Queue<String> previousRecordings = new ArrayDeque<>(rule.preservedArchives);

initPreviousRecordings(target, rule, previousRecordings);

while (previousRecordings.size() >= rule.preservedArchives) {
pruneArchive(target, previousRecordings, previousRecordings.remove());
}
previousRecordings.add(
QuarkusTransaction.joiningExisting()
.call(
() ->
recordingHelper
.archiveRecording(recording, null, null)
.name()));

try {
previousRecordings.add(recordingHelper.archiveRecording(recording, null, null).name());
} catch (Exception e) {
throw new JobExecutionException(e);
}
}

void initPreviousRecordings(Target target, Rule rule, Queue<String> previousRecordings) {
Expand Down

0 comments on commit 2d2462b

Please sign in to comment.