Skip to content

Commit

Permalink
Implement graceful exit
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr265 committed Jan 29, 2023
1 parent f18863c commit 446f7c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ConfigPrintd struct {
TabSize int
KeepPDF bool
DelayAfter time.Duration
DelayError time.Duration
}

func (c *ConfigPrintd) initDefaults() {
Expand All @@ -42,6 +43,7 @@ func (c *ConfigPrintd) initDefaults() {
c.TabSize = 4
c.KeepPDF = true
c.DelayAfter = 500 * time.Millisecond
c.DelayError = 5 * time.Second
}

type ConfigPrinter struct {
Expand Down
70 changes: 54 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)

Expand Down Expand Up @@ -35,27 +39,61 @@ func main() {
catch(err)
validateConfig(cfg)

go pulseLoop(cfg)
wg := sync.WaitGroup{}
exitch := make(chan struct{})

for {
log.Println("Waiting for prints")
pr, err := getNextPrint(ctx, cfg)
if errors.As(err, &tophError{}) {
log.Println(err)
time.Sleep(5 * time.Second)
continue
wg.Add(1)
go func() {
defer wg.Done()
pulseLoop(cfg, exitch)
}()

wg.Add(1)
go func() {
defer wg.Done()
delay := 0 * time.Second
L:
for {
log.Println("Waiting for prints")
pr, err := getNextPrint(ctx, cfg)
if errors.As(err, &tophError{}) {
log.Println(err)
delay = cfg.Printd.DelayError
goto retry
}
catch(err)

log.Printf("Printing %s", pr.ID)
err = runPrintJob(ctx, cfg, pr)
catch(err)
err = markPrintDone(ctx, cfg, pr)
catch(err)
log.Printf(".. Done")

delay = cfg.Printd.DelayAfter

retry:
select {
case <-exitch:
break L
case <-time.After(delay):
}
}
catch(err)
}()

log.Printf("Printing %s", pr.ID)
err = runPrintJob(ctx, cfg, pr)
catch(err)
err = markPrintDone(ctx, cfg, pr)
catch(err)
log.Printf(".. Done")
sigch := make(chan os.Signal, 2)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)

time.Sleep(cfg.Printd.DelayAfter)
select {
case sig := <-sigch:
log.Printf("Received %s", sig)
}

log.Println("Exiting")
close(exitch)
wg.Wait()

log.Println("Goodbye")
}

func catch(err error) {
Expand Down
18 changes: 12 additions & 6 deletions pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import (
"time"
)

func pulseLoop(cfg Config) {
func pulseLoop(cfg Config, exitch chan struct{}) {
L:
for {
var resp *http.Response
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/printd/pulse?contest=%s", cfg.Toph.BaseURL, cfg.Toph.ContestID), nil)
req.Header.Add("Authorization", "Printd "+cfg.Toph.Token)
if err != nil {
log.Println(err)
continue
goto retry
}
resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
time.Sleep(5 * time.Second)
continue
goto retry
}
resp.Body.Close()

time.Sleep(5 * time.Second)
retry:
select {
case <-exitch:
break L
case <-time.After(5 * time.Second):
}
}
}

0 comments on commit 446f7c3

Please sign in to comment.