Snippets
Created by
Francesco Stefanini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | package com.atlassian.scheduler.compat.clustered;
import java.util.Date;
import javax.annotation.Nullable;
import com.atlassian.scheduler.JobRunner;
import com.atlassian.scheduler.SchedulerService;
import com.atlassian.scheduler.SchedulerServiceException;
import com.atlassian.scheduler.compat.CompatibilityPluginScheduler;
import com.atlassian.scheduler.compat.JobHandler;
import com.atlassian.scheduler.compat.JobHandlerKey;
import com.atlassian.scheduler.compat.JobInfo;
import com.atlassian.scheduler.config.IntervalScheduleInfo;
import com.atlassian.scheduler.config.JobConfig;
import com.atlassian.scheduler.config.JobId;
import com.atlassian.scheduler.config.JobRunnerKey;
import com.atlassian.scheduler.config.RunMode;
import com.atlassian.scheduler.config.Schedule;
import com.atlassian.scheduler.status.JobDetails;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.atlassian.sal.api.component.ComponentLocator.getComponent;
import static com.atlassian.scheduler.config.RunMode.RUN_ONCE_PER_CLUSTER;
import static com.atlassian.scheduler.config.Schedule.Type.INTERVAL;
import static com.atlassian.scheduler.config.Schedule.forInterval;
import static com.atlassian.util.concurrent.Assertions.notNull;
/**
* Implements the compatibility plugin scheduler by delegating to the atlassian-scheduler library.
* <p>
* This implementation maps {@link JobHandler} to {@link JobRunner}, the scheduling interval in
* {@link #scheduleClusteredJob(String, JobHandlerKey, Date, long)} to
* {@link Schedule#forInterval(long, Date)}, and so on. The end result is that everything is
* scheduled as if {@link RunMode#RUN_ONCE_PER_CLUSTER} jobs with
* {@link Schedule#forInterval(long, Date) interval schedules} were created directly with the
* {@code atlassian-scheduler} API.
* </p>
*
* @since v1.0
*/
public class ClusteredCompatibilityPluginScheduler implements CompatibilityPluginScheduler
{
private static final Logger LOG = LoggerFactory.getLogger(ClusteredCompatibilityPluginScheduler.class);
private static final String JOB_RUNNER_KEY_PREFIX = "CompatibilityPluginScheduler.JobRunnerKey.";
private static final String JOB_ID_PREFIX = "CompatibilityPluginScheduler.JobId.";
private final SchedulerService schedulerService;
@SuppressWarnings("UnusedDeclaration") // lies
public ClusteredCompatibilityPluginScheduler()
{
this(getComponent(SchedulerService.class));
}
@VisibleForTesting
ClusteredCompatibilityPluginScheduler(SchedulerService schedulerService)
{
this.schedulerService = notNull("schedulerService", schedulerService);
}
@Override
public void registerJobHandler(JobHandlerKey jobHandlerKey, JobHandler jobHandler)
{
notNull("jobHandlerKey", jobHandlerKey);
notNull("jobHandler", jobHandler);
final JobRunnerKey jobRunnerKey = toJobRunnerKey(jobHandlerKey);
if (isRegistered(jobRunnerKey))
{
throw new IllegalArgumentException("Job handler already registered: " + jobHandlerKey);
}
schedulerService.registerJobRunner(jobRunnerKey, new ClusteredJobRunner(jobHandler));
}
@Override
public void unregisterJobHandler(JobHandlerKey jobHandlerKey)
{
schedulerService.unregisterJobRunner(toJobRunnerKey(jobHandlerKey));
}
@Override
public void scheduleClusteredJob(String jobKey, JobHandlerKey jobHandlerKey,
Date startTime, long repeatInterval)
{
// Explicitly reject scheduling a job without registering a runner for it first,
// because it is actually *impossible* to do this in the other implementation and
// consistency is more important than being nice.
final JobRunnerKey jobRunnerKey = toJobRunnerKey(jobHandlerKey);
if (!isRegistered(jobRunnerKey))
{
throw new IllegalArgumentException("Job handler not registered: " + jobHandlerKey);
}
final JobConfig jobConfig = JobConfig.forJobRunnerKey(jobRunnerKey)
.withRunMode(RUN_ONCE_PER_CLUSTER)
.withSchedule(forInterval(repeatInterval, startTime));
try
{
schedulerService.scheduleJob(toJobId(jobKey), jobConfig);
}
catch (SchedulerServiceException sse)
{
LOG.error("Error scheduling job " + jobKey, sse);
}
}
@Nullable
@Override
public JobInfo getJobInfo(String jobKey)
{
final JobDetails jobDetails = schedulerService.getJobDetails(toJobId(jobKey));
if (jobDetails == null ||
!jobDetails.isRunnable() ||
/*jobDetails.getRunMode() != RUN_ONCE_PER_CLUSTER ||*/
jobDetails.getSchedule().getType() != INTERVAL)
{
return null;
}
final IntervalScheduleInfo interval = jobDetails.getSchedule().getIntervalScheduleInfo();
return new JobInfo(jobKey, toJobHandlerKey(jobDetails.getJobRunnerKey()),
interval.getFirstRunTime(), interval.getIntervalInMillis());
}
@Override
public void unscheduleClusteredJob(String jobKey)
{
schedulerService.unscheduleJob(toJobId(jobKey));
}
private boolean isRegistered(JobRunnerKey jobRunnerKey)
{
return schedulerService.getRegisteredJobRunnerKeys().contains(jobRunnerKey);
}
static JobRunnerKey toJobRunnerKey(JobHandlerKey jobHandlerKey)
{
return JobRunnerKey.of(JOB_RUNNER_KEY_PREFIX + notNull("jobHandlerKey", jobHandlerKey));
}
static JobId toJobId(String jobKey)
{
return JobId.of(JOB_ID_PREFIX + notNull("jobKey", jobKey));
}
static JobHandlerKey toJobHandlerKey(JobRunnerKey jobRunnerKey)
{
final String s = notNull("jobRunnerKey", jobRunnerKey).toString();
if (s.length() > JOB_RUNNER_KEY_PREFIX.length() && s.startsWith(JOB_RUNNER_KEY_PREFIX))
{
return JobHandlerKey.of(s.substring(JOB_RUNNER_KEY_PREFIX.length()));
}
throw new IllegalArgumentException("Job runner key belongs to somebody else: " + jobRunnerKey);
}
static String toJobKey(JobId jobId)
{
final String s = notNull("jobId", jobId).toString();
if (s.length() > JOB_ID_PREFIX.length() && s.startsWith(JOB_ID_PREFIX))
{
return s.substring(JOB_ID_PREFIX.length());
}
throw new IllegalArgumentException("Job ID belongs to somebody else: " + jobId);
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.