|
| 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.SentryIntegrationPackageStorage; |
| 9 | +import io.sentry.SentryLevel; |
| 10 | +import io.sentry.protocol.SentryId; |
| 11 | +import io.sentry.util.Objects; |
| 12 | +import io.sentry.util.TracingUtils; |
| 13 | +import org.jetbrains.annotations.ApiStatus; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | +import org.quartz.JobDataMap; |
| 17 | +import org.quartz.JobExecutionContext; |
| 18 | +import org.quartz.JobExecutionException; |
| 19 | +import org.quartz.JobListener; |
| 20 | + |
| 21 | +@ApiStatus.Experimental |
| 22 | +public final class SentryJobListener implements JobListener { |
| 23 | + |
| 24 | + public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id"; |
| 25 | + public static final String SENTRY_SLUG_KEY = "sentry-slug"; |
| 26 | + |
| 27 | + private final @NotNull IHub hub; |
| 28 | + |
| 29 | + public SentryJobListener() { |
| 30 | + this(HubAdapter.getInstance()); |
| 31 | + } |
| 32 | + |
| 33 | + public SentryJobListener(final @NotNull IHub hub) { |
| 34 | + this.hub = Objects.requireNonNull(hub, "hub is required"); |
| 35 | + SentryIntegrationPackageStorage.getInstance().addIntegration("Quartz"); |
| 36 | + SentryIntegrationPackageStorage.getInstance() |
| 37 | + .addPackage("maven:io.sentry:sentry-quartz", BuildConfig.VERSION_NAME); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getName() { |
| 42 | + return "sentry-job-listener"; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void jobToBeExecuted(final @NotNull JobExecutionContext context) { |
| 47 | + try { |
| 48 | + final @Nullable String maybeSlug = getSlug(context); |
| 49 | + if (maybeSlug == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + hub.pushScope(); |
| 53 | + TracingUtils.startNewTrace(hub); |
| 54 | + final @NotNull String slug = maybeSlug; |
| 55 | + final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); |
| 56 | + final @NotNull SentryId checkInId = hub.captureCheckIn(checkIn); |
| 57 | + context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); |
| 58 | + context.put(SENTRY_SLUG_KEY, slug); |
| 59 | + } catch (Throwable t) { |
| 60 | + hub.getOptions() |
| 61 | + .getLogger() |
| 62 | + .log(SentryLevel.ERROR, "Unable to capture check-in in jobToBeExecuted.", t); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private @Nullable String getSlug(final @NotNull JobExecutionContext context) { |
| 67 | + final @Nullable JobDataMap jobDataMap = context.getMergedJobDataMap(); |
| 68 | + if (jobDataMap != null) { |
| 69 | + final @Nullable Object o = jobDataMap.get(SENTRY_SLUG_KEY); |
| 70 | + if (o != null) { |
| 71 | + return o.toString(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void jobExecutionVetoed(JobExecutionContext context) { |
| 80 | + // do nothing |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { |
| 85 | + try { |
| 86 | + final @Nullable Object checkInIdObjectFromContext = context.get(SENTRY_CHECK_IN_ID_KEY); |
| 87 | + final @Nullable Object slugObjectFromContext = context.get(SENTRY_SLUG_KEY); |
| 88 | + final @NotNull SentryId checkInId = |
| 89 | + checkInIdObjectFromContext == null |
| 90 | + ? new SentryId() |
| 91 | + : (SentryId) checkInIdObjectFromContext; |
| 92 | + final @Nullable String slug = |
| 93 | + slugObjectFromContext == null ? null : (String) slugObjectFromContext; |
| 94 | + if (slug != null) { |
| 95 | + final boolean isFailed = jobException != null; |
| 96 | + final @NotNull CheckInStatus status = isFailed ? CheckInStatus.ERROR : CheckInStatus.OK; |
| 97 | + hub.captureCheckIn(new CheckIn(checkInId, slug, status)); |
| 98 | + } |
| 99 | + } catch (Throwable t) { |
| 100 | + hub.getOptions() |
| 101 | + .getLogger() |
| 102 | + .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); |
| 103 | + } finally { |
| 104 | + hub.popScope(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments