diff --git a/helloworld/helloworld.go b/helloworld/helloworld.go index 3d6107b58..62f454e18 100644 --- a/helloworld/helloworld.go +++ b/helloworld/helloworld.go @@ -167,10 +167,16 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) { } func IsJob() bool { - // If we're being run as a Batch Jobthen the JOB_INDEX env var will be set. + // If we're being run as a Batch Job then the JOB_INDEX env var will be set. + // If we're being run as a Fleet then the CE_TASK_INDEX or CE_TASK_ID env var will be set. return os.Getenv("JOB_INDEX") != "" } +func IsFleet() bool { + // If we're being run as a Fleet then the CE_TASK_INDEX or CE_TASK_ID env var will be set. + return os.Getenv("CE_TASK_INDEX") != "" || os.Getenv("CE_TASK_ID") != "" +} + func AppURL() string { return fmt.Sprintf("%s.%s.%s", os.Getenv("CE_APP"), os.Getenv("CE_SUBDOMAIN"), os.Getenv("CE_DOMAIN")) } @@ -292,9 +298,8 @@ func main() { Debug(false, "Envs:\n%s", strings.Join(envs, "\n")) // Real work. - // If we're being run as a Batch Job just print the message to stdout. - // Otherwise we're an App and we need to start the HTTP server - // to processing incoming requests + // If run as part of a Batch Job or Fleet it just prints the message to stdout. + // Otherwise run as part of an App and start the HTTP server to processing incoming requests if IsJob() { // Jobs can be either started in 'task' mode and run to completion or in 'daemon' mode which jobMode := os.Getenv("JOB_MODE") @@ -330,6 +335,31 @@ func main() { break } } + } else if IsFleet() { + // Fleet tasks run to completion + + fleetName := os.Getenv("CE_FLEET_NAME") + + sleep := os.Getenv("SLEEP") + sleepDuration := 0 + if sleep != "" { + sleepDuration, _ = strconv.Atoi(sleep) + } + + // Check whether the job should sleep a while before printing the helloworld statement + if sleepDuration > 0 { + Debug(false, "Sleeping %ds", sleepDuration) + time.Sleep(time.Duration(sleepDuration) * time.Second) + } + + fmt.Printf("Hello from helloworld! I'm a task of fleet: %s! Task Index: %s, Task ID: %s\n\n", fleetName, os.Getenv("CE_TASK_INDEX"), os.Getenv("CE_TASK_ID")) + PrintMessage(os.Stdout, os.Getenv("SHOW") == "") + + // If the 'CRASH' or 'FAIL' env vars are set then crash! + if os.Getenv("CRASH") != "" || os.Getenv("FAIL") != "" { + fmt.Printf("Crashing...") + os.Exit(1) + } } else { srv := &http.Server{Addr: ":8080"}