Skip to content

Commit

Permalink
Reloading ponder.config.ts restarts the ponder app (#466)
Browse files Browse the repository at this point in the history
- Add setup() method to server service to replicate interface of other services
- Reload ponder app when config file changes rather than exiting
- resetMetrics() method to metrics service
  • Loading branch information
bankisan authored Dec 4, 2023
1 parent b0b8dc1 commit 96a5321
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
26 changes: 24 additions & 2 deletions packages/core/src/Ponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Ponder {

// One-time setup for some services.
await this.syncStore.migrateUp();
await this.serverService.start();
this.serverService.setup();

// Finally, load the schema + indexing functions which will trigger
// the indexing service to reload (for the first time).
Expand All @@ -217,6 +217,7 @@ export class Ponder {
},
});
this.serverService.registerDevRoutes();
await this.serverService.start();

await Promise.all(
this.syncServices.map(async ({ historical, realtime }) => {
Expand All @@ -241,6 +242,7 @@ export class Ponder {

// If not using `dev`, can kill the build service here to avoid hot reloads.
await this.buildService.kill();
await this.serverService.start();

await Promise.all(
this.syncServices.map(async ({ historical, realtime }) => {
Expand Down Expand Up @@ -302,6 +304,7 @@ export class Ponder {
indexingStore: this.indexingStore,
});

this.serverService.setup();
await this.serverService.start();

const { schema, graphqlSchema } = schemaResult;
Expand Down Expand Up @@ -358,11 +361,30 @@ export class Ponder {

private registerServiceDependencies() {
this.buildService.on("newConfig", async () => {
this.common.logger.fatal({
this.common.logger.info({
service: "build",
msg: "Detected change in ponder.config.ts",
});
this.common.logger.info({
service: "app",
msg: "Reloading ponder, killing and restarting services",
});
await this.kill();

// Clear all listeners. Will be added back in setup.
this.buildService.clearListeners();
this.syncServices.forEach(({ historical, realtime }) => {
historical.clearListeners();
realtime.clearListeners();
});
this.syncGatewayService.clearListeners();
this.serverService.clearListeners();
this.indexingService.clearListeners();
this.common.metrics.resetMetrics();

await this.setup();
// NOTE: We know we are in dev mode if the build service is receiving events. Build events are disabled in production.
await this.dev();
});

this.buildService.on("newSchema", async ({ schema, graphqlSchema }) => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/build/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export class BuildService extends Emittery<BuildServiceEvents> {
// TODO: Validate config lol

this.emit("newConfig", { config });

return config;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/metrics/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,8 @@ export class MetricsService {
async getMetrics() {
return await this.registry.metrics();
}

async resetMetrics() {
this.registry.resetMetrics();
}
}
1 change: 1 addition & 0 deletions packages/core/src/server/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const setup = async ({
await indexingStore.reload({ schema: s });

const service = new ServerService({ common, indexingStore });
service.setup();
await service.start();
service.reloadGraphqlSchema({ graphqlSchema });

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/server/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class ServerService extends Emittery<ServerEvents> {
this.indexingStore = indexingStore;
this.port = this.common.options.port;
this.app = express();
}

setup() {
this.registerRoutes();
}

Expand Down

1 comment on commit 96a5321

@vercel
Copy link

@vercel vercel bot commented on 96a5321 Dec 4, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.