snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
27
cmd/courier/root.go
Normal file
27
cmd/courier/root.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package courier
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/ory/kratos/driver"
|
||||
"github.com/ory/x/configx"
|
||||
)
|
||||
|
||||
// NewCourierCmd creates a new courier command
|
||||
func NewCourierCmd() *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "courier",
|
||||
Short: "Commands related to the Ory Kratos message courier",
|
||||
}
|
||||
configx.RegisterFlags(c.PersistentFlags())
|
||||
return c
|
||||
}
|
||||
|
||||
func RegisterCommandRecursive(parent *cobra.Command, dOpts []driver.RegistryOption) {
|
||||
c := NewCourierCmd()
|
||||
parent.AddCommand(c)
|
||||
c.AddCommand(NewWatchCmd(dOpts))
|
||||
}
|
||||
108
cmd/courier/watch.go
Normal file
108
cmd/courier/watch.go
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package courier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/urfave/negroni"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/ory/graceful"
|
||||
"github.com/ory/kratos/driver"
|
||||
"github.com/ory/x/configx"
|
||||
"github.com/ory/x/prometheusx"
|
||||
"github.com/ory/x/reqlog"
|
||||
)
|
||||
|
||||
func NewWatchCmd(dOpts []driver.RegistryOption) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "watch",
|
||||
Short: "Starts the Ory Kratos message courier",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
r, err := driver.New(cmd.Context(), cmd.ErrOrStderr(), append(dOpts, driver.WithConfigOptions(configx.WithFlags(cmd.Flags())))...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return StartCourier(cmd.Context(), r)
|
||||
},
|
||||
}
|
||||
c.PersistentFlags().Int("expose-metrics-port", 0, "The port to expose the metrics endpoint on (not exposed by default)")
|
||||
return c
|
||||
}
|
||||
|
||||
func StartCourier(ctx context.Context, r driver.Registry) error {
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
if port := r.Config().CourierExposeMetricsPort(ctx); port != 0 {
|
||||
eg.Go(func() error {
|
||||
return ServeMetrics(ctx, r, port)
|
||||
})
|
||||
}
|
||||
|
||||
eg.Go(func() error {
|
||||
return Watch(ctx, r)
|
||||
})
|
||||
|
||||
return eg.Wait()
|
||||
}
|
||||
|
||||
func ServeMetrics(ctx context.Context, r driver.Registry, port int) error {
|
||||
cfg := r.Config().ServeAdmin(ctx)
|
||||
l := r.Logger()
|
||||
|
||||
recovery := negroni.NewRecovery()
|
||||
recovery.Logger = l
|
||||
|
||||
n := negroni.New(
|
||||
recovery,
|
||||
reqlog.NewMiddlewareFromLogger(l, "admin#"+cfg.BaseURL.String()),
|
||||
)
|
||||
|
||||
router := http.NewServeMux()
|
||||
router.Handle(prometheusx.MetricsPrometheusPath, prometheusx.Handler())
|
||||
n.UseHandler(router)
|
||||
|
||||
var handler http.Handler = n
|
||||
|
||||
//#nosec G112 -- the correct settings are set by graceful.WithDefaults
|
||||
server := graceful.WithDefaults(&http.Server{
|
||||
Addr: configx.GetAddress(cfg.Host, port),
|
||||
Handler: handler,
|
||||
})
|
||||
|
||||
l.WithField("addr", server.Addr).Info("Starting the metrics httpd")
|
||||
if err := graceful.GracefulContext(ctx, server.ListenAndServe, server.Shutdown); err != nil {
|
||||
l.Errorln("Failed to gracefully shutdown metrics httpd")
|
||||
return err
|
||||
}
|
||||
l.Println("Metrics httpd was shutdown gracefully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func Watch(ctx context.Context, r driver.Registry) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
r.Logger().Println("Courier worker started.")
|
||||
if err := graceful.Graceful(func() error {
|
||||
c, err := r.Courier(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Work(ctx)
|
||||
}, func(_ context.Context) error {
|
||||
cancel()
|
||||
return nil
|
||||
}); err != nil {
|
||||
r.Logger().WithError(err).Error("Failed to run courier worker.")
|
||||
return err
|
||||
}
|
||||
|
||||
r.Logger().Println("Courier worker was shutdown gracefully.")
|
||||
return nil
|
||||
}
|
||||
47
cmd/courier/watch_test.go
Normal file
47
cmd/courier/watch_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package courier
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/phayes/freeport"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/kratos/pkg"
|
||||
"github.com/ory/x/configx"
|
||||
)
|
||||
|
||||
func TestStartCourier(t *testing.T) {
|
||||
t.Run("case=without metrics", func(t *testing.T) {
|
||||
_, r := pkg.NewFastRegistryWithMocks(t)
|
||||
go func() { _ = StartCourier(t.Context(), r) }()
|
||||
require.Equal(t, r.Config().CourierExposeMetricsPort(t.Context()), 0)
|
||||
})
|
||||
|
||||
t.Run("case=with metrics", func(t *testing.T) {
|
||||
port, err := freeport.GetFreePort()
|
||||
require.NoError(t, err)
|
||||
_, r := pkg.NewFastRegistryWithMocks(t, configx.WithValue("expose-metrics-port", port))
|
||||
go func() { _ = StartCourier(t.Context(), r) }()
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.Cleanup(transport.CloseIdleConnections)
|
||||
client := &http.Client{Transport: transport}
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/metrics/prometheus", port)
|
||||
require.EventuallyWithT(t, func(t *assert.CollectT) {
|
||||
res, err := client.Get(url)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
assert.NoError(t, res.Body.Close())
|
||||
}()
|
||||
assert.Equal(t, 200, res.StatusCode)
|
||||
}, 10*time.Second, 10*time.Millisecond)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user