-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document ways to cleanup expired sessions #3221
Open
pujux
wants to merge
4
commits into
vendure-ecommerce:minor
Choose a base branch
from
pujux:feat/cleanup-expired-sessions
base: minor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,327
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
14f8d62
feat(core): Add cleanupExpiredSessions method in SessionService
pujux 94e8cae
docs: Fix docusaurus-theme-search-typesense package version
pujux 129aada
docs: Update SessionService documentation and wording fix for Session…
pujux 545eeaa
docs: Add how-to guide for creating expired session cleanup script
pujux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,49 @@ | ||
--- | ||
title: 'Expired Session Cleanup' | ||
--- | ||
|
||
# Expired Session Cleanup | ||
|
||
As noted in [SessionService](/reference/typescript-api/services/session-service), sessions are not automatically deleted when expired. This means that if you have a large number of sessions, you may need to clean them up periodically to avoid clogging up your database. | ||
|
||
This guide aims to demonstrate how to create [Stand-alone CLI Scripts](/guides/developer-guide/stand-alone-scripts/) to automate the process of cleaning up expired sessions. | ||
|
||
## Code | ||
|
||
This code bootstraps the Vendure Worker, then retrieves the `SessionService` and calls the `cleanupExpiredSessions` method on it, completely removing all expired sessions from the database. It can be easily run from the command-line or scheduled to run periodically. | ||
|
||
```ts title="src/expired-session-cleanup.ts" | ||
import { bootstrapWorker, Logger, SessionService, RequestContextService } from '@vendure/core'; | ||
import { config } from './vendure-config'; | ||
|
||
const loggerCtx = 'ExpiredSessionCleanup'; | ||
|
||
if (require.main === module) { | ||
cleanupExpiredSessions() | ||
.then(() => process.exit(0)) | ||
.catch(err => { | ||
Logger.error(err, loggerCtx); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
async function cleanupExpiredSessions() { | ||
Logger.info('Session cleanup started.', loggerCtx); | ||
|
||
// Bootstrap an instance of the Vendure Worker | ||
const { app } = await bootstrapWorker(config); | ||
|
||
// Retrieve the SessionService | ||
const sessionService = app.get(SessionService); | ||
|
||
// Create a RequestContext for administrative tasks | ||
const ctx = await app.get(RequestContextService).create({ | ||
apiType: 'admin', | ||
}); | ||
|
||
// Call the cleanup function | ||
await sessionService.cleanupExpiredSessions(ctx); | ||
|
||
Logger.info('Session cleanup completed.', loggerCtx); | ||
} | ||
``` |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have a more general topic of "how to run scheduled tasks" with a general explanation of the problem and solution and then the session cleanup as the main example. In future we can then add more examples like cleaning up abandoned orders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea! should I put that into the "Developer Guide" section under "Advanced Topics" just like the "Migrating from v1" topic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good idea 👍