-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add OnStart hook for synchronous startup jobs #2079
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
Open
HeerakKashyap
wants to merge
24
commits into
gofr-dev:development
Choose a base branch
from
HeerakKashyap:fix/onstart-di-context
base: development
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.
+189
−6
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8202ed1
feat: improve OnStart hook with DI context, update docs, and ensure c…
0cb726b
refactor: improve OnStart hook context, move logic to method, and tes…
8526959
feat(onstart): implement all reviewer feedback
2cbe7e1
Merge branch 'development' into fix/onstart-di-context
HeerakKashyap 79f0fa5
Merge branch 'development' into fix/onstart-di-context
Umang01-hash 2cf9cbf
Merge branch 'development' into fix/onstart-di-context
HeerakKashyap 9fd31d4
fix: improve startup error handling for OnStart hooks (graceful shutd…
7c38332
Merge branch 'fix/onstart-di-context' of https://github.com/HeerakKas…
27ca38f
fix: address linter errors in OnStart tests
463b194
fix: remove os.Exit call to resolve deep-exit linter error
9e8bcdf
fix: resolve remaining linter errors
55d94eb
refactor: remove newContextForHooks and reuse existing newContext fun…
e13b8cd
fix: resolve linting errors and refactor Run() method for better main…
dd95596
fix: resolve remaining linting issues in TestApp_OnStart
60b6ca9
fix: resolve godot and wsl linter errors in run.go and gofr_test.go
6b9639b
Update go.work.sum
HeerakKashyap 8c6a4b6
fix: restore comments as requested by reviewer
e950953
Update run.go
HeerakKashyap bada9a7
Update run.go
HeerakKashyap 9f2b697
Merge branch 'development' into fix/onstart-di-context
Umang01-hash 2767521
merge: resolve conflicts and fix formatting
eddd360
Merge branch 'development' into fix/onstart-di-context
Umang01-hash d21b836
Merge branch 'development' into fix/onstart-di-context
HeerakKashyap 0e871e8
Merge branch 'development' into fix/onstart-di-context
Umang01-hash 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 hidden or 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,55 @@ | ||
# Startup Hooks | ||
|
||
GoFr provides a way to run synchronous jobs when your application starts, before any servers begin handling requests. This is useful for tasks like seeding a database, warming up a cache, or performing other critical setup procedures. | ||
|
||
## `OnStart` | ||
|
||
You can register a startup hook using the `a.OnStart()` method on your `app` instance. | ||
|
||
### Usage | ||
|
||
The method accepts a function with the signature `func(ctx *gofr.Context) error`. | ||
|
||
- The `*gofr.Context` passed to the hook is fully initialized and provides access to all dependency-injection-managed services (e.g., `ctx.Container.SQL`, `ctx.Container.Redis`). | ||
- If any `OnStart` hook returns an error, the application will log the error and refuse to start. | ||
|
||
### Example: Warming up a Cache | ||
|
||
Here is an example of using `OnStart` to set an initial value in a Redis cache when the application starts. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"gofr.dev/pkg/gofr" | ||
) | ||
|
||
func main() { | ||
a := gofr.New() | ||
|
||
// Register an OnStart hook to warm up a cache. | ||
a.OnStart(func(ctx *gofr.Context) error { | ||
ctx.Container.Logger.Info("Warming up the cache...") | ||
|
||
// In a real app, this might come from a database or another service. | ||
cacheKey := "initial-data" | ||
cacheValue := "This is some data cached at startup." | ||
|
||
err := ctx.Redis.Set(ctx, cacheKey, cacheValue, 0).Err() | ||
if err != nil { | ||
ctx.Container.Logger.Errorf("Failed to warm up cache: %v", err) | ||
return err // Return the error to halt startup if caching fails. | ||
} | ||
|
||
ctx.Container.Logger.Info("Cache warmed up successfully!") | ||
|
||
return nil | ||
}) | ||
|
||
// ... register your routes | ||
|
||
a.Run() | ||
} | ||
``` | ||
|
||
This ensures that critical startup tasks are completed successfully before the application begins accepting traffic. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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'm unsure why this was changed. As if code using a different logger was added
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 was building a new docker container, it automatically changes if u run so "go build " kinds of prompts
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.
@HeerakKashyap Please reomve these changes. There should be no changes to
go.work.sum
file.