fix(quick-261119-kj8): sweep the JWT blacklist from a scheduled user:jwt-blacklist-sweep command
Boot started a ticker goroutine inside lagoon.OnDatabase that nothing stopped, so every booting process (serve, each console command, each test boot) leaked one and swept the blacklist once per process. The sweep is now the console command user:jwt-blacklist-sweep, which the conga scheduler runs on the job worker every golem15.user.jwt.blacklist_sweep_interval (default 10m, 0 for none), once per period across instances. An interval the scheduler cannot run (unparsable, negative, under 1s, not dividing 24h) now fails boot.
This commit is contained in:
55
console/jwt_blacklist_sweep.go
Normal file
55
console/jwt_blacklist_sweep.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-user-plugin/classes"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||
)
|
||||
|
||||
// JWTBlacklistSweep deletes the expired rows of the JWT blacklist. It uses
|
||||
// the blacklist store the plugin published or, when it runs as a console
|
||||
// command without a published database, a store over a database opened from
|
||||
// the config for the duration of the run.
|
||||
func JWTBlacklistSweep(app *backpack.App) bonfire.Command {
|
||||
return bonfire.Command{
|
||||
Name: "user:jwt-blacklist-sweep",
|
||||
Description: "Delete the expired JWT blacklist rows",
|
||||
Long: `Deletes the jwt_blacklist rows whose expires_at has passed. A row that
|
||||
still blocks a token or its refresh is not touched, so running it twice
|
||||
changes nothing.
|
||||
|
||||
The scheduler runs it every golem15.user.jwt.blacklist_sweep_interval on
|
||||
the job worker's scheduled queue. Run it by hand or from cron when no
|
||||
worker runs that queue. It ends with "Deleted the expired JWT blacklist
|
||||
rows."`,
|
||||
Examples: []bonfire.Example{
|
||||
{Command: "user:jwt-blacklist-sweep", Description: "Delete the expired JWT blacklist rows"},
|
||||
{Command: "user:jwt-blacklist-sweep -q", Description: "Run it quietly, printing only the result line"},
|
||||
},
|
||||
Run: func(ctx context.Context, _ bonfire.Input, out bonfire.Output) error {
|
||||
if app == nil {
|
||||
return fmt.Errorf("user:jwt-blacklist-sweep: app is nil")
|
||||
}
|
||||
store, ok := app.Lookup[bouncer.BlacklistStore]()
|
||||
if !ok || store == nil {
|
||||
sqlDB, _, err := lagoon.OpenFromApp(ctx, app)
|
||||
if err != nil {
|
||||
return fmt.Errorf("user:jwt-blacklist-sweep: %w", err)
|
||||
}
|
||||
defer sqlDB.Close()
|
||||
store = bouncer.NewPostgresBlacklist(sqlDB, classes.JWTBlacklistTable)
|
||||
}
|
||||
if err := store.Sweep(ctx, time.Now()); err != nil {
|
||||
return fmt.Errorf("user:jwt-blacklist-sweep: %w", err)
|
||||
}
|
||||
out.Success("Deleted the expired JWT blacklist rows.")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user