|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.agentscope.extensions.scheduler.quartz; |
| 17 | + |
| 18 | +import io.agentscope.extensions.scheduler.ScheduleAgentTask; |
| 19 | +import io.agentscope.extensions.scheduler.config.ScheduleMode; |
| 20 | +import org.quartz.InterruptableJob; |
| 21 | +import org.quartz.JobExecutionContext; |
| 22 | +import org.quartz.JobExecutionException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Quartz Job implementation that executes an AgentScope agent task. |
| 26 | + * |
| 27 | + * <p>This job retrieves the corresponding {@link QuartzAgentScheduler} and |
| 28 | + * {@link QuartzScheduleAgentTask} using job data, and then executes the agent's run logic. |
| 29 | + * It also handles the rescheduling for {@code FIXED_DELAY} tasks. |
| 30 | + */ |
| 31 | +public class AgentQuartzJob implements InterruptableJob { |
| 32 | + |
| 33 | + private volatile boolean interrupted; |
| 34 | + |
| 35 | + /** |
| 36 | + * Executes the agent task. |
| 37 | + * |
| 38 | + * @param context The Quartz JobExecutionContext |
| 39 | + * @throws JobExecutionException if the job execution fails |
| 40 | + */ |
| 41 | + @Override |
| 42 | + public void execute(JobExecutionContext context) throws JobExecutionException { |
| 43 | + if (interrupted) { |
| 44 | + return; |
| 45 | + } |
| 46 | + String schedulerId = context.getJobDetail().getJobDataMap().getString("schedulerId"); |
| 47 | + if (schedulerId == null || schedulerId.trim().isEmpty()) { |
| 48 | + schedulerId = "default-scheduler"; |
| 49 | + } |
| 50 | + String taskName = context.getJobDetail().getJobDataMap().getString("taskName"); |
| 51 | + QuartzAgentScheduler scheduler = QuartzAgentSchedulerRegistry.get(schedulerId); |
| 52 | + if (scheduler == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + QuartzScheduleAgentTask task = scheduler.getScheduledAgent(taskName); |
| 56 | + if (task == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + try { |
| 60 | + ScheduleAgentTask<io.agentscope.core.message.Msg> t = task; |
| 61 | + // Blocking is acceptable here because Quartz jobs run in their own dedicated thread |
| 62 | + // pool |
| 63 | + // and are executed synchronously; we need to wait for the reactive pipeline to |
| 64 | + // complete. |
| 65 | + t.run().block(); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new JobExecutionException(e); |
| 68 | + } |
| 69 | + if (interrupted) { |
| 70 | + return; |
| 71 | + } |
| 72 | + ScheduleMode mode = task.getScheduleConfig().getScheduleMode(); |
| 73 | + if (mode == ScheduleMode.FIXED_DELAY) { |
| 74 | + long delay = context.getJobDetail().getJobDataMap().getLongValue("fixedDelay"); |
| 75 | + scheduler.rescheduleNextFixedDelay(context.getJobDetail().getKey(), delay); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Interrupts the currently running job. |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public void interrupt() { |
| 84 | + interrupted = true; |
| 85 | + } |
| 86 | +} |
0 commit comments