Search in sources :

Example 16 with EdgeConnectorForKVSException

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);
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) ParseException(java.text.ParseException) Date(java.util.Date)

Example 17 with EdgeConnectorForKVSException

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);
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Aggregations

EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)17 ArrayList (java.util.ArrayList)7 EdgeConnectorForKVSConfiguration (com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration)4 Scheduler (org.quartz.Scheduler)4 SchedulerException (org.quartz.SchedulerException)4 IOException (java.io.IOException)3 DescribeAssetResponse (software.amazon.awssdk.services.iotsitewise.model.DescribeAssetResponse)3 StreamManagerException (com.amazonaws.greengrass.streammanager.client.exception.StreamManagerException)2 TestWatchEventCallBack (com.aws.iot.edgeconnectorforkvs.diskmanager.callback.TestWatchEventCallBack)2 EdgeConnectorForKVSUnrecoverableException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 WatchEvent (java.nio.file.WatchEvent)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Test (org.junit.jupiter.api.Test)2 JobDetail (org.quartz.JobDetail)2 MessageStreamDefinition (com.amazonaws.greengrass.streammanager.model.MessageStreamDefinition)1 ExportDefinition (com.amazonaws.greengrass.streammanager.model.export.ExportDefinition)1 IoTSiteWiseConfig (com.amazonaws.greengrass.streammanager.model.export.IoTSiteWiseConfig)1 AssetPropertyValue (com.amazonaws.greengrass.streammanager.model.sitewise.AssetPropertyValue)1