-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): hot load environments for runtime instead of restarting…
… runtime (#1077)
- Loading branch information
Showing
14 changed files
with
1,581 additions
and
2,825 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"name": "f1", | ||
"description": "laf e2e function(hello-laf)", | ||
"websocket": false, | ||
"methods": [ | ||
"GET", | ||
"POST" | ||
|
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"name": "f2", | ||
"description": "laf e2e function(db example)", | ||
"websocket": false, | ||
"methods": [ | ||
"GET", | ||
"POST" | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CONFIG_COLLECTION } from '../constants' | ||
import { DatabaseAgent } from '../db' | ||
|
||
import { logger } from './logger' | ||
|
||
export class DatabaseChangeStream { | ||
static async initialize() { | ||
this.watchConf() | ||
} | ||
|
||
/** | ||
* stream the change of cloud function | ||
* @param | ||
* @returns | ||
*/ | ||
static async watchConf() { | ||
logger.info('Listening for changes in conf collection...') | ||
this.updateEnvironments() | ||
|
||
const stream = DatabaseAgent.db.collection(CONFIG_COLLECTION).watch() | ||
|
||
stream.on('change', async (_change) => { | ||
this.updateEnvironments() | ||
}) | ||
} | ||
|
||
private static async updateEnvironments() { | ||
const conf = await DatabaseAgent.db | ||
.collection(CONFIG_COLLECTION) | ||
.findOne({}) | ||
|
||
if (!conf) { | ||
return | ||
} | ||
|
||
const environments = conf.environments || [] | ||
for (const env of environments) { | ||
process.env[env.name] = env.value | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { ApplicationConfiguration } from '@prisma/client' | ||
import { CN_PUBLISHED_CONF } from 'src/constants' | ||
import { DatabaseService } from 'src/database/database.service' | ||
import { PrismaService } from 'src/prisma/prisma.service' | ||
|
||
@Injectable() | ||
export class ApplicationConfigurationService { | ||
private readonly logger = new Logger(ApplicationConfigurationService.name) | ||
|
||
constructor(private readonly prisma: PrismaService) {} | ||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly databaseService: DatabaseService, | ||
) {} | ||
|
||
async count(appid: string) { | ||
return this.prisma.applicationConfiguration.count({ | ||
where: { | ||
appid, | ||
}, | ||
}) | ||
return this.prisma.applicationConfiguration.count({ where: { appid } }) | ||
} | ||
|
||
async remove(appid: string) { | ||
return this.prisma.applicationConfiguration.delete({ | ||
where: { | ||
appid, | ||
}, | ||
}) | ||
return this.prisma.applicationConfiguration.delete({ where: { appid } }) | ||
} | ||
|
||
async publish(conf: ApplicationConfiguration) { | ||
const { db, client } = await this.databaseService.findAndConnect(conf.appid) | ||
const session = client.startSession() | ||
try { | ||
await session.withTransaction(async () => { | ||
const coll = db.collection(CN_PUBLISHED_CONF) | ||
await coll.deleteOne({ appid: conf.appid }, { session }) | ||
await coll.insertOne(conf, { session }) | ||
}) | ||
} finally { | ||
await client.close() | ||
} | ||
} | ||
} |
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