本文整理匯總了Golang中github.com/MG-RAST/AWE/lib/core.Workunit.CDworkpath方法的典型用法代碼示例。如果您正苦於以下問題:Golang Workunit.CDworkpath方法的具體用法?Golang Workunit.CDworkpath怎麽用?Golang Workunit.CDworkpath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/MG-RAST/AWE/lib/core.Workunit
的用法示例。
在下文中一共展示了Workunit.CDworkpath方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RunWorkunit
func RunWorkunit(work *core.Workunit) (err error) {
args := work.Cmd.ParsedArgs
//change cwd to the workunit's working directory
if err := work.CDworkpath(); err != nil {
return err
}
commandName := work.Cmd.Name
cmd := exec.Command(commandName, args...)
msg := fmt.Sprintf("worker: start cmd=%s, args=%v", commandName, args)
fmt.Println(msg)
logger.Debug(1, msg)
logger.Event(event.WORK_START, "workid="+work.Id,
"cmd="+commandName,
fmt.Sprintf("args=%v", args))
var stdout, stderr io.ReadCloser
if conf.PRINT_APP_MSG {
stdout, err = cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err = cmd.StderrPipe()
if err != nil {
return err
}
}
if err := cmd.Start(); err != nil {
return errors.New(fmt.Sprintf("start_cmd=%s, err=%s", commandName, err.Error()))
}
if conf.PRINT_APP_MSG {
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-chankill:
if err := cmd.Process.Kill(); err != nil {
fmt.Println("failed to kill" + err.Error())
}
<-done // allow goroutine to exit
fmt.Println("process killed")
return errors.New("process killed")
case err := <-done:
if err != nil {
return errors.New(fmt.Sprintf("wait_cmd=%s, err=%s", commandName, err.Error()))
}
}
logger.Event(event.WORK_END, "workid="+work.Id)
return
}
示例2: RunWorkunitDirect
func RunWorkunitDirect(work *core.Workunit) (pstats *core.WorkPerf, err error) {
pstats = new(core.WorkPerf)
args := work.Cmd.ParsedArgs
//change cwd to the workunit's working directory
if err := work.CDworkpath(); err != nil {
return nil, err
}
commandName := work.Cmd.Name
if commandName == "" {
return nil, errors.New(fmt.Sprintf("error: command name is empty"))
}
cmd := exec.Command(commandName, args...)
msg := fmt.Sprintf("worker: start cmd=%s, args=%v", commandName, args)
fmt.Println(msg)
logger.Debug(1, msg)
logger.Event(event.WORK_START, "workid="+work.Id,
"cmd="+commandName,
fmt.Sprintf("args=%v", args))
var stdout, stderr io.ReadCloser
if conf.PRINT_APP_MSG {
stdout, err = cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err = cmd.StderrPipe()
if err != nil {
return nil, err
}
}
stdoutFilePath := fmt.Sprintf("%s/%s", work.Path(), conf.STDOUT_FILENAME)
stderrFilePath := fmt.Sprintf("%s/%s", work.Path(), conf.STDERR_FILENAME)
outfile, err := os.Create(stdoutFilePath)
defer outfile.Close()
errfile, err := os.Create(stderrFilePath)
defer errfile.Close()
out_writer := bufio.NewWriter(outfile)
defer out_writer.Flush()
err_writer := bufio.NewWriter(errfile)
defer err_writer.Flush()
if conf.PRINT_APP_MSG {
go io.Copy(out_writer, stdout)
go io.Copy(err_writer, stderr)
}
if err := cmd.Start(); err != nil {
return nil, errors.New(fmt.Sprintf("start_cmd=%s, err=%s", commandName, err.Error()))
}
var MaxMem uint64 = 0
done := make(chan error)
memcheck_done := make(chan bool)
go func() {
done <- cmd.Wait()
memcheck_done <- true
}()
mem_check_interval_here := conf.MEM_CHECK_INTERVAL
if mem_check_interval_here == 0 {
mem_check_interval_here = 10 * time.Second
}
go func() {
mstats := new(runtime.MemStats)
runtime.ReadMemStats(mstats)
MaxMem = mstats.Alloc
time.Sleep(2 * time.Second)
for {
select {
default:
mstats := new(runtime.MemStats)
runtime.ReadMemStats(mstats)
if mstats.Alloc > MaxMem {
MaxMem = mstats.Alloc
}
time.Sleep(mem_check_interval_here)
case <-memcheck_done:
return
}
}
}()
select {
case <-chankill:
if err := cmd.Process.Kill(); err != nil {
fmt.Println("failed to kill" + err.Error())
}
<-done // allow goroutine to exit
fmt.Println("process killed")
return nil, errors.New("process killed")
case err := <-done:
if err != nil {
//.........這裏部分代碼省略.........
示例3: RunWorkunitDocker
func RunWorkunitDocker(work *core.Workunit) (pstats *core.WorkPerf, err error) {
pstats = new(core.WorkPerf)
pstats.MaxMemUsage = -1
pstats.MaxMemoryTotalRss = -1
pstats.MaxMemoryTotalSwap = -1
args := work.Cmd.ParsedArgs
//change cwd to the workunit's working directory
if err := work.CDworkpath(); err != nil {
return nil, err
}
docker_preparation_start := time.Now().Unix()
commandName := work.Cmd.Name
use_wrapper_script := false
wrapper_script_filename := "awe_workunit_wrapper.sh"
wrapper_script_filename_host := path.Join(work.Path(), wrapper_script_filename)
wrapper_script_filename_docker := path.Join(conf.DOCKER_WORK_DIR, wrapper_script_filename)
if len(work.Cmd.Cmd_script) > 0 {
use_wrapper_script = true
// create wrapper script
//conf.DOCKER_WORK_DIR
var wrapper_content_string = "#!/bin/bash\n" + strings.Join(work.Cmd.Cmd_script, "\n") + "\n"
logger.Debug(1, fmt.Sprintf("write wrapper script: %s\n%s", wrapper_script_filename_host, strings.Join(work.Cmd.Cmd_script, ", ")))
var wrapper_content_bytes = []byte(wrapper_content_string)
err = ioutil.WriteFile(wrapper_script_filename_host, wrapper_content_bytes, 0755) // not executable: 0644
if err != nil {
return nil, errors.New(fmt.Sprintf("error writing wrapper script, err=%s", err.Error()))
}
}
//cmd := exec.Command(commandName, args...)
container_name := "AWE_workunit"
Dockerimage := work.Cmd.Dockerimage
if work.App != nil && work.App.Name != "" {
Dockerimage = work.App.AppDef.Dockerimage
}
if Dockerimage == "" {
return nil, errors.New(fmt.Sprintf("Error Dockerimage string empty"))
}
logger.Debug(1, fmt.Sprintf("Dockerimage: %s", Dockerimage))
use_docker_api := true
if conf.DOCKER_BINARY != "API" {
use_docker_api = false
}
var client *docker.Client = nil
if use_docker_api {
logger.Debug(1, fmt.Sprintf("Using docker API..."))
client, err = docker.NewClient(conf.DOCKER_SOCKET)
if err != nil {
return nil, errors.New(fmt.Sprintf("error creating docker client", err.Error()))
}
} else {
logger.Debug(1, fmt.Sprintf("Using docker docker binary..."))
}
//imgs, _ := client.ListImages(false)
//for _, img := range imgs {
// spew.Dump(img)
//}
// delete any old AWE_container
err = RemoveOldAWEContainers(client, container_name)
if err != nil {
return nil, err
}
//var node *core.ShockNode = nil
// find image in repo (e.g. extract docker image id)
node, dockerimage_download_url, err := findDockerImageInShock(Dockerimage, work.Info.DataToken)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error getting docker url, err=%s", err.Error()))
}
// TODO attr_json, _ := json.Marshal(node.Attributes) might be better
node_attr_map, ok := node.Attributes.(map[string]interface{})
if !ok {
return nil, errors.New(fmt.Sprintf("(1) could not type assert Shock_Dockerimage_attributes, Dockerimage=%s", Dockerimage))
}
dockerimage_id, ok := node_attr_map["id"].(string)
if !ok {
return nil, errors.New(fmt.Sprintf("(2) could not type assert Shock_Dockerimage_attributes, Dockerimage=%s", Dockerimage))
//.........這裏部分代碼省略.........
示例4: RunWorkunitDocker
func RunWorkunitDocker(work *core.Workunit) (pstats *core.WorkPerf, err error) {
pstats = new(core.WorkPerf)
pstats.MaxMemUsage = -1
pstats.MaxMemoryTotalRss = -1
pstats.MaxMemoryTotalSwap = -1
args := work.Cmd.ParsedArgs
//change cwd to the workunit's working directory
if err := work.CDworkpath(); err != nil {
return nil, err
}
commandName := work.Cmd.Name
use_wrapper_script := false
wrapper_script_filename := "awe_workunit_wrapper.sh"
wrapper_script_filename_host := path.Join(work.Path(), wrapper_script_filename)
wrapper_script_filename_docker := path.Join(conf.DOCKER_WORK_DIR, wrapper_script_filename)
if strings.HasPrefix(commandName, "app:") {
if len(work.Cmd.ParsedArgs) > 0 {
use_wrapper_script = true
// create wrapper script
//conf.DOCKER_WORK_DIR
var wrapper_content_string = "#!/bin/bash\n" + strings.Join(work.Cmd.Cmd_script, "\n") + "\n"
logger.Debug(1, fmt.Sprintf("write wrapper script: %s\n%s", wrapper_script_filename_host, strings.Join(work.Cmd.Cmd_script, ", ")))
var wrapper_content_bytes = []byte(wrapper_content_string)
err = ioutil.WriteFile(wrapper_script_filename_host, wrapper_content_bytes, 0644)
if err != nil {
return nil, errors.New(fmt.Sprintf("error writing wrapper script, err=%s", err.Error()))
}
}
}
//cmd := exec.Command(commandName, args...)
container_name := "AWE_workunit"
Dockerimage := work.Cmd.Dockerimage
logger.Debug(1, fmt.Sprintf("Dockerimage: %s", Dockerimage))
endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)
if err != nil {
return nil, errors.New(fmt.Sprintf("error creating docker client", err.Error()))
}
//imgs, _ := client.ListImages(false)
//for _, img := range imgs {
// spew.Dump(img)
//}
// delete any old AWE_container
err = RemoveOldAWEContainers(client, container_name)
if err != nil {
return nil, err
}
//var node *core.ShockNode = nil
// find image in repo (e.g. extract docker image id)
node, dockerimage_download_url, err := findDockerImageInShock(Dockerimage)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error getting docker url, err=%s", err.Error()))
}
// TODO attr_json, _ := json.Marshal(node.Attributes) might be better
node_attr_map, ok := node.Attributes.(map[string]interface{})
if !ok {
return nil, errors.New(fmt.Sprintf("(1) could not type assert Shock_Dockerimage_attributes, Dockerimage=%s", Dockerimage))
}
dockerimage_id, ok := node_attr_map["id"].(string)
if !ok {
return nil, errors.New(fmt.Sprintf("(2) could not type assert Shock_Dockerimage_attributes, Dockerimage=%s", Dockerimage))
}
if dockerimage_id == "" {
return nil, errors.New(fmt.Sprintf("Id of Dockerimage=%s not found", Dockerimage))
}
logger.Debug(1, fmt.Sprintf("using dockerimage id %s instead of name %s ", dockerimage_id, Dockerimage))
// *** find/inspect image
image, err := client.InspectImage(dockerimage_id)
if err != nil {
logger.Debug(1, fmt.Sprintf("docker image %s is not yet in local repository", Dockerimage))
image_retrieval := "load" // TODO only load is guaraneed to work
switch {
case image_retrieval == "load":
//.........這裏部分代碼省略.........