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.execute.ShardingTotalCountUsageJobExecutorServiceHandler;
22  import org.apache.shardingsphere.data.pipeline.core.job.config.PipelineJobConfiguration;
23  import org.apache.shardingsphere.data.pipeline.core.job.config.yaml.swapper.YamlPipelineJobConfigurationSwapper;
24  import org.apache.shardingsphere.data.pipeline.core.job.id.PipelineJobIdUtils;
25  import org.apache.shardingsphere.data.pipeline.core.job.type.PipelineJobOption;
26  import org.apache.shardingsphere.data.pipeline.core.listener.PipelineElasticJobListener;
27  import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
28  import org.apache.shardingsphere.infra.util.datetime.DateTimeFormatterFactory;
29  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
30  
31  import java.time.LocalDateTime;
32  import java.util.Collections;
33  
34  /**
35   * Pipeline job configuration manager.
36   */
37  @RequiredArgsConstructor
38  public final class PipelineJobConfigurationManager {
39      
40      private final PipelineJobOption jobOption;
41      
42      /**
43       * Get job configuration.
44       *
45       * @param jobId job ID
46       * @param <T> type of pipeline job configuration
47       * @return pipeline job configuration
48       */
49      @SuppressWarnings("unchecked")
50      public <T extends PipelineJobConfiguration> T getJobConfiguration(final String jobId) {
51          return (T) jobOption.getYamlJobConfigurationSwapper().swapToObject(PipelineJobIdUtils.getElasticJobConfigurationPOJO(jobId).getJobParameter());
52      }
53      
54      /**
55       * Convert to job configuration POJO.
56       *
57       * @param jobConfig pipeline job configuration
58       * @return converted job configuration POJO
59       */
60      @SuppressWarnings({"unchecked", "rawtypes"})
61      public JobConfigurationPOJO convertToJobConfigurationPOJO(final PipelineJobConfiguration jobConfig) {
62          JobConfigurationPOJO result = new JobConfigurationPOJO();
63          result.setJobName(jobConfig.getJobId());
64          result.setShardingTotalCount(jobOption.isForceNoShardingWhenConvertToJobConfigurationPOJO() ? 1 : jobConfig.getJobShardingCount());
65          YamlPipelineJobConfigurationSwapper swapper = jobOption.getYamlJobConfigurationSwapper();
66          result.setJobParameter(YamlEngine.marshal(swapper.swapToYamlConfiguration(jobConfig)));
67          String createTimeFormat = LocalDateTime.now().format(DateTimeFormatterFactory.getDatetimeFormatter());
68          result.getProps().setProperty("create_time", createTimeFormat);
69          result.getProps().setProperty("start_time_millis", String.valueOf(System.currentTimeMillis()));
70          result.getProps().setProperty("run_count", "1");
71          result.setJobListenerTypes(Collections.singletonList(PipelineElasticJobListener.class.getName()));
72          result.setJobExecutorServiceHandlerType(ShardingTotalCountUsageJobExecutorServiceHandler.TYPE);
73          return result;
74      }
75  }