Skip to content

Commit e3b6466

Browse files
enhance helloworld image to support fleets
1 parent 1a89e30 commit e3b6466

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

helloworld/helloworld.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,16 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) {
167167
}
168168

169169
func IsJob() bool {
170-
// If we're being run as a Batch Jobthen the JOB_INDEX env var will be set.
170+
// If we're being run as a Batch Job then the JOB_INDEX env var will be set.
171+
// If we're being run as a Fleet then the CE_TASK_INDEX or CE_TASK_ID env var will be set.
171172
return os.Getenv("JOB_INDEX") != ""
172173
}
173174

175+
func IsFleet() bool {
176+
// If we're being run as a Fleet then the CE_TASK_INDEX or CE_TASK_ID env var will be set.
177+
return os.Getenv("CE_TASK_INDEX") != "" || os.Getenv("CE_TASK_ID") != ""
178+
}
179+
174180
func AppURL() string {
175181
return fmt.Sprintf("%s.%s.%s", os.Getenv("CE_APP"), os.Getenv("CE_SUBDOMAIN"), os.Getenv("CE_DOMAIN"))
176182
}
@@ -292,9 +298,8 @@ func main() {
292298
Debug(false, "Envs:\n%s", strings.Join(envs, "\n"))
293299

294300
// Real work.
295-
// If we're being run as a Batch Job just print the message to stdout.
296-
// Otherwise we're an App and we need to start the HTTP server
297-
// to processing incoming requests
301+
// If run as part of a Batch Job or Fleet it just prints the message to stdout.
302+
// Otherwise run as part of an App and start the HTTP server to processing incoming requests
298303
if IsJob() {
299304
// Jobs can be either started in 'task' mode and run to completion or in 'daemon' mode which
300305
jobMode := os.Getenv("JOB_MODE")
@@ -330,6 +335,31 @@ func main() {
330335
break
331336
}
332337
}
338+
} else if IsFleet() {
339+
// Fleet tasks run to completion
340+
341+
fleetName := os.Getenv("CE_FLEET_NAME")
342+
343+
sleep := os.Getenv("SLEEP")
344+
sleepDuration := 0
345+
if sleep != "" {
346+
sleepDuration, _ = strconv.Atoi(sleep)
347+
}
348+
349+
// Check whether the job should sleep a while before printing the helloworld statement
350+
if sleepDuration > 0 {
351+
Debug(false, "Sleeping %ds", sleepDuration)
352+
time.Sleep(time.Duration(sleepDuration) * time.Second)
353+
}
354+
355+
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"))
356+
PrintMessage(os.Stdout, os.Getenv("SHOW") == "")
357+
358+
// If the 'CRASH' or 'FAIL' env vars are set then crash!
359+
if os.Getenv("CRASH") != "" || os.Getenv("FAIL") != "" {
360+
fmt.Printf("Crashing...")
361+
os.Exit(1)
362+
}
333363

334364
} else {
335365
srv := &http.Server{Addr: ":8080"}

0 commit comments

Comments
 (0)