Skip to content

Commit

Permalink
task supports perMs parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cofcool committed Aug 15, 2024
1 parent e25b320 commit 2c13148
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Java,可把后缀为`.desktop`,`.webloc`的多个网页快捷文件提取到

## 重复执行 SHELL 命令

使用: `./sourcebox.sh --tool=task --count=10 --cmd='echo $count'`
支持指定执行次数,循环次数,执行频率等,使用: `./sourcebox.sh --tool=task --count=10 --cmd='echo $count'`

## 参与开发

Expand Down
80 changes: 53 additions & 27 deletions go/src/sourcebox/file/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,66 @@ func (m *Task) Run() error {
if level <= 0 {
level = 1
}
fmt.Printf("task execute times: %d x %d\n", level, count)

perMsIn, err := m.config.ReadArg("perMs")
sleepDuration := time.Duration(0)
if err == nil && perMsIn.Val != "" {
perMs, err := strconv.Atoi(perMsIn.Val)
if err != nil {
return err
}
sleepDuration = time.Millisecond * time.Duration(perMs)
level = 1
}

idx := 0
for i := 0; i < level; i++ {
for j := 0; j < count; j++ {
s := cmdIn.Val
idx++
cmds, err := shell.Fields(s, func(s string) string {
switch s {
case "level":
return fmt.Sprintf("%d", i)
case "count":
return fmt.Sprintf("%d", j)
case "idx":
return fmt.Sprintf("%d", idx)
case "timestamp":
return fmt.Sprintf("%d", time.Now().Unix())
case "timestamp_milli":
return fmt.Sprintf("%d", time.Now().UnixMilli())
default:
return os.Getenv(s)
}
})
if err != nil {
ok, err := executeTask(cmdIn.Val, i, j, idx)
if !ok && err != nil {
return err
}
fmt.Println(cmds)
cmd := exec.Command(cmds[0], cmds[1:]...)
o, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err.Error())
continue
if sleepDuration > 0 {
time.Sleep(sleepDuration)
}
fmt.Printf("result is: %s\n", o)
}
}

return nil
}

func executeTask(s string, level, count, idx int) (bool, error) {
cmds, err := shell.Fields(s, func(s string) string {
switch s {
case "level":
return fmt.Sprintf("%d", level)
case "count":
return fmt.Sprintf("%d", count)
case "idx":
return fmt.Sprintf("%d", idx)
case "timestamp":
return fmt.Sprintf("%d", time.Now().Unix())
case "timestamp_milli":
return fmt.Sprintf("%d", time.Now().UnixMilli())
default:
return os.Getenv(s)
}
})
if err != nil {
return false, err
}
fmt.Println(cmds)
cmd := exec.Command(cmds[0], cmds[1:]...)
o, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err.Error())
return true, nil
}
fmt.Printf("result is: %s\n", o)
return true, nil
}

func (m *Task) Config() *tool.Config {
return m.config
}
Expand All @@ -92,7 +113,12 @@ func (m *Task) Init() {
"count": {
Key: "count",
Required: true,
Desc: "single loop count",
Desc: "single loop count or execute count",
},
"perMs": {
Key: "perMs",
Required: false,
Desc: "frequency of execution",
},
},
}
Expand Down
20 changes: 20 additions & 0 deletions go/src/sourcebox/file/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ func TestTask_Run(t *testing.T) {
},
},
},
{
Name: "runWithPerMs",
Config: &tool.Config{
Context: test.NewContext("", nil),
Args: map[string]*tool.Arg{
"cmd": {
Key: "cmd",
Val: "echo $level-$count-$HOME-$timestamp-$idx-$timestamp_milli",
},
"count": {
Key: "count",
Val: "10",
},
"perMs": {
Key: "perMs",
Val: "100",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
Expand Down

0 comments on commit 2c13148

Please sign in to comment.