use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class JobScheduler method scheduleJob.
/**
* Schedules a Job to start video upload/capture. As soon as the start job is triggered,
* a corresponding stop job is scheduled based on jobDurationInMins
*
* @param jobType - Type of Job - Upload/Capture
* @param streamName - Name of the Video Stream/Camera Stream
* @param startTimeExpr - Cron Expression defined as minute, hour, day-of-month, month,
* day-of-week, year(optional)
* @param jobDurationInMins - Number of minutes the started job should run for
* @throws EdgeConnectorForKVSException - generic exception
*/
public void scheduleJob(Constants.JobType jobType, String streamName, String startTimeExpr, long jobDurationInMins) {
try {
// hardcode seconds part to 0
startTimeExpr = Integer.toString(Constants.SCHEDULER_SECONDS_BOUNDARY) + " " + startTimeExpr;
boolean valid = verifyCron(startTimeExpr, jobDurationInMins);
if (!valid) {
final String errorMessage = String.format("Error: Invalid Start Time Expression and Job Duration." + "Job Type: %s, Stream Name: %s, StartTime Expr: %s, Job Duration: %d mins." + "Job not scheduled.", jobType.name(), streamName, startTimeExpr, jobDurationInMins);
log.error(errorMessage);
throw new EdgeConnectorForKVSException(errorMessage);
}
Scheduler sched = null;
sched = this.schedulerFactory.getScheduler();
// seconds, minute, hour, day-of-month, month, day-of-week, year(optional)
log.info("Start Job Cron Expression: " + startTimeExpr);
JobDetail jobDetail = produceJobDetails(streamName, jobType.name());
CronTrigger startTrigger = newTrigger().withIdentity(streamName + "-" + jobDetail.getKey(), jobType.name()).withSchedule(cronSchedule(startTimeExpr)).build();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put(Constants.JOB_TYPE_KEY, jobType);
jobDataMap.put(Constants.JOB_DURATION_IN_MINS_KEY, jobDurationInMins);
jobDataMap.put(Constants.JOB_STREAM_NAME_KEY, streamName);
jobDataMap.put(Constants.JOB_CALLBACK_INSTANCE, schedulerCallback);
Date ft = sched.scheduleJob(jobDetail, startTrigger);
log.info(jobDetail.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + startTrigger.getCronExpression());
} catch (ParseException | SchedulerException ex) {
final String errorMessage = String.format("Could not schedule job for Job Type: %s, Stream Name: %s, " + "StartTime Expr: %s, Job Duration: %d mins. %s", jobType.name(), streamName, startTimeExpr, jobDurationInMins, ex.getMessage());
log.error(errorMessage);
throw new EdgeConnectorForKVSException(errorMessage, ex);
}
}
use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class JobScheduler method stop.
/**
* Stops the Quartz Scheduler for camera level
*/
public void stop(EdgeConnectorForKVSConfiguration configuration) {
try {
Scheduler sched = this.schedulerFactory.getScheduler();
// Remove jobs for failed camera
String kvsStreamName = configuration.getKinesisVideoStreamName();
JobDetail recorderJobDetail = produceJobDetails(kvsStreamName, Constants.JobType.LOCAL_VIDEO_CAPTURE.name());
JobDetail uploaderJobDetail = produceJobDetails(kvsStreamName, Constants.JobType.LIVE_VIDEO_STREAMING.name());
JobKey recorderJobKey = recorderJobDetail.getKey();
JobKey uploaderJobKey = uploaderJobDetail.getKey();
if (sched.checkExists(recorderJobKey)) {
sched.deleteJob(recorderJobKey);
log.info("Removing recorder job from scheduler for " + recorderJobKey);
}
if (sched.checkExists(uploaderJobKey)) {
sched.deleteJob(uploaderJobKey);
log.info("Removing uploader job from scheduler for " + uploaderJobKey);
}
} catch (SchedulerException ex) {
final String errorMessage = String.format("Error stopping scheduler for single camera: " + ex.getMessage());
log.error(errorMessage);
throw new EdgeConnectorForKVSException(errorMessage, ex);
}
}
Aggregations