Skip to content
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

2.x.x - Run process before server start #340

Merged
merged 5 commits into from
Jan 11, 2024
Merged

Conversation

adam-fowler
Copy link
Member

While writing the Todos tutorial I came across an issue with database setup. There are situations where you need to do pre-processing of your database before starting the server eg running migrations. Unfortunately the new PostgresClient requires that you have a background process running, not currently managed by ServiceLifecycle but I assume it will be eventually. So you can't do the database preprocessing before the server has started because it requires the PostgresClient service to be running, which would also kick off the server service.

This PR adds a way to add processes to be run before the server has started but while all other services are running.

let db = DatabaseClient()
let app = HBApplication()
app.addService(db)
add.runBeforeServerStart {
    db.migrate()
}

@adam-fowler adam-fowler requested a review from Joannis January 8, 2024 11:33
let router = HBRouter()
var app = HBApplication(
responder: router.buildResponder()
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Once #339 is merged I can add a check to see if the atomic has been set by then

Copy link
Member

Choose a reason for hiding this comment

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

#339 can be merged

Copy link

codecov bot commented Jan 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1e3b93d) 83.08% compared to head (3de1cd5) 83.27%.
Report is 2 commits behind head on 2.x.x.

Additional details and impacted files
@@            Coverage Diff             @@
##            2.x.x     #340      +/-   ##
==========================================
+ Coverage   83.08%   83.27%   +0.18%     
==========================================
  Files          92       93       +1     
  Lines        5037     5058      +21     
==========================================
+ Hits         4185     4212      +27     
+ Misses        852      846       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Jan 9, 2024

var services: [any Service] { self.base.services }

/// Processes run before server start
public var processesRunBeforeServerStart: [@Sendable () async throws -> Void] { self.base.processesRunBeforeServerStart }
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this variable name, but I don't know a better one.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we could use pre. Like preStartProcesses. A little bit more vague than processesRunBeforeServerStart, but should do the job.

In that case, there is also the runBeforeServerStart function that needs to have a change of name to something like preStartRun. Not sure if these are the best options, but they sound good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it's not a great variable name, but I don't think @MahdiBM your suggestions are much better especially the function rename. preStartRun doesn't mean a great deal.

Copy link
Member Author

Choose a reason for hiding this comment

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

Gonna add a Bikeshedding issue with all the symbol names that need revisiting and add this to it

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@Joannis Joannis left a comment

Choose a reason for hiding this comment

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

Please do fix the test, but otherwise approved

let router = HBRouter()
var app = HBApplication(
responder: router.buildResponder()
)
Copy link
Member

Choose a reason for hiding this comment

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

#339 can be merged

Sources/Hummingbird/Application.swift Outdated Show resolved Hide resolved
var services: [any Service] { self.base.services }

/// Processes run before server start
public var processesRunBeforeServerStart: [@Sendable () async throws -> Void] { self.base.processesRunBeforeServerStart }
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we could use pre. Like preStartProcesses. A little bit more vague than processesRunBeforeServerStart, but should do the job.

In that case, there is also the runBeforeServerStart function that needs to have a change of name to something like preStartRun. Not sure if these are the best options, but they sound good to me.

@adam-fowler adam-fowler merged commit 9e98314 into 2.x.x Jan 11, 2024
4 of 5 checks passed
@adam-fowler adam-fowler deleted the preprocess-service branch January 11, 2024 07:57
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