Created
December 17, 2015 05:20
-
-
Save thinkhy/2b7e519446c42870dce8 to your computer and use it in GitHub Desktop.
Replay MINIIST log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
"bufio" | |
"regexp" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func schedule(what func(), delay time.Duration) chan bool { | |
stop := make(chan bool) | |
go func() { | |
defer wg.Done() | |
for { | |
select { | |
case <-time.After(delay): | |
select { | |
case <-time.After(delay): | |
what() | |
return | |
case <-stop: | |
return | |
} | |
} | |
}() | |
return stop | |
} | |
func checkError(err error) { | |
if err != nil { // Always check errors even if they should not happen. | |
log.Panic(err) | |
} | |
} | |
func main() { | |
jesTool := "/home/huangye/tool/git/randomwkld/jes.pl" | |
workDir := "/home/huangye/tool/git/randomwkld/" | |
err := os.Chdir(workDir) | |
checkError(err) | |
logFile := "/svt/recreate.log" | |
re := `^(?P<date_time>.+CST\s+\d{4})\s+submit\s+(?P<jcl_name>.+jcl).+?on\s+(?P<lpar_name>\w+)\s*` | |
regex := regexp.MustCompile(re) | |
fh, err := os.Open(logFile) | |
f := bufio.NewReader(fh) | |
checkError(err) | |
defer fh.Close() | |
timeBegin, err := time.Parse(time.UnixDate, "Thu Nov 19 22:16:25 CST 2015") | |
checkError(err) | |
fmt.Println("Begin time: ", timeBegin) | |
timeEnd, err := time.Parse(time.UnixDate, "Fri Nov 20 08:30:20 CST 2015") | |
checkError(err) | |
fmt.Println("End time: ", timeEnd) | |
totalDuration := timeEnd.Sub(timeBegin) | |
if (totalDuration <= 0) { | |
log.Fatal("End time must be greater than start time") | |
} | |
buf := make([]byte, 1024) | |
for { | |
buf, _, err = f.ReadLine() | |
if err != nil { | |
break | |
} | |
s := string(buf) | |
matchResult := regex.FindStringSubmatch(s) | |
if (len(matchResult) < 4) { // all groups matched | |
continue | |
} | |
curDate := matchResult[1] | |
curJCL := matchResult[2] | |
curLPAR := matchResult[3] | |
// fmt.Println(curDate, "||", curJCL, "|| ", curLPAR); | |
if (curDate == "" || curJCL == "" || curLPAR == "") { | |
continue | |
} | |
t1, err := time.Parse(time.UnixDate, curDate) | |
checkError(err); | |
duration := t1.Sub(timeBegin) | |
// time is within specified scope | |
if (duration >= 0 && timeEnd.Sub(t1) >= 0) { | |
// Add a scheduled task | |
wg.Add(1) | |
log.Println("Add TASK: after ", duration, " Submit JCL ", curJCL, " on ", curLPAR) | |
schedule(func() { | |
tmp := []string{} | |
tmp = append(tmp, "pkst", curLPAR, ".pok.stglabs.ibm.com") | |
domainName := strings.Join(tmp, "") | |
cmdArgs := []string{ jesTool, "-s", domainName, "-f", curJCL, "-u", "mega", "-p", "mega" } | |
command := strings.Join(cmdArgs, " ") | |
log.Println("Execute command: ", command) | |
cmd := exec.Command(jesTool, cmdArgs...) | |
// cmd := exec.Command(command) | |
cmd.Stderr = os.Stderr | |
cmd.Stdout = os.Stdout | |
err := cmd.Run() | |
cmd.Wait() | |
if err != nil { | |
log.Println(err) | |
} else { | |
log.Println("Command completed") | |
} | |
}, | |
duration) | |
} | |
} // for | |
// time.Sleep(timeEnd.Sub(timeBegin) + time.Minute*15) | |
wg.Wait() | |
log.Println("[Done]") | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment