Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove expired data from the database, ISSUE-952
- Loading branch information
Showing
23 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cleanup | ||
|
||
import ( | ||
cx "context" | ||
"time" | ||
|
||
"github.com/ory/graceful" | ||
"github.com/ory/kratos/driver" | ||
) | ||
|
||
func BackgroundCleanup(ctx cx.Context, r driver.Registry) { | ||
ctx, cancel := cx.WithCancel(ctx) | ||
|
||
r.Logger().Println("Cleanup worker started.") | ||
if err := graceful.Graceful(func() error { | ||
for { | ||
select { | ||
case <-time.After(r.Config(ctx).DatabaseCleanupSleepBackground()): | ||
err := r.Persister().CleanupDatabase(ctx, r.Config(ctx).DatabaseCleanupSleepTables()) | ||
r.Logger().Error(err) | ||
case <-ctx.Done(): | ||
return nil | ||
} | ||
} | ||
}, func(_ cx.Context) error { | ||
cancel() | ||
return nil | ||
}); err != nil { | ||
r.Logger().WithError(err).Fatalf("Failed to run cleanup worker.") | ||
} | ||
|
||
r.Logger().Println("Background cleanup worker was shutdown gracefully.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cleanup | ||
|
||
import ( | ||
"github.com/ory/x/configx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCleanupCmd() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "cleanup", | ||
Short: "Various cleanup helpers", | ||
} | ||
configx.RegisterFlags(c.PersistentFlags()) | ||
return c | ||
} | ||
|
||
func RegisterCommandRecursive(parent *cobra.Command) { | ||
c := NewCleanupCmd() | ||
parent.AddCommand(c) | ||
c.AddCommand(NewCleanupSQLCmd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright © 2019 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cleanup | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ory/kratos/driver/config" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/kratos/cmd/cliclient" | ||
"github.com/ory/x/configx" | ||
) | ||
|
||
// cleanupSqlCmd represents the sql command | ||
func NewCleanupSQLCmd() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "sql <database-url>", | ||
Short: "Cleanup sql database from expired flows and sessions", | ||
Long: `Run this command as frequently as you need. | ||
It is recommended to run this command close to the SQL instance (e.g. same subnet) instead of over the public internet. | ||
This decreases risk of failure and decreases time required. | ||
You can read in the database URL using the -e flag, for example: | ||
export DSN=... | ||
kratos cleanup sql -e | ||
### WARNING ### | ||
Before running this command on an existing database, create a back up! | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cliclient.NewCleanupHandler().CleanupSQL(cmd, args) | ||
}, | ||
} | ||
|
||
configx.RegisterFlags(c.PersistentFlags()) | ||
c.Flags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.") | ||
c.Flags().IntP(config.ViperKeyDatabaseCleanupBatchSize, "b", 100, "Set the number of records to be cleaned per run") | ||
c.Flags().Duration(config.ViperKeyDatabaseCleanupSleepBackground, 30*time.Minute, "How long to wait between each cleanup run") | ||
c.Flags().Duration(config.ViperKeyDatabaseCleanupSleepTables, time.Minute, "How long to wait between each table cleanup") | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.