github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/config/task_cli_args.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package config
    15  
    16  import (
    17  	"encoding/json"
    18  	"time"
    19  
    20  	"github.com/pingcap/tiflow/dm/pkg/terror"
    21  	"github.com/pingcap/tiflow/dm/pkg/utils"
    22  )
    23  
    24  // TaskCliArgs is the task command line arguments, these arguments have higher priority than the config file and
    25  // downstream checkpoint, but may need to be removed after the first time they take effect.
    26  type TaskCliArgs struct {
    27  	StartTime        string `toml:"start-time" json:"start_time"`
    28  	SafeModeDuration string `toml:"safe-mode-duration" json:"safe_mode_duration"`
    29  	WaitTimeOnStop   string `toml:"wait-time-on-stop" json:"wait_time_on_stop"`
    30  }
    31  
    32  // ToJSON returns json marshal result.
    33  func (t *TaskCliArgs) ToJSON() (string, error) {
    34  	cfg, err := json.Marshal(t)
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  	return string(cfg), nil
    39  }
    40  
    41  // Decode load a json representation of TaskCliArgs.
    42  func (t *TaskCliArgs) Decode(data []byte) error {
    43  	err := json.Unmarshal(data, t)
    44  	return err
    45  }
    46  
    47  // Verify checks if all fields are legal.
    48  func (t *TaskCliArgs) Verify() error {
    49  	if t.StartTime != "" {
    50  		if _, err := utils.ParseStartTime(t.StartTime); err != nil {
    51  			return terror.Annotate(err, "error while parse start-time, expected in the format like '2006-01-02 15:04:05' or '2006-01-02T15:04:05'")
    52  		}
    53  	}
    54  
    55  	if t.SafeModeDuration != "" {
    56  		_, err := time.ParseDuration(t.SafeModeDuration)
    57  		if err != nil {
    58  			return terror.Annotate(err, "error while parse safe-mode-duration, expected in the format like '1s' or '1h'")
    59  		}
    60  	}
    61  
    62  	if t.WaitTimeOnStop != "" {
    63  		_, err := time.ParseDuration(t.WaitTimeOnStop)
    64  		if err != nil {
    65  			return terror.Annotate(err, "error while parse stop_wait_timeout_duration, expected in the format like '1s' or '1h'")
    66  		}
    67  	}
    68  	return nil
    69  }