osbuild-{composer/worker}: exit cleanly

Only panic on compile-time errors (e.g., built for unsupported
architecture). Otherwise, use log.Fatalf(), which is equivalent to
printing and exiting with return code 1. Only ever do this from
main(), in all other cases pass on the error object.

This is mostly relevant when the server disconects, in which case
we'll get EOF, and will now restart cleanly instead of panicing.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2019-12-10 20:41:58 +01:00 committed by Lars Karlitski
parent 42dfbf7da6
commit 118b185fdd
2 changed files with 16 additions and 13 deletions

View file

@ -38,11 +38,11 @@ func main() {
listeners, err := activation.Listeners()
if err != nil {
panic(err)
log.Fatalf("Could not get listening sockets: " + err.Error())
}
if len(listeners) != 2 {
panic("Unexpected number of sockets. Composer require 2 of them.")
log.Fatalf("Unexpected number of listening sockets (%d), expected 2", len(listeners))
}
weldrListener := listeners[0]
@ -52,7 +52,7 @@ func main() {
distribution, err := distro.FromHost()
if err != nil {
panic("cannot detect distro from host: " + err.Error())
log.Fatalf("Could not determine distro from host: " + err.Error())
}
var logger *log.Logger

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
@ -76,40 +77,42 @@ func (c *ComposerClient) UpdateJob(job *jobqueue.Job, status string, image *stor
return nil
}
func handleJob(client *ComposerClient, distro distro.Distro) {
func handleJob(client *ComposerClient, distro distro.Distro) error {
fmt.Println("Waiting for a new job...")
job, err := client.AddJob()
if err != nil {
panic(err)
return err
}
client.UpdateJob(job, "RUNNING", nil)
err = client.UpdateJob(job, "RUNNING", nil)
if err != nil {
return err
}
fmt.Printf("Running job %s\n", job.ID.String())
image, err, errs := job.Run(distro)
if err != nil {
client.UpdateJob(job, "FAILED", nil)
return
return client.UpdateJob(job, "FAILED", nil)
}
for _, err := range errs {
if err != nil {
client.UpdateJob(job, "FAILED", nil)
return
return client.UpdateJob(job, "FAILED", nil)
}
}
client.UpdateJob(job, "FINISHED", image)
return client.UpdateJob(job, "FINISHED", image)
}
func main() {
distro, err := distro.FromHost()
if err != nil {
panic(err)
log.Fatalf("Could not determine distro from host: " + err.Error())
}
client := NewClient()
for {
handleJob(client, distro)
err = handleJob(client, distro)
log.Fatalf("Failed to handle job: " + err.Error())
}
}