Skip to content

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
wants to merge 23 commits into
base: development
Choose a base branch
from

Conversation

HeerakKashyap
Copy link

@HeerakKashyap HeerakKashyap commented Jul 19, 2025

Description:

  • Implements an OnStart hook for the GoFr framework, allowing users to register synchronous startup jobs that execute before the server starts.
  • The hook receives a fully initialized *gofr.Context with all DI-managed services, as requested by maintainers.
  • Removes any previous custom context logic and follows the GoFr way for dependency injection.
  • Updates documentation and provides usage examples.
  • Includes proof of working feature (see attached screenshots).

Testing:

  • Verified that OnStart hooks can access DI-managed services (e.g., SQL, Redis).
  • All relevant tests pass with MySQL and Redis running in Docker.

Fixes #1833


Let me know if you want to add or change anything, or if you need help attaching screenshots or filling out the checklist!

Copy link
Contributor

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

Copy link
Author

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

Copy link
Member

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.

Copy link
Member

@Umang01-hash Umang01-hash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There are no tests added for the newly added code.
  • No documentation updates for the new feature.
  • We have following code quality/linter issues in your PR:
Screenshot 2025-07-21 at 4 33 49 PM

Please resolve them along with other review comments.
Thankyou

@HeerakKashyap
Copy link
Author

HeerakKashyap commented Jul 21, 2025

  • There are no tests added for the newly added code.
  • No documentation updates for the new feature.
  • We have following code quality/linter issues in your PR:
Screenshot 2025-07-21 at 4 33 49 PM Please resolve them along with other review comments. Thankyou

should i update iin readme.md , it would be okay to do so right ? @Umang01-hash

@Umang01-hash
Copy link
Member

  • There are no tests added for the newly added code.
  • No documentation updates for the new feature.
  • We have following code quality/linter issues in your PR:
Screenshot 2025-07-21 at 4 33 49 PM Please resolve them along with other review comments. Thankyou

should i update iin readme.md , it would be okay to do so right ? @Umang01-hash

@HeerakKashyap I think we can update the existing docs or maybe create a new one for the same. Both can work depending upon the content we are sharing.

- Move OnStart logic to a dedicated, testable method.
- Fix all linter issues (deep-exit and cyclomatic complexity).
- Add unit tests for success and error cases.
- Add comprehensive documentation with a realistic example.
- Update the example app to demonstrate a real-world use case.
@HeerakKashyap HeerakKashyap force-pushed the fix/onstart-di-context branch from 57c4743 to 8526959 Compare July 21, 2025 14:38
@HeerakKashyap
Copy link
Author

HeerakKashyap commented Jul 21, 2025

ss for testing Test case are working fine @Umang01-hash @ccoVeille

@HeerakKashyap HeerakKashyap changed the title feat: add OnStart hook for synchronous startup jobs (closes #1833) feat: add OnStart hook for synchronous startup jobs Jul 22, 2025
@HeerakKashyap
Copy link
Author

kidnly have a look @ccoVeille @Umang01-hash

@ccoVeille
Copy link
Contributor

The code is in, but I would prefer if the context was not forged twice.

Can't we create the gofr.Context before calling startup hooks, then pass it through all the layers as argument up to the place where the gofr.Context is created now?

Or would this implies breaking changes,? Or simply too much changes?

Please note, it doesn't necessary have to be addressed in this PR.

@HeerakKashyap
Copy link
Author

The code is in, but I would prefer if the context was not forged twice.

Can't we create the gofr.Context before calling startup hooks, then pass it through all the layers as argument up to the place where the gofr.Context is created now?

Or would this implies breaking changes,? Or simply too much changes?

Please note, it doesn't necessary have to be addressed in this PR.

Thank you for the suggestion Sir! @ccoVeille

I agree that creating the gofr.Context only once and passing it through would be cleaner and more efficient.
However, implementing this would require significant refactoring and could potentially introduce breaking changes, especially if other parts of the framework expect to construct their own context.

For this PR, I’ll keep the current approach to avoid introducing breaking changes, but I think this is a great idea for future improvement and can be considered in a dedicated refactor.

@HeerakKashyap
Copy link
Author

@Umang01-hash sir, kindly run the workflows and let me know is there now any linter errors etc.

@Umang01-hash
Copy link
Member

Hey @HeerakKashyap only a few things:

  • Changes in go.work.sum file needs to be reverted.
  • We dont need a newContextForHooks methods, we can use exitisting methods to create new context.
  • A lot of linter errors are there in the code we need to resolve them.

Please do the above changes and we should be ready to go with your PR.

HeerakKashyap added 3 commits July 23, 2025 22:56
- Fix dynamic error definition
- Fix unused parameters with underscore
- Use t.Context() instead of context.Background()
- Use assert.NoError instead of assert.Nil
- Fix cuddled return statement in newContextForHooks
- Fix cuddled assignment in OnStart test
- Remove os.Exit call to resolve deep-exit error
@HeerakKashyap HeerakKashyap force-pushed the fix/onstart-di-context branch from f4882d6 to 9e8bcdf Compare July 23, 2025 17:52
@HeerakKashyap
Copy link
Author

@Umang01-hash kidnly , check the changes out

@Umang01-hash
Copy link
Member

Screenshot 2025-07-24 at @HeerakKashyap 0 53 08 AM Hey @HeerakKashyap. Please resolve the linter issues on your PR.

…tainability

- Fix err113: Replace dynamic error creation with static error in TestApp_OnStart
- Fix gocyclo: Refactor Run() method by extracting helper methods to reduce cyclomatic complexity
- Fix ws1: Correct whitespace issues in gofr_test.go and run.go
- Add helper methods: handleStartupHooks, startShutdownHandler, startTelemetryIfEnabled, startAllServers
- Improve code organization and readability while maintaining functionality
@HeerakKashyap
Copy link
Author

Screenshot 2025-07-24 at @HeerakKashyap 0 53 08 AM Hey @HeerakKashyap. Please resolve the linter issues on your PR.

i had to make couple of changes to fix the complexity issue , kindly see if this works @Umang01-hash

HeerakKashyap added 2 commits July 24, 2025 13:12
- Fix err113: Move error variable to package level to avoid dynamic error creation
- Fix error-naming: Rename hookFailedErr to errHookFailed following Go naming conventions
- Fix whitespace: Remove unnecessary blank line before app assignment
- All linting issues in modified files are now resolved
@HeerakKashyap
Copy link
Author

@Umang01-hash So this PR is ready to get merged ?

@HeerakKashyap
Copy link
Author

@Umang01-hash CAN U LET me know how can i run linter test locally, if there's some application in Golang for running linter tests

@Umang01-hash
Copy link
Member

@Umang01-hash CAN U LET me know how can i run linter test locally, if there's some application in Golang for running linter tests

Hey @HeerakKashyap! You can follow this command to install linter :

go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1 

then export it's path and you can run the linters locally using golangci-lint run command inside the GoFr directory.

@HeerakKashyap
Copy link
Author

@Umang01-hash CAN U LET me know how can i run linter test locally, if there's some application in Golang for running linter tests

Hey @HeerakKashyap! You can follow this command to install linter :

go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1 

then export it's path and you can run the linters locally using golangci-lint run command inside the GoFr directory.

okay Sir, let me do this then i'll let u know
Thanks!

@HeerakKashyap
Copy link
Author

HeerakKashyap commented Jul 25, 2025

@Umang01-hash Code quality tests are passed, what's next ?

@HeerakKashyap
Copy link
Author

HeerakKashyap commented Jul 25, 2025

@Umang01-hash CAN U LET me know how can i run linter test locally, if there's some application in Golang for running linter tests

Hey @HeerakKashyap! You can follow this command to install linter :

go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1 

then export it's path and you can run the linters locally using golangci-lint run command inside the GoFr directory.

all linter issues are solved, I tested it locally
@Umang01-hash

@HeerakKashyap
Copy link
Author

HeerakKashyap commented Jul 25, 2025

@Umang01-hash . KINDLY REVIEW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants