Skip to content

Commit

Permalink
ctl: support using task-file when need task-name (pingcap#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 committed Aug 13, 2020
1 parent 21a6e6e commit adc645d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 9 deletions.
19 changes: 19 additions & 0 deletions dm/ctl/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,22 @@ func IsDDL(sql string) (bool, error) {
return false, nil
}
}

// GetTaskNameFromArgOrFile tries to retrieve name field from the file if arg is filename-like, otherwise returns arg directly
func GetTaskNameFromArgOrFile(arg string) string {
if !strings.HasSuffix(arg, ".yaml") {
return arg
}
var (
content []byte
err error
)
if content, err = GetFileContent(arg); err != nil {
return arg
}
cfg := config.NewTaskConfig()
if err := cfg.Decode(string(content)); err != nil {
return arg
}
return cfg.Name
}
4 changes: 2 additions & 2 deletions dm/ctl/master/pause_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// NewPauseTaskCmd creates a PauseTask command
func NewPauseTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pause-task [-w worker ...] <task-name>",
Use: "pause-task [-w worker ...] <task-name | task-file>",
Short: "pause a specified running task",
Run: pauseTaskFunc,
}
Expand All @@ -39,7 +39,7 @@ func pauseTaskFunc(cmd *cobra.Command, _ []string) {
cmd.Usage()
return
}
name := cmd.Flags().Arg(0)
name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0))

workers, err := common.GetWorkerArgs(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/resume_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// NewResumeTaskCmd creates a ResumeTask command
func NewResumeTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "resume-task [-w worker ...] <task-name>",
Use: "resume-task [-w worker ...] <task-name | task-file>",
Short: "resume a specified paused task",
Run: resumeTaskFunc,
}
Expand All @@ -39,7 +39,7 @@ func resumeTaskFunc(cmd *cobra.Command, _ []string) {
cmd.Usage()
return
}
name := cmd.Flags().Arg(0)
name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0))

workers, err := common.GetWorkerArgs(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions dm/ctl/master/stop_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// NewStopTaskCmd creates a StopTask command
func NewStopTaskCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "stop-task [-w worker ...] <task-name>",
Use: "stop-task [-w worker ...] <task-name | task-file>",
Short: "stop a specified task",
Run: stopTaskFunc,
}
Expand All @@ -39,7 +39,7 @@ func stopTaskFunc(cmd *cobra.Command, _ []string) {
cmd.Usage()
return
}
name := cmd.Flags().Arg(0)
name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0))

workers, err := common.GetWorkerArgs(cmd)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/pause_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function pause_task_wrong_arg() {
run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
"pause-task" \
"pause-task \[-w worker ...\] <task-name> \[flags\]" 1
"pause-task \[-w worker ...\] <task-name | task-file> \[flags\]" 1
}

function pause_task_success() {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/resume_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function resume_task_wrong_arg() {
run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
"resume-task" \
"resume-task \[-w worker ...\] <task-name> \[flags\]" 1
"resume-task \[-w worker ...\] <task-name | task-file> \[flags\]" 1
}

function resume_task_success() {
Expand Down
2 changes: 1 addition & 1 deletion tests/dmctl_basic/check_list/stop_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
function stop_task_wrong_arg() {
run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
"stop-task" \
"stop-task \[-w worker ...\] <task-name> \[flags\]" 1
"stop-task \[-w worker ...\] <task-name | task-file> \[flags\]" 1
}
4 changes: 4 additions & 0 deletions tests/dmctl_basic/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ function run() {
resume_task_success $TASK_NAME
check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 20

# test use task file instead of task name
pause_task_success "$cur/conf/dm-task.yaml"
resume_task_success "$cur/conf/dm-task.yaml"
update_relay_success $dm_worker1_conf 127.0.0.1:$WORKER1_PORT
update_relay_success $dm_worker2_conf 127.0.0.1:$WORKER2_PORT

# check worker config backup file is correct
[ -f $WORK_DIR/worker1/dm-worker-config.bak ] && cmp $WORK_DIR/worker1/dm-worker-config.bak $cur/conf/dm-worker1.toml
[ -f $WORK_DIR/worker2/dm-worker-config.bak ] && cmp $WORK_DIR/worker2/dm-worker-config.bak $cur/conf/dm-worker2.toml
Expand Down

0 comments on commit adc645d

Please sign in to comment.