|
| 1 | +package org.dashjoin.service; |
| 2 | + |
| 3 | +import java.security.Principal; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.logging.Level; |
| 7 | +import org.dashjoin.function.FunctionService; |
| 8 | +import org.dashjoin.model.Table; |
| 9 | +import io.quarkus.runtime.Startup; |
| 10 | +import io.quarkus.runtime.StartupEvent; |
| 11 | +import io.quarkus.scheduler.Scheduled; |
| 12 | +import io.quarkus.scheduler.Scheduler; |
| 13 | +import io.quarkus.scheduler.Trigger; |
| 14 | +import jakarta.enterprise.context.ApplicationScoped; |
| 15 | +import jakarta.enterprise.event.Observes; |
| 16 | +import jakarta.inject.Inject; |
| 17 | +import jakarta.ws.rs.core.SecurityContext; |
| 18 | +import lombok.extern.java.Log; |
| 19 | + |
| 20 | +@Log |
| 21 | +@Startup |
| 22 | +@ApplicationScoped |
| 23 | +public class Cron { |
| 24 | + |
| 25 | + @Inject |
| 26 | + Scheduler scheduler; |
| 27 | + |
| 28 | + @Inject |
| 29 | + Services services; |
| 30 | + |
| 31 | + @Inject |
| 32 | + FunctionService function; |
| 33 | + |
| 34 | + /** |
| 35 | + * startup bean must not throw exceptions |
| 36 | + */ |
| 37 | + public void onStart(@Observes StartupEvent ev) { |
| 38 | + try { |
| 39 | + resetSchedule(); |
| 40 | + } catch (Exception e) { |
| 41 | + log.log(Level.SEVERE, "Error while starting scheduler", e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public Map<String, Object> resetSchedule() throws Exception { |
| 46 | + |
| 47 | + // map of job to error / next runtime |
| 48 | + Map<String, Object> res = new HashMap<>(); |
| 49 | + |
| 50 | + // unschedule all jobs |
| 51 | + for (Trigger t : scheduler.getScheduledJobs()) |
| 52 | + scheduler.unscheduleJob(t.getId()); |
| 53 | + |
| 54 | + // jobs run as "scheduler" |
| 55 | + SecurityContext sc = new SecurityContext() { |
| 56 | + @Override |
| 57 | + public boolean isUserInRole(String role) { |
| 58 | + return role.equals("scheduler"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isSecure() { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Principal getUserPrincipal() { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String getAuthenticationScheme() { |
| 73 | + return null; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + // schedule any function with "cron" or "every" |
| 78 | + for (Map<String, Object> f : services.getConfig().getConfigDatabase() |
| 79 | + .all(Table.ofName("dj-function"), null, null, null, false, null)) { |
| 80 | + String id = (String) f.get("ID"); |
| 81 | + String cron = (String) f.get("cron"); |
| 82 | + String every = (String) f.get("every"); |
| 83 | + if (cron != null) { |
| 84 | + try { |
| 85 | + log.info("scheduling " + id + " " + cron); |
| 86 | + Trigger t = scheduler.newJob((String) id).setCron(cron).setTask(ctx -> { |
| 87 | + try { |
| 88 | + log.info("running cron scheduled " + id); |
| 89 | + function.call(sc, ctx.getTrigger().getId(), null); |
| 90 | + log.info(ctx.getTrigger().getId() + " complete"); |
| 91 | + } catch (Exception e) { |
| 92 | + log.log(Level.SEVERE, "error running scheduled job " + id, e); |
| 93 | + } |
| 94 | + }).setConcurrentExecution(Scheduled.ConcurrentExecution.SKIP).schedule(); |
| 95 | + res.put(t.getId(), "" + t.getNextFireTime()); |
| 96 | + } catch (Exception cronEx) { |
| 97 | + log.warning("cron syntax error: " + cronEx); |
| 98 | + res.put(id, cronEx.toString()); |
| 99 | + } |
| 100 | + } |
| 101 | + if (every != null) { |
| 102 | + try { |
| 103 | + log.info("scheduling " + id + " " + every); |
| 104 | + Trigger t = scheduler.newJob((String) id).setInterval(every).setTask(ctx -> { |
| 105 | + try { |
| 106 | + log.info("running interval scheduled " + id); |
| 107 | + function.call(sc, ctx.getTrigger().getId(), null); |
| 108 | + log.info(ctx.getTrigger().getId() + " complete"); |
| 109 | + } catch (Exception e) { |
| 110 | + log.log(Level.SEVERE, "error running scheduled job " + id, e); |
| 111 | + } |
| 112 | + }).setConcurrentExecution(Scheduled.ConcurrentExecution.SKIP).schedule(); |
| 113 | + res.put(t.getId(), "" + t.getNextFireTime()); |
| 114 | + } catch (Exception cronEx) { |
| 115 | + log.warning("cron syntax error: " + cronEx); |
| 116 | + res.put(id, cronEx.toString()); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return res; |
| 121 | + } |
| 122 | +} |
0 commit comments