package top.quantic.sentry.service.util;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;

import static top.quantic.sentry.service.util.DateUtil.humanize;
import static top.quantic.sentry.service.util.DateUtil.instantToDate;

public class LoggingTriggerListener implements TriggerListener {

    private static final Logger log = LoggerFactory.getLogger(LoggingTriggerListener.class);

    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        log.info("[{}] Firing job {}.{}", trigger.getKey().getName(),
            context.getJobDetail().getKey().getGroup(), context.getJobDetail().getKey().getName());
    }

    public void triggerMisfired(Trigger trigger) {
        log.info("[{}] Misfiring job {}.{} - Should have fired at {}", trigger.getKey().getName(),
            trigger.getJobKey().getGroup(), trigger.getJobKey().getName(),
            instantToDate(trigger.getNextFireTime().toInstant()));
    }

    public void triggerComplete(Trigger trigger, JobExecutionContext context,
                                Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        log.info("[{}] Completed job {}.{} in {}{}",
            trigger.getKey().getName(), context.getJobDetail().getKey().getGroup(),
            context.getJobDetail().getKey().getName(), humanize(Duration.ofMillis(context.getJobRunTime())),
            (context.getResult() != null ? " with result: " + context.getResult() : ""));
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        return false;
    }
}

@@ -281,86 +281,9 @@
 til.
-humanize;%0Aimport static top.quantic.sentry.service.util.DateUtil.instantToDate
+*
 ;%0A%0Ap
@@ -644,53 +644,35 @@
 %7B%7D%5D 
-Firing job %7B%7D.%7B%7D%22, trigger.getKey().getName()
+Trigger '%7B%7D' is firing job%22
 ,%0A  
@@ -716,43 +716,17 @@
 ey()
-.getGroup(), context.getJobDetail()
+, trigger
 .get
@@ -828,27 +828,33 @@
 %7B%7D%5D 
-M
+Trigger '%7B%7D' m
 isfir
-ing
+ed
  job
- %7B%7D.%7B%7D
  - S
@@ -879,37 +879,14 @@
 t %7B%7D
-%22, trigger.getKey().getName()
+ (%7B%7D)%22
 ,%0A  
@@ -914,27 +914,16 @@
 JobKey()
-.getGroup()
 , trigge
@@ -923,27 +923,24 @@
  trigger.get
-Job
 Key().getNam
@@ -965,19 +965,88 @@
 nstantTo
-Dat
+System(trigger.getNextFireTime().toInstant()),%0A            formatRelativ
 e(trigge
@@ -1284,17 +1284,30 @@
 o(%22%5B%7B%7D%5D 
-C
+Trigger '%7B%7D' c
 ompleted
@@ -1314,14 +1314,8 @@
  job
- %7B%7D.%7B%7D
  in 
@@ -1336,36 +1336,8 @@
     
- trigger.getKey().getName(),
  con
@@ -1368,55 +1368,17 @@
 ey()
-.getGroup(),%0A            context.getJobDetail()
+, trigger
 .get
@@ -1393,16 +1393,28 @@
 tName(),
+%0A           
  humaniz

Reformat messages from trigger listener