Debian Codegraph @

cmdline/workercmd/workercmd.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package workercmd
18
19 import (
20 "context"
21 "fmt"
22 "io"
23 "net/http"
24 "os"
25 "os/signal"
26 "sync"
27 "syscall"
28 "time"
29
30 "github.com/rs/zerolog"
31 "github.com/rs/zerolog/log"
32 "github.com/spf13/cobra"
33
34 "github.com/sassoftware/relic/v7/cmdline/shared"
35 "github.com/sassoftware/relic/v7/internal/activation"
36 "github.com/sassoftware/relic/v7/internal/activation/activatecmd"
37 "github.com/sassoftware/relic/v7/internal/zhttp"
38 "github.com/sassoftware/relic/v7/token/open"
39 "github.com/sassoftware/relic/v7/token/tokencache"
40 )
41
42 func init() {
43 AddWorkerCommand(shared.RootCmd)
44 }
45
46 func AddWorkerCommand(parent *cobra.Command) {
47 wc := &cobra.Command{
48 Use: "worker",
49 Run: worker,
50 Hidden: true,
51 }
52 parent.AddCommand(wc)
53 }
54
55 func worker(cmd *cobra.Command, args []string) {
56 if len(args) != 2 {
57 log.Fatal().Msg("invalid argument")
58 }
59 shared.ArgConfig = args[0]
60 tokenName := args[1]
61 if err := runWorker(tokenName); err != nil {
62 log.Fatal().Msgf("worker stopping for token %s", tokenName)
63 }
64 }
65
66 func runWorker(tokenName string) error {
67 if err := shared.InitConfig(); err != nil {
68 return err
69 }
70 cookie, err := io.ReadAll(os.Stdin)
71 if err != nil {
72 return err
73 }
74 lis, err := activation.GetListener(0, "tcp", "")
75 if err != nil {
76 return err
77 }
78 cfg := shared.CurrentConfig
79 tok, err := open.Token(cfg, tokenName, nil)
80 if err != nil {
81 return err
82 }
83 if err := zhttp.SetupLogging(cfg.Server.LogLevel, cfg.Server.LogFile); err != nil {
84 return fmt.Errorf("configuring logging: %w", err)
85 }
86 log.Logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
87 return c.Str("token", tokenName).Int("pid", os.Getpid())
88 })
89 tconf := tok.Config()
90 if tconf.RateLimit != 0 {
91 tok = tokencache.NewLimiter(tok, tconf.RateLimit, tconf.RateBurst)
92 }
93 expiry := time.Second * time.Duration(cfg.Server.TokenCacheSeconds)
94 handler := &handler{
95 token: tokencache.New(tok, expiry),
96 cookie: cookie,
97 }
98 srv := &http.Server{Handler: handler}
99 wg := new(sync.WaitGroup)
100 handler.shutdown = func() {
101 // keep the main goroutine from exiting until all requests are served
102 wg.Add(1)
103 // notify parent that this process is doomed and to start another one
104 _ = activatecmd.DaemonStopping()
105 // stop accepting requests and wait for ongoing ones to finish
106 _ = srv.Shutdown(context.Background())
107 wg.Done()
108 }
109 go handler.watchSignals()
110 go handler.healthCheck()
111 _ = activation.DaemonReady()
112 err = srv.Serve(lis)
113 if err == http.ErrServerClosed {
114 err = nil
115 }
116 wg.Wait() // wait for shutdown to finish
117 return err
118 }
119
120 func (h *handler) watchSignals() {
121 ch := make(chan os.Signal, 1)
122 signal.Notify(ch,
123 syscall.SIGINT,
124 syscall.SIGTERM,
125 syscall.SIGQUIT,
126 )
127 <-ch
128 signal.Stop(ch)
129 h.shutdown()
130 }