org.springframework.cloud.deployer.spi.scheduler.SchedulerPropertyKeys Java Examples

The following examples show how to use org.springframework.cloud.deployer.spi.scheduler.SchedulerPropertyKeys. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Override
public List<ScheduleInfo> list() {
	CronJobList cronJobList = this.client.batch().cronjobs().list();

	List<CronJob> cronJobs = cronJobList.getItems();
	List<ScheduleInfo> scheduleInfos = new ArrayList<>();

	for (CronJob cronJob : cronJobs) {
		if (cronJob.getMetadata() != null && cronJob.getMetadata().getLabels() != null &&
				StringUtils.hasText(cronJob.getMetadata().getLabels().get(SPRING_CRONJOB_ID_KEY))) {
			Map<String, String> properties = new HashMap<>();
			properties.put(SchedulerPropertyKeys.CRON_EXPRESSION, cronJob.getSpec().getSchedule());

			ScheduleInfo scheduleInfo = new ScheduleInfo();
			scheduleInfo.setScheduleName(cronJob.getMetadata().getName());
			scheduleInfo.setTaskDefinitionName(cronJob.getMetadata().getLabels().get(SPRING_CRONJOB_ID_KEY));
			scheduleInfo.setScheduleProperties(properties);

			scheduleInfos.add(scheduleInfo);
		}
	}

	return scheduleInfos;
}
 
Example #2
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
protected CronJob createCronJob(ScheduleRequest scheduleRequest) {
	Map<String, String> labels = Collections.singletonMap(SPRING_CRONJOB_ID_KEY,
			scheduleRequest.getDefinition().getName());

	Map<String, String> schedulerProperties = scheduleRequest.getSchedulerProperties();
	String schedule = schedulerProperties.get(SchedulerPropertyKeys.CRON_EXPRESSION);
	Assert.hasText(schedule, "The property: " + SchedulerPropertyKeys.CRON_EXPRESSION + " must be defined");

	PodSpec podSpec = createPodSpec(scheduleRequest);
	String taskServiceAccountName = this.deploymentPropertiesResolver.getTaskServiceAccountName(scheduleRequest.getSchedulerProperties());
	if (StringUtils.hasText(taskServiceAccountName)) {
		podSpec.setServiceAccountName(taskServiceAccountName);
	}

	CronJob cronJob = new CronJobBuilder().withNewMetadata().withName(scheduleRequest.getScheduleName())
			.withLabels(labels).endMetadata().withNewSpec().withSchedule(schedule).withNewJobTemplate()
			.withNewSpec().withNewTemplate().withSpec(podSpec).endTemplate().endSpec()
			.endJobTemplate().endSpec().build();

	setImagePullSecret(scheduleRequest, cronJob);

	return this.client.batch().cronjobs().create(cronJob);
}
 
Example #3
Source File: AbstractSchedulerIntegrationTests.java    From spring-cloud-deployer with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidCronExpression() {
	final String INVALID_EXPRESSION = "BAD";
	String definitionName = randomName();
	String scheduleName = scheduleName() + definitionName;
	Map<String, String> properties = new HashMap<>(getSchedulerProperties());
	properties.put(SchedulerPropertyKeys.CRON_EXPRESSION, INVALID_EXPRESSION);
	AppDefinition definition = new AppDefinition(definitionName, properties);
	ScheduleRequest request = new ScheduleRequest(definition, properties, getDeploymentProperties(), getCommandLineArgs(), scheduleName, testApplication());
	this.expectedException.expect(CreateScheduleException.class);

	taskScheduler().schedule(request);
}
 
Example #4
Source File: CloudFoundryAppScheduler.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a Flux of {@link ScheduleInfo}s for the pageNumber specified.
 * The PCF-Scheduler returns all data in pages of 50 entries.  This method
 * retrieves the specified page and transforms the {@link Flux} of {@link Job}s to
 * a {@link Flux} of {@link ScheduleInfo}s
 *
 * @param pageNumber integer containing the page offset for the {@link ScheduleInfo}s to retrieve.
 * @return {@link Flux} containing the {@link ScheduleInfo}s for the specified page number.
 */
private Flux<ScheduleInfo> getSchedules(int pageNumber) {
	Flux<ApplicationSummary> applicationSummaries = cacheAppSummaries();
	return this.getSpace(this.properties.getSpace()).flatMap(requestSummary -> {
		return this.client.jobs().list(ListJobsRequest.builder()
				.spaceId(requestSummary.getId())
				.page(pageNumber)
				.detailed(true).build());})
			.flatMapIterable(jobs -> jobs.getResources())// iterate over the resources returned.
			.flatMap(job -> {
				return getApplication(applicationSummaries,
						job.getApplicationId()) // get the application name for each job.
						.map(optionalApp -> {
							ScheduleInfo scheduleInfo = new ScheduleInfo();
							scheduleInfo.setScheduleProperties(new HashMap<>());
							scheduleInfo.setScheduleName(job.getName());
							scheduleInfo.setTaskDefinitionName(optionalApp.getName());
							if (job.getJobSchedules() != null) {
								scheduleInfo.getScheduleProperties().put(SchedulerPropertyKeys.CRON_EXPRESSION,
										job.getJobSchedules().get(0).getExpression());
							}
							else {
								logger.warn(String.format("Job %s does not have an associated schedule", job.getName()));
							}
							return scheduleInfo;
						});
			});
}
 
Example #5
Source File: TaskScheduleIT.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void listTest() {

	Assumptions.assumeTrue(enabledScheduler);

	logger.info("schedule-list-test");

	try (Task task1 = tasks.builder().name(randomName("task1")).definition("timestamp").create();
		 Task task2 = tasks.builder().name(randomName("task2")).definition("timestamp").create();

		 TaskSchedule taskSchedule1 = schedules.builder().prefix(randomName("schedule1")).task(task1).create();
		 TaskSchedule taskSchedule2 = schedules.builder().prefix(randomName("schedule2")).task(task2).create()) {

		taskSchedule1.schedule(Collections.singletonMap(DEFAULT_SCDF_EXPRESSION_KEY, DEFAULT_CRON_EXPRESSION));
		taskSchedule2.schedule(Collections.singletonMap(DEFAULT_SCDF_EXPRESSION_KEY, DEFAULT_CRON_EXPRESSION));

		assertThat(schedules.list().size()).isEqualTo(2);

		HashSet<String> scheduleSet = new HashSet<>(Arrays.asList(taskSchedule1.getScheduleName(), taskSchedule2.getScheduleName()));

		for (TaskSchedule taskSchedule : schedules.list()) {
			if (scheduleSet.contains(taskSchedule.getScheduleName())) {
				assertThat(taskSchedule.getScheduleProperties().get(SchedulerPropertyKeys.CRON_EXPRESSION)).isEqualTo(DEFAULT_CRON_EXPRESSION);
			}
			else {
				fail(String.format("%s schedule is missing from result set of list.", taskSchedule.getScheduleName()));
			}
		}
	}
}
 
Example #6
Source File: TaskScheduleIT.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void filterByTaskTest() {
	Assumptions.assumeTrue(enabledScheduler);

	logger.info("schedule-find-by-task-test");

	try (Task task1 = tasks.builder().name(randomName("task1")).definition("timestamp").create();
		 Task task2 = tasks.builder().name(randomName("task2")).definition("timestamp").create();

		 TaskSchedule taskSchedule1 = schedules.builder().prefix(randomName("schedule1")).task(task1).create();
		 TaskSchedule taskSchedule2 = schedules.builder().prefix(randomName("schedule2")).task(task2).create()) {

		assertThat(schedules.list().size()).isEqualTo(0);
		assertThat(schedules.list(task1).size()).isEqualTo(0);
		assertThat(schedules.list(task2).size()).isEqualTo(0);

		taskSchedule1.schedule(Collections.singletonMap(DEFAULT_SCDF_EXPRESSION_KEY, DEFAULT_CRON_EXPRESSION));
		taskSchedule2.schedule(Collections.singletonMap(DEFAULT_SCDF_EXPRESSION_KEY, DEFAULT_CRON_EXPRESSION));

		assertThat(schedules.list().size()).isEqualTo(2);
		assertThat(schedules.list(task1).size()).isEqualTo(1);
		assertThat(schedules.list(task2).size()).isEqualTo(1);

		assertThat(schedules.list(task1).get(0).getScheduleName()).isEqualTo(taskSchedule1.getScheduleName());
		assertThat(schedules.list(task1).get(0).getScheduleProperties().containsKey(SchedulerPropertyKeys.CRON_EXPRESSION)).isTrue();
		assertThat(schedules.list(task1).get(0).getScheduleProperties().get(SchedulerPropertyKeys.CRON_EXPRESSION)).isEqualTo(DEFAULT_CRON_EXPRESSION);

		assertThat(schedules.list(task2).get(0).getScheduleName()).isEqualTo(taskSchedule2.getScheduleName());
		assertThat(schedules.list(task2).get(0).getScheduleProperties().containsKey(SchedulerPropertyKeys.CRON_EXPRESSION)).isTrue();
		assertThat(schedules.list(task2).get(0).getScheduleProperties().get(SchedulerPropertyKeys.CRON_EXPRESSION)).isEqualTo(DEFAULT_CRON_EXPRESSION);
	}
}
 
Example #7
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Override
protected Map<String, String> getSchedulerProperties() {
	return Collections.singletonMap(SchedulerPropertyKeys.CRON_EXPRESSION, "57 13 ? * *");
}
 
Example #8
Source File: SpringCloudSchedulerIntegrationTests.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 4 votes vote down vote up
@Override
protected Map<String, String> getSchedulerProperties() {
	return Collections.singletonMap(SchedulerPropertyKeys.CRON_EXPRESSION,"41 17 ? * *");
}
 
Example #9
Source File: TaskSchedulerControllerTests.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
private void createSampleSchedule(String taskDefinitionName, String scheduleName) {
	Map<String, String> properties = new HashMap<>();
	properties.put("scheduler.testApp." + SchedulerPropertyKeys.CRON_EXPRESSION, "* * * * *");
	schedulerService.schedule(scheduleName, taskDefinitionName, properties, new ArrayList<>(), null);
}
 
Example #10
Source File: TaskScheduleIT.java    From spring-cloud-dataflow with Apache License 2.0 3 votes vote down vote up
@Test
public void scheduleLifeCycle() {

	Assumptions.assumeTrue(enabledScheduler);

	logger.info("schedule-lifecycle-test");

	try (Task task = tasks.builder().name(randomName("task")).definition("timestamp").create();
		 TaskSchedule taskSchedule = schedules.builder().prefix(randomName("schedule")).task(task).create()) {

		assertThat(taskSchedule.isScheduled()).isFalse();

		logger.info("schedule-lifecycle-test: SCHEDULE");
		taskSchedule.schedule(Collections.singletonMap(DEFAULT_SCDF_EXPRESSION_KEY, DEFAULT_CRON_EXPRESSION));

		assertThat(taskSchedule.isScheduled()).isTrue();

		TaskSchedule retrievedSchedule = schedules.findByScheduleName(taskSchedule.getScheduleName());
		assertThat(retrievedSchedule.getScheduleName()).isEqualTo(taskSchedule.getScheduleName());
		assertThat(retrievedSchedule.getScheduleProperties().containsKey(SchedulerPropertyKeys.CRON_EXPRESSION)).isTrue();
		assertThat(retrievedSchedule.getScheduleProperties().get(SchedulerPropertyKeys.CRON_EXPRESSION)).isEqualTo(DEFAULT_CRON_EXPRESSION);

		logger.info("schedule-lifecycle-test: UNSCHEDULE");
		taskSchedule.unschedule();

		assertThat(taskSchedule.isScheduled()).isFalse();
	}
}