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

fix(runtime): fix db stream initialize #1592

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(runtime): fix db stream initialize
  • Loading branch information
0fatal committed Oct 17, 2023
commit 80fffab213ebd44c230a73861b43894a976fd3da
2 changes: 0 additions & 2 deletions runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import xmlparser from 'express-xml-bodyparser'
import './support/cloud-sdk'
import storageServer from './storage-server'
import { DatabaseChangeStream } from './support/database-change-stream'
import { FunctionCache } from './support/function-engine/cache'

const app = express()

DatabaseAgent.accessor.ready.then(() => {
DatabaseChangeStream.initialize()
FunctionCache.initialize()
})

if (process.env.NODE_ENV === 'development') {
Expand Down
5 changes: 0 additions & 5 deletions runtimes/nodejs/src/storage-server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import express from 'express'
import Config from './config'
import { logger } from './support/logger'
import { DatabaseAgent } from './db'
import './support/cloud-sdk'
import { WebsiteHostingChangeStream } from './support/database-change-stream/website-hosting-change-stream'
import proxy from 'express-http-proxy'
import axios from 'axios'

const app = express()

DatabaseAgent.accessor.ready.then(() => {
WebsiteHostingChangeStream.initialize()
})

const tryPath = (bucket: string, path: string) => {
const testPaths = path.endsWith('/')
? [path + 'index.html', '/index.html']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DatabaseChangeStream } from '.'

export class ConfChangeStream {
static initialize() {
this.updateEnvironments()

DatabaseChangeStream.onStreamChange(
CONFIG_COLLECTION,
this.updateEnvironments,
Expand Down
26 changes: 20 additions & 6 deletions runtimes/nodejs/src/support/database-change-stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import {
CONFIG_COLLECTION,
WEBSITE_HOSTING_COLLECTION,
} from '../../constants'
import { ConfChangeStream } from './conf-change-stream'
import { WebsiteHostingChangeStream } from './website-hosting-change-stream'
import { FunctionCache } from '../function-engine/cache'

const collectionsToWatch = [
CONFIG_COLLECTION,
CLOUD_FUNCTION_COLLECTION,
WEBSITE_HOSTING_COLLECTION,
{
name: CONFIG_COLLECTION,
handler: () => ConfChangeStream,
},
{
name: WEBSITE_HOSTING_COLLECTION,
handler: () => WebsiteHostingChangeStream,
},
{
name: CLOUD_FUNCTION_COLLECTION,
handler: () => FunctionCache,
},
] as const

export class DatabaseChangeStream extends EventEmitter {
private static instance: DatabaseChangeStream

Expand Down Expand Up @@ -50,13 +63,14 @@ export class DatabaseChangeStream extends EventEmitter {
static initialize() {
const instance = DatabaseChangeStream.getInstance()

collectionsToWatch.forEach((collectionName) => {
instance.initializeForCollection(collectionName)
collectionsToWatch.forEach((v) => {
instance.initializeForCollection(v.name)
v.handler().initialize()
})
}

static onStreamChange(
collectionName: (typeof collectionsToWatch)[number],
collectionName: (typeof collectionsToWatch)[number]['name'],
listener: (...args: any[]) => void,
) {
const instance = DatabaseChangeStream.getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class WebsiteHostingChangeStream {
static websiteHosting = []

static initialize() {
this.onStreamChange()

DatabaseChangeStream.onStreamChange(
WEBSITE_HOSTING_COLLECTION,
this.onStreamChange.bind(this),
Expand Down