55import io .sentry .CheckInStatus ;
66import io .sentry .HubAdapter ;
77import io .sentry .IHub ;
8- import io .sentry .MonitorConfig ;
9- import io .sentry .MonitorSchedule ;
10- import io .sentry .MonitorScheduleUnit ;
118import io .sentry .Sentry ;
129import io .sentry .SentryIntegrationPackageStorage ;
1310import io .sentry .SentryLevel ;
1411import io .sentry .protocol .SentryId ;
1512import io .sentry .util .Objects ;
16- import java .util .List ;
17- import java .util .TimeZone ;
1813import org .jetbrains .annotations .ApiStatus ;
1914import org .jetbrains .annotations .NotNull ;
2015import 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 ;
16+ import org .quartz .JobDataMap ;
2617import org .quartz .JobExecutionContext ;
2718import org .quartz .JobExecutionException ;
28- import org .quartz .JobKey ;
2919import org .quartz .JobListener ;
30- import org .quartz .SimpleTrigger ;
3120import org .quartz .Trigger ;
3221
3322@ ApiStatus .Experimental
@@ -55,19 +44,17 @@ public String getName() {
5544 }
5645
5746 @ Override
58- public void jobToBeExecuted (JobExecutionContext context ) {
47+ public void jobToBeExecuted (final @ NotNull JobExecutionContext context ) {
5948 try {
6049 if (isDisabled ()) {
6150 return ;
6251 }
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 );
52+ final @ Nullable String maybeSlug = getSlug (context );
53+ if (maybeSlug == null ) {
54+ return ;
6955 }
70-
56+ final @ NotNull String slug = maybeSlug ;
57+ final @ NotNull CheckIn checkIn = new CheckIn (slug , CheckInStatus .IN_PROGRESS );
7158 final @ NotNull SentryId checkInId = Sentry .captureCheckIn (checkIn );
7259 context .put (SENTRY_CHECK_IN_ID_KEY , checkInId );
7360 context .put (SENTRY_CHECK_IN_SLUG_KEY , slug );
@@ -79,112 +66,18 @@ public void jobToBeExecuted(JobExecutionContext context) {
7966 }
8067 }
8168
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 ) {
69+ private @ Nullable String getSlug (final @ NotNull JobExecutionContext context ) {
70+ final Trigger trigger = context .getTrigger ();
71+ if (trigger == null ) {
17372 return null ;
17473 }
17574
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 ;
75+ final @ Nullable JobDataMap jobDataMap = trigger .getJobDataMap ();
76+ if (jobDataMap != null ) {
77+ final @ Nullable Object o = jobDataMap .get (SENTRY_CHECK_IN_SLUG_KEY );
78+ if (o != null ) {
79+ return o .toString ();
80+ }
18881 }
18982
19083 return null ;
0 commit comments