|
| 1 | +package io.sentry.quartz; |
| 2 | + |
| 3 | +import io.sentry.BuildConfig; |
| 4 | +import io.sentry.CheckIn; |
| 5 | +import io.sentry.CheckInStatus; |
| 6 | +import io.sentry.HubAdapter; |
| 7 | +import io.sentry.IHub; |
| 8 | +import io.sentry.MonitorConfig; |
| 9 | +import io.sentry.MonitorSchedule; |
| 10 | +import io.sentry.MonitorScheduleUnit; |
| 11 | +import io.sentry.Sentry; |
| 12 | +import io.sentry.SentryIntegrationPackageStorage; |
| 13 | +import io.sentry.SentryLevel; |
| 14 | +import io.sentry.protocol.SentryId; |
| 15 | +import io.sentry.util.Objects; |
| 16 | +import java.util.List; |
| 17 | +import java.util.TimeZone; |
| 18 | +import org.jetbrains.annotations.ApiStatus; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | +import org.quartz.CalendarIntervalTrigger; |
| 22 | +import org.quartz.CronTrigger; |
| 23 | +import org.quartz.DateBuilder; |
| 24 | +import org.quartz.Job; |
| 25 | +import org.quartz.JobDetail; |
| 26 | +import org.quartz.JobExecutionContext; |
| 27 | +import org.quartz.JobExecutionException; |
| 28 | +import org.quartz.JobKey; |
| 29 | +import org.quartz.JobListener; |
| 30 | +import org.quartz.SimpleTrigger; |
| 31 | +import org.quartz.Trigger; |
| 32 | + |
| 33 | +@ApiStatus.Experimental |
| 34 | +public final class SentryJobListener implements JobListener { |
| 35 | + |
| 36 | + public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; |
| 37 | + public static final String SENTRY_CHECK_IN_SLUG_KEY = "sentry-checkin-slug"; |
| 38 | + |
| 39 | + private final @NotNull IHub hub; |
| 40 | + |
| 41 | + public SentryJobListener() { |
| 42 | + this(HubAdapter.getInstance()); |
| 43 | + } |
| 44 | + |
| 45 | + public SentryJobListener(final @NotNull IHub hub) { |
| 46 | + this.hub = Objects.requireNonNull(hub, "hub is required"); |
| 47 | + SentryIntegrationPackageStorage.getInstance().addIntegration("Quartz"); |
| 48 | + SentryIntegrationPackageStorage.getInstance() |
| 49 | + .addPackage("maven:io.sentry:sentry-quartz", BuildConfig.VERSION_NAME); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getName() { |
| 54 | + return "sentry-job-listener"; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void jobToBeExecuted(JobExecutionContext context) { |
| 59 | + try { |
| 60 | + if (isDisabled()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + final @NotNull String slug = getSlug(context.getJobDetail()); |
| 64 | + final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); |
| 65 | + |
| 66 | + final @Nullable MonitorConfig monitorConfig = extractMonitorConfig(context); |
| 67 | + if (monitorConfig != null) { |
| 68 | + checkIn.setMonitorConfig(monitorConfig); |
| 69 | + } |
| 70 | + |
| 71 | + final @NotNull SentryId checkInId = Sentry.captureCheckIn(checkIn); |
| 72 | + context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); |
| 73 | + context.put(SENTRY_CHECK_IN_SLUG_KEY, slug); |
| 74 | + } catch (Throwable t) { |
| 75 | + Sentry.getCurrentHub() |
| 76 | + .getOptions() |
| 77 | + .getLogger() |
| 78 | + .log(SentryLevel.ERROR, "Unable to capture check-in in jobToBeExecuted.", t); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private @NotNull String getSlug(final @Nullable JobDetail jobDetail) { |
| 83 | + if (jobDetail == null) { |
| 84 | + return "fallback"; |
| 85 | + } |
| 86 | + final @NotNull StringBuilder slugBuilder = new StringBuilder(); |
| 87 | + |
| 88 | + final @Nullable JobKey key = jobDetail.getKey(); |
| 89 | + if (key != null) { |
| 90 | + slugBuilder.append(key.getName()); |
| 91 | + slugBuilder.append("__"); |
| 92 | + } |
| 93 | + |
| 94 | + final @Nullable Class<? extends Job> jobClass = jobDetail.getJobClass(); |
| 95 | + if (jobClass != null) { |
| 96 | + slugBuilder.append(jobClass.getCanonicalName()); |
| 97 | + } |
| 98 | + |
| 99 | + return slugBuilder.toString(); |
| 100 | + } |
| 101 | + |
| 102 | + private @Nullable MonitorConfig extractMonitorConfig(final @NotNull JobExecutionContext context) { |
| 103 | + @Nullable MonitorSchedule schedule = null; |
| 104 | + @Nullable String cronExpression = null; |
| 105 | + @Nullable TimeZone timeZone = TimeZone.getDefault(); |
| 106 | + @Nullable Integer repeatInterval = null; |
| 107 | + @Nullable MonitorScheduleUnit timeUnit = null; |
| 108 | + |
| 109 | + try { |
| 110 | + List<? extends Trigger> triggersOfJob = |
| 111 | + context.getScheduler().getTriggersOfJob(context.getTrigger().getJobKey()); |
| 112 | + for (Trigger trigger : triggersOfJob) { |
| 113 | + if (trigger instanceof CronTrigger) { |
| 114 | + final CronTrigger cronTrigger = (CronTrigger) trigger; |
| 115 | + cronExpression = cronTrigger.getCronExpression(); |
| 116 | + timeZone = cronTrigger.getTimeZone(); |
| 117 | + } else if (trigger instanceof SimpleTrigger) { |
| 118 | + final SimpleTrigger simpleTrigger = (SimpleTrigger) trigger; |
| 119 | + long tmpRepeatInterval = simpleTrigger.getRepeatInterval(); |
| 120 | + repeatInterval = millisToMinutes(Double.valueOf(tmpRepeatInterval)); |
| 121 | + timeUnit = MonitorScheduleUnit.MINUTE; |
| 122 | + } else if (trigger instanceof CalendarIntervalTrigger) { |
| 123 | + final CalendarIntervalTrigger calendarIntervalTrigger = (CalendarIntervalTrigger) trigger; |
| 124 | + DateBuilder.IntervalUnit repeatIntervalUnit = |
| 125 | + calendarIntervalTrigger.getRepeatIntervalUnit(); |
| 126 | + int tmpRepeatInterval = calendarIntervalTrigger.getRepeatInterval(); |
| 127 | + if (DateBuilder.IntervalUnit.SECOND.equals(repeatIntervalUnit)) { |
| 128 | + repeatInterval = secondsToMinutes(Double.valueOf(tmpRepeatInterval)); |
| 129 | + timeUnit = MonitorScheduleUnit.MINUTE; |
| 130 | + } else if (DateBuilder.IntervalUnit.MILLISECOND.equals(repeatIntervalUnit)) { |
| 131 | + repeatInterval = millisToMinutes(Double.valueOf(tmpRepeatInterval)); |
| 132 | + timeUnit = MonitorScheduleUnit.MINUTE; |
| 133 | + } else { |
| 134 | + repeatInterval = tmpRepeatInterval; |
| 135 | + timeUnit = convertUnit(repeatIntervalUnit); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } catch (Throwable t) { |
| 140 | + Sentry.getCurrentHub() |
| 141 | + .getOptions() |
| 142 | + .getLogger() |
| 143 | + .log(SentryLevel.ERROR, "Unable to extract monitor config for check-in.", t); |
| 144 | + } |
| 145 | + if (cronExpression != null) { |
| 146 | + schedule = MonitorSchedule.crontab(cronExpression); |
| 147 | + } else if (repeatInterval != null && timeUnit != null) { |
| 148 | + schedule = MonitorSchedule.interval(repeatInterval.intValue(), timeUnit); |
| 149 | + } |
| 150 | + |
| 151 | + if (schedule != null) { |
| 152 | + final @Nullable MonitorConfig monitorConfig = new MonitorConfig(schedule); |
| 153 | + if (timeZone != null) { |
| 154 | + monitorConfig.setTimezone(timeZone.getID()); |
| 155 | + } |
| 156 | + return monitorConfig; |
| 157 | + } else { |
| 158 | + return null; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private @Nullable Integer millisToMinutes(final @NotNull Double milis) { |
| 163 | + return Double.valueOf((milis / 1000.0) / 60.0).intValue(); |
| 164 | + } |
| 165 | + |
| 166 | + private @Nullable Integer secondsToMinutes(final @NotNull Double seconds) { |
| 167 | + return Double.valueOf(seconds / 60.0).intValue(); |
| 168 | + } |
| 169 | + |
| 170 | + private @Nullable MonitorScheduleUnit convertUnit( |
| 171 | + final @Nullable DateBuilder.IntervalUnit intervalUnit) { |
| 172 | + if (intervalUnit == null) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + if (DateBuilder.IntervalUnit.MINUTE.equals(intervalUnit)) { |
| 177 | + return MonitorScheduleUnit.MINUTE; |
| 178 | + } else if (DateBuilder.IntervalUnit.HOUR.equals(intervalUnit)) { |
| 179 | + return MonitorScheduleUnit.HOUR; |
| 180 | + } else if (DateBuilder.IntervalUnit.DAY.equals(intervalUnit)) { |
| 181 | + return MonitorScheduleUnit.DAY; |
| 182 | + } else if (DateBuilder.IntervalUnit.WEEK.equals(intervalUnit)) { |
| 183 | + return MonitorScheduleUnit.WEEK; |
| 184 | + } else if (DateBuilder.IntervalUnit.MONTH.equals(intervalUnit)) { |
| 185 | + return MonitorScheduleUnit.MONTH; |
| 186 | + } else if (DateBuilder.IntervalUnit.YEAR.equals(intervalUnit)) { |
| 187 | + return MonitorScheduleUnit.YEAR; |
| 188 | + } |
| 189 | + |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void jobExecutionVetoed(JobExecutionContext context) { |
| 195 | + // do nothing |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { |
| 200 | + try { |
| 201 | + if (isDisabled()) { |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + final @Nullable Object checkInIdObjectFromContext = context.get(SENTRY_CHECK_IN_ID_KEY); |
| 206 | + final @Nullable Object slugObjectFromContext = context.get(SENTRY_CHECK_IN_SLUG_KEY); |
| 207 | + final @NotNull SentryId checkInId = |
| 208 | + checkInIdObjectFromContext == null |
| 209 | + ? new SentryId() |
| 210 | + : (SentryId) checkInIdObjectFromContext; |
| 211 | + final @Nullable String slug = |
| 212 | + slugObjectFromContext == null ? null : (String) slugObjectFromContext; |
| 213 | + if (slug != null) { |
| 214 | + final boolean isFailed = jobException != null; |
| 215 | + final @NotNull CheckInStatus status = isFailed ? CheckInStatus.ERROR : CheckInStatus.OK; |
| 216 | + Sentry.captureCheckIn(new CheckIn(checkInId, slug, status)); |
| 217 | + } |
| 218 | + } catch (Throwable t) { |
| 219 | + Sentry.getCurrentHub() |
| 220 | + .getOptions() |
| 221 | + .getLogger() |
| 222 | + .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private boolean isDisabled() { |
| 227 | + return !hub.getOptions().isEnableAutomaticCheckIns(); |
| 228 | + } |
| 229 | +} |
0 commit comments