Search in sources :

Example 21 with JobTrigger

use of org.pentaho.platform.api.scheduler2.JobTrigger in project pentaho-platform by pentaho.

the class QuartzScheduler method createJob.

/**
 * {@inheritDoc}
 */
protected Job createJob(String jobName, Map<String, Serializable> jobParams, IJobTrigger trigger, IBackgroundExecutionStreamProvider outputStreamProvider) throws SchedulerException {
    String curUser = getCurrentUser();
    // determine if the job params tell us who owns the job
    Serializable jobOwner = jobParams.get(RESERVEDMAPKEY_ACTIONUSER);
    if (jobOwner != null && jobOwner.toString().length() > 0) {
        curUser = jobOwner.toString();
    }
    QuartzJobKey jobId = new QuartzJobKey(jobName, curUser);
    Trigger quartzTrigger = createQuartzTrigger(trigger, jobId);
    if (trigger.getEndTime() != null) {
        quartzTrigger.setEndTime(trigger.getEndTime());
    }
    Calendar triggerCalendar = quartzTrigger instanceof CronTrigger ? createQuartzCalendar((ComplexJobTrigger) trigger) : null;
    if (outputStreamProvider != null) {
        jobParams.put(RESERVEDMAPKEY_STREAMPROVIDER, outputStreamProvider);
    }
    if (trigger.getUiPassParam() != null) {
        jobParams.put(RESERVEDMAPKEY_UIPASSPARAM, trigger.getUiPassParam());
    }
    if (!jobParams.containsKey(RESERVEDMAPKEY_LINEAGE_ID)) {
        String uuid = UUID.randomUUID().toString();
        jobParams.put(RESERVEDMAPKEY_LINEAGE_ID, uuid);
    }
    JobDetail jobDetail = createJobDetails(jobId, jobParams);
    try {
        Scheduler scheduler = getQuartzScheduler();
        if (triggerCalendar != null) {
            scheduler.addCalendar(jobId.toString(), triggerCalendar, false, false);
            quartzTrigger.setCalendarName(jobId.toString());
        }
        logger.debug(MessageFormat.format("Scheduling job {0} with trigger {1} and job parameters [ {2} ]", jobId.toString(), trigger, // $NON-NLS-1$
        prettyPrintMap(jobParams)));
        scheduler.scheduleJob(jobDetail, quartzTrigger);
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0001_FAILED_TO_SCHEDULE_JOB", jobName), // $NON-NLS-1$
        e);
    }
    Job job = new Job();
    job.setJobParams(jobParams);
    job.setJobTrigger((JobTrigger) trigger);
    job.setNextRun(quartzTrigger.getNextFireTime());
    job.setLastRun(quartzTrigger.getPreviousFireTime());
    job.setJobId(jobId.toString());
    job.setJobName(jobName);
    job.setUserName(curUser);
    job.setState(JobState.NORMAL);
    return job;
}
Also used : Serializable(java.io.Serializable) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) Scheduler(org.quartz.Scheduler) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler) Calendar(org.quartz.Calendar) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) JobTrigger(org.pentaho.platform.api.scheduler2.JobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Job(org.pentaho.platform.api.scheduler2.Job)

Example 22 with JobTrigger

use of org.pentaho.platform.api.scheduler2.JobTrigger in project pentaho-platform by pentaho.

the class SchedulerResourceUtil method convertScheduleRequestToJobTrigger.

public static IJobTrigger convertScheduleRequestToJobTrigger(JobScheduleRequest scheduleRequest, IScheduler scheduler) throws SchedulerException, UnifiedRepositoryException {
    // Used to determine if created by a RunInBackgroundCommand
    boolean runInBackground = scheduleRequest.getSimpleJobTrigger() == null && scheduleRequest.getComplexJobTrigger() == null && scheduleRequest.getCronJobTrigger() == null;
    // add 10 seconds to the RIB to ensure execution (see PPP-3264)
    IJobTrigger jobTrigger = runInBackground ? new SimpleJobTrigger(new Date(System.currentTimeMillis() + 10000), null, 0, 0) : scheduleRequest.getSimpleJobTrigger();
    if (scheduleRequest.getSimpleJobTrigger() != null) {
        SimpleJobTrigger simpleJobTrigger = scheduleRequest.getSimpleJobTrigger();
        if (simpleJobTrigger.getStartTime() == null) {
            simpleJobTrigger.setStartTime(new Date());
        }
        jobTrigger = simpleJobTrigger;
    } else if (scheduleRequest.getComplexJobTrigger() != null) {
        ComplexJobTriggerProxy proxyTrigger = scheduleRequest.getComplexJobTrigger();
        ComplexJobTrigger complexJobTrigger = new ComplexJobTrigger();
        complexJobTrigger.setStartTime(proxyTrigger.getStartTime());
        complexJobTrigger.setEndTime(proxyTrigger.getEndTime());
        complexJobTrigger.setDuration(scheduleRequest.getDuration());
        if (proxyTrigger.getDaysOfWeek().length > 0) {
            if (proxyTrigger.getWeeksOfMonth().length > 0) {
                for (int dayOfWeek : proxyTrigger.getDaysOfWeek()) {
                    for (int weekOfMonth : proxyTrigger.getWeeksOfMonth()) {
                        QualifiedDayOfWeek qualifiedDayOfWeek = new QualifiedDayOfWeek();
                        qualifiedDayOfWeek.setDayOfWeek(DayOfWeek.values()[dayOfWeek]);
                        if (weekOfMonth == JobScheduleRequest.LAST_WEEK_OF_MONTH) {
                            qualifiedDayOfWeek.setQualifier(DayOfWeekQualifier.LAST);
                        } else {
                            qualifiedDayOfWeek.setQualifier(DayOfWeekQualifier.values()[weekOfMonth]);
                        }
                        complexJobTrigger.addDayOfWeekRecurrence(qualifiedDayOfWeek);
                    }
                }
            } else {
                for (int dayOfWeek : proxyTrigger.getDaysOfWeek()) {
                    complexJobTrigger.addDayOfWeekRecurrence(dayOfWeek + 1);
                }
            }
        } else if (proxyTrigger.getDaysOfMonth().length > 0) {
            for (int dayOfMonth : proxyTrigger.getDaysOfMonth()) {
                complexJobTrigger.addDayOfMonthRecurrence(dayOfMonth);
            }
        }
        for (int month : proxyTrigger.getMonthsOfYear()) {
            complexJobTrigger.addMonthlyRecurrence(month + 1);
        }
        for (int year : proxyTrigger.getYears()) {
            complexJobTrigger.addYearlyRecurrence(year);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(complexJobTrigger.getStartTime());
        complexJobTrigger.setHourlyRecurrence(calendar.get(Calendar.HOUR_OF_DAY));
        complexJobTrigger.setMinuteRecurrence(calendar.get(Calendar.MINUTE));
        complexJobTrigger.setUiPassParam(scheduleRequest.getComplexJobTrigger().getUiPassParam());
        jobTrigger = complexJobTrigger;
    } else if (scheduleRequest.getCronJobTrigger() != null) {
        if (scheduler instanceof QuartzScheduler) {
            String cronString = scheduleRequest.getCronJobTrigger().getCronString();
            // $NON-NLS-1$
            String delims = "[ ]+";
            String[] tokens = cronString.split(delims);
            if (tokens.length < 7) {
                cronString += " *";
            }
            ComplexJobTrigger complexJobTrigger = QuartzScheduler.createComplexTrigger(cronString);
            complexJobTrigger.setStartTime(scheduleRequest.getCronJobTrigger().getStartTime());
            complexJobTrigger.setEndTime(scheduleRequest.getCronJobTrigger().getEndTime());
            complexJobTrigger.setDuration(scheduleRequest.getCronJobTrigger().getDuration());
            complexJobTrigger.setUiPassParam(scheduleRequest.getCronJobTrigger().getUiPassParam());
            jobTrigger = complexJobTrigger;
        } else {
            throw new IllegalArgumentException();
        }
    }
    return jobTrigger;
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) QuartzScheduler(org.pentaho.platform.scheduler2.quartz.QuartzScheduler) Date(java.util.Date) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) QualifiedDayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger)

Example 23 with JobTrigger

use of org.pentaho.platform.api.scheduler2.JobTrigger in project java-docs-samples by GoogleCloudPlatform.

the class Triggers method createTrigger.

// [START dlp_create_trigger]
/**
 * Schedule a DLP inspection trigger for a GCS location.
 *
 * @param triggerId (Optional) name of the trigger to be created
 * @param displayName (Optional) display name for the trigger to be created
 * @param description (Optional) description for the trigger to be created
 * @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
 * @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
 *     InfoType.EMAIL_ADDRESS
 * @param minLikelihood minimum likelihood required before returning a match
 * @param maxFindings maximum number of findings to report per request (0 = server maximum)
 * @param projectId The project ID to run the API call under
 */
private static void createTrigger(String triggerId, String displayName, String description, String bucketName, String fileName, int scanPeriod, List<InfoType> infoTypes, Likelihood minLikelihood, int maxFindings, String projectId) throws Exception {
    // instantiate a client
    DlpServiceClient dlpServiceClient = DlpServiceClient.create();
    try {
        CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
        StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
        InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
        InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setInspectConfig(inspectConfig).setStorageConfig(storageConfig).build();
        // Schedule scan of GCS bucket every scanPeriod number of days (minimum = 1 day)
        Duration duration = Duration.newBuilder().setSeconds(scanPeriod * 24 * 3600).build();
        Schedule schedule = Schedule.newBuilder().setRecurrencePeriodDuration(duration).build();
        JobTrigger.Trigger trigger = JobTrigger.Trigger.newBuilder().setSchedule(schedule).build();
        JobTrigger jobTrigger = JobTrigger.newBuilder().setInspectJob(inspectJobConfig).setName(triggerId).setDisplayName(displayName).setDescription(description).setStatus(JobTrigger.Status.HEALTHY).addTriggers(trigger).build();
        // Create scan request
        CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setJobTrigger(jobTrigger).build();
        JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
        System.out.println("Created Trigger: " + createdJobTrigger.getName());
    } catch (Exception e) {
        System.out.println("Error creating trigger: " + e.getMessage());
    }
}
Also used : StorageConfig(com.google.privacy.dlp.v2.StorageConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Schedule(com.google.privacy.dlp.v2.Schedule) CloudStorageOptions(com.google.privacy.dlp.v2.CloudStorageOptions) Duration(com.google.protobuf.Duration) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) JobTrigger(com.google.privacy.dlp.v2.JobTrigger) ParseException(org.apache.commons.cli.ParseException) InspectJobConfig(com.google.privacy.dlp.v2.InspectJobConfig) CreateJobTriggerRequest(com.google.privacy.dlp.v2.CreateJobTriggerRequest)

Aggregations

Job (org.pentaho.platform.api.scheduler2.Job)12 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)11 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)11 Test (org.junit.Test)9 Date (java.util.Date)8 Serializable (java.io.Serializable)7 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)7 JobTrigger (org.pentaho.platform.api.scheduler2.JobTrigger)7 HashMap (java.util.HashMap)5 IScheduler (org.pentaho.platform.api.scheduler2.IScheduler)5 SchedulerException (org.pentaho.platform.api.scheduler2.SchedulerException)5 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 CronJobTrigger (org.pentaho.platform.api.scheduler2.CronJobTrigger)3 QuartzScheduler (org.pentaho.platform.scheduler2.quartz.QuartzScheduler)3 IncrementalRecurrence (org.pentaho.platform.scheduler2.recur.IncrementalRecurrence)3 JobScheduleRequest (org.pentaho.platform.web.http.api.resources.JobScheduleRequest)3 ListParamValue (org.pentaho.platform.scheduler2.ws.ListParamValue)2 MapParamValue (org.pentaho.platform.scheduler2.ws.MapParamValue)2 ParamValue (org.pentaho.platform.scheduler2.ws.ParamValue)2