use of org.exist.storage.SystemTask in project exist by eXist-db.
the class TriggerSystemTask method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final String className = args[0].getStringValue();
final Properties properties = new Properties();
if (args[1].hasOne()) {
parseParameters(((NodeValue) args[1].itemAt(0)).getNode(), properties);
}
try {
final Class<?> clazz = Class.forName(className);
final Object taskObject = clazz.newInstance();
if (!(taskObject instanceof SystemTask)) {
final XPathException xPathException = new XPathException(this, className + " is not an instance of org.exist.storage.SystemTask");
logger.error("Java classname is not a SystemTask", xPathException);
throw xPathException;
}
final SystemTask task = (SystemTask) taskObject;
task.configure(context.getBroker().getConfiguration(), properties);
LOG.info("Triggering SystemTask: {}", className);
context.getBroker().getBrokerPool().triggerSystemTask(task);
} catch (final ClassNotFoundException e) {
final String message = "system task class '" + className + "' not found";
logger.error(message, e);
throw new XPathException(this, message);
} catch (final InstantiationException e) {
final String message = "system task '" + className + "' can not be instantiated";
logger.error(message, e);
throw new XPathException(this, message);
} catch (final IllegalAccessException e) {
final String message = "system task '" + className + "' can not be accessed";
logger.error(message, e);
throw new XPathException(this, message);
} catch (final EXistException e) {
final String message = "system task " + className + " reported an error during initialization: ";
logger.error(message, e);
throw new XPathException(this, message + e.getMessage(), e);
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.storage.SystemTask in project exist by eXist-db.
the class QuartzSchedulerImpl method setupConfiguredJobs.
/**
* Set's up all the jobs that are listed in conf.xml and loaded through org.exist.util.Configuration.
*/
@Override
public void setupConfiguredJobs() {
final JobConfig[] jobList = (JobConfig[]) config.getProperty(JobConfig.PROPERTY_SCHEDULER_JOBS);
if (jobList == null) {
return;
}
for (final JobConfig jobConfig : jobList) {
JobDescription job = null;
if (jobConfig.getResourceName().startsWith("/db/") || jobConfig.getResourceName().indexOf(':') > 0) {
if (jobConfig.getType().equals(JobType.SYSTEM)) {
LOG.error("System jobs may only be written in Java");
} else {
// create an XQuery job
final Subject guestUser = brokerPool.getSecurityManager().getGuestSubject();
job = new UserXQueryJob(jobConfig.getJobName(), jobConfig.getResourceName(), guestUser);
try {
// check if a job with the same name is already registered
if (getScheduler().getJobDetail(new JobKey(job.getName(), UserJob.JOB_GROUP)) != null) {
// yes, try to make the job's name unique
job.setName(job.getName() + job.hashCode());
}
} catch (final SchedulerException e) {
LOG.error("Unable to set job name: {}", e.getMessage(), e);
}
}
} else {
// create a Java job
try {
final Class<?> jobClass = Class.forName(jobConfig.getResourceName());
final Object jobObject = jobClass.newInstance();
if (jobConfig.getType().equals(JobType.SYSTEM)) {
if (jobObject instanceof SystemTask) {
final SystemTask task = (SystemTask) jobObject;
task.configure(config, jobConfig.getParameters());
job = new SystemTaskJobImpl(jobConfig.getJobName(), task);
} else {
LOG.error("System jobs must extend SystemTask");
// throw exception? will be handled nicely
}
} else {
if (jobObject instanceof JobDescription) {
job = (JobDescription) jobObject;
if (jobConfig.getJobName() != null) {
job.setName(jobConfig.getJobName());
}
} else {
LOG.error("Startup job {} must extend org.exist.scheduler.StartupJob", jobConfig.getJobName());
// throw exception? will be handled nicely
}
}
} catch (final Exception e) {
// Throwable?
LOG.error("Unable to schedule '{}' job {}: {}", jobConfig.getType(), jobConfig.getResourceName(), e.getMessage(), e);
}
}
// if there is a job, schedule it
if (job != null) {
// trigger is Cron or period?
if (jobConfig.getSchedule().indexOf(' ') > -1) {
// schedule job with Cron trigger
createCronJob(jobConfig.getSchedule(), job, jobConfig.getParameters());
} else {
// schedule job with periodic trigger
createPeriodicJob(Long.parseLong(jobConfig.getSchedule()), job, jobConfig.getDelay(), jobConfig.getParameters(), jobConfig.getRepeat(), jobConfig.unscheduleOnException());
}
}
}
}
use of org.exist.storage.SystemTask in project exist by eXist-db.
the class SystemTaskJobImpl method execute.
@Override
public final void execute(final JobExecutionContext jec) throws JobExecutionException {
final JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap();
final BrokerPool pool = (BrokerPool) jobDataMap.get(DATABASE);
final SystemTask task = (SystemTask) jobDataMap.get(SYSTEM_TASK);
// if invalid arguments then abort
if ((pool == null) || (task == null)) {
// abort all triggers for this job
final JobExecutionException jaa = new JobExecutionException("SystemTaskJob Failed: BrokerPool or SystemTask was null! Unscheduling SystemTask", false);
jaa.setUnscheduleAllTriggers(true);
throw jaa;
}
// trigger the system task
pool.triggerSystemTask(task);
}
use of org.exist.storage.SystemTask in project exist by eXist-db.
the class SanityReport method triggerCheck.
@Override
public void triggerCheck(String output, String backup, String incremental) {
try {
this.output = output;
final SystemTask task = new ConsistencyCheckTask();
final Properties properties = parseParameter(output, backup, incremental);
task.configure(pool.getConfiguration(), properties);
pool.triggerSystemTask(task);
} catch (final EXistException existException) {
taskstatus.setStatus(TaskStatus.Status.STOPPED_ERROR);
final List<ErrorReport> errors = new ArrayList<>();
errors.add(new ErrorReport(ErrorReport.CONFIGURATION_FAILD, existException.getMessage(), existException));
taskstatus.setReason(errors);
changeStatus(taskstatus);
taskstatus.setStatusChangeTime();
taskstatus.setReason(existException.toString());
LOG.warn("Failed to trigger db sanity check: {}", existException.getMessage(), existException);
}
}
Aggregations