View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.job.service;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.data.pipeline.core.job.config.PipelineJobConfiguration;
22  import org.apache.shardingsphere.data.pipeline.core.job.type.PipelineJobType;
23  import org.apache.shardingsphere.data.pipeline.core.listener.PipelineElasticJobListener;
24  import org.apache.shardingsphere.data.pipeline.core.job.id.PipelineJobIdUtils;
25  import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
26  import org.apache.shardingsphere.infra.util.datetime.DateTimeFormatterFactory;
27  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
28  
29  import java.time.LocalDateTime;
30  import java.util.Collections;
31  
32  /**
33   * Pipeline job configuration manager.
34   */
35  @RequiredArgsConstructor
36  public final class PipelineJobConfigurationManager {
37      
38      private final PipelineJobType jobType;
39      
40      /**
41       * Get job configuration.
42       *
43       * @param jobId job ID
44       * @param <T> type of pipeline job configuration
45       * @return pipeline job configuration
46       */
47      @SuppressWarnings("unchecked")
48      public <T extends PipelineJobConfiguration> T getJobConfiguration(final String jobId) {
49          return (T) jobType.getYamlJobConfigurationSwapper().swapToObject(PipelineJobIdUtils.getElasticJobConfigurationPOJO(jobId).getJobParameter());
50      }
51      
52      /**
53       * Convert to job configuration POJO.
54       *
55       * @param jobConfig pipeline job configuration
56       * @return converted job configuration POJO
57       */
58      public JobConfigurationPOJO convertToJobConfigurationPOJO(final PipelineJobConfiguration jobConfig) {
59          JobConfigurationPOJO result = new JobConfigurationPOJO();
60          result.setJobName(jobConfig.getJobId());
61          result.setShardingTotalCount(jobType.isForceNoShardingWhenConvertToJobConfigurationPOJO() ? 1 : jobConfig.getJobShardingCount());
62          result.setJobParameter(YamlEngine.marshal(jobType.getYamlJobConfigurationSwapper().swapToYamlConfiguration(jobConfig)));
63          String createTimeFormat = LocalDateTime.now().format(DateTimeFormatterFactory.getStandardFormatter());
64          result.getProps().setProperty("create_time", createTimeFormat);
65          result.getProps().setProperty("start_time_millis", String.valueOf(System.currentTimeMillis()));
66          result.getProps().setProperty("run_count", "1");
67          result.setJobListenerTypes(Collections.singletonList(PipelineElasticJobListener.class.getName()));
68          return result;
69      }
70  }