Skip to content

Commit

Permalink
chore(plugin-server): make it easier to run multiple plugin-server in…
Browse files Browse the repository at this point in the history
…stances locally (#17456)

* chore(plugin-server): allow customizing the HTTP server port

* chore(plugin-server): add NO_WATCH mode for development

* fix: http-server test
  • Loading branch information
bretthoerner authored Sep 15, 2023
1 parent 0b62ee2 commit 5b5d0d4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
10 changes: 9 additions & 1 deletion bin/plugin-server
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ if [ $? -ne 0 ]; then
exit 1
fi

[[ -n $DEBUG ]] && cmd="pnpm start:dev" || cmd="node dist/index.js"
if [[ -n $DEBUG ]]; then
if [[ -n $NO_WATCH ]]; then
cmd="pnpm start:devNoWatch"
else
cmd="pnpm start:dev"
fi
else
cmd="node dist/index.js"
fi

if [[ -n $NO_RESTART_LOOP ]]; then
echo "▶️ Starting plugin server..."
Expand Down
1 change: 1 addition & 0 deletions plugin-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "pnpm start:dist",
"start:dist": "BASE_DIR=.. node dist/index.js",
"start:dev": "NODE_ENV=dev BASE_DIR=.. nodemon --watch src/ --exec node -r @swc-node/register src/index.ts",
"start:devNoWatch": "NODE_ENV=dev BASE_DIR=.. node -r @swc-node/register src/index.ts",
"build": "pnpm clean && pnpm compile",
"clean": "rm -rf dist/*",
"typescript:compile": "tsc -b",
Expand Down
3 changes: 3 additions & 0 deletions plugin-server/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW,
} from './kafka-topics'

export const DEFAULT_HTTP_SERVER_PORT = 6738

export const defaultConfig = overrideWithEnv(getDefaultConfig())

export function getDefaultConfig(): PluginsServerConfig {
Expand Down Expand Up @@ -75,6 +77,7 @@ export function getDefaultConfig(): PluginsServerConfig {
SENTRY_DSN: null,
SENTRY_PLUGIN_SERVER_TRACING_SAMPLE_RATE: 0,
SENTRY_PLUGIN_SERVER_PROFILING_SAMPLE_RATE: 0,
HTTP_SERVER_PORT: DEFAULT_HTTP_SERVER_PORT,
STATSD_HOST: null,
STATSD_PORT: 8125,
STATSD_PREFIX: 'plugin-server.',
Expand Down
2 changes: 1 addition & 1 deletion plugin-server/src/main/pluginsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export async function startPluginsServer(
}

if (capabilities.http) {
httpServer = createHttpServer(healthChecks, analyticsEventsIngestionConsumer)
httpServer = createHttpServer(serverConfig.HTTP_SERVER_PORT, healthChecks, analyticsEventsIngestionConsumer)
}

// If session recordings consumer is defined, then join it. If join
Expand Down
9 changes: 4 additions & 5 deletions plugin-server/src/main/services/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import * as prometheus from 'prom-client'

import { status } from '../../utils/status'

export const HTTP_SERVER_PORT = 6738

prometheus.collectDefaultMetrics()
const v8Profiler = require('v8-profiler-next')
v8Profiler.setGenerateType(1)

export function createHttpServer(
port: number,
healthChecks: { [service: string]: () => Promise<boolean> | boolean },
analyticsEventsIngestionConsumer?: KafkaJSIngestionConsumer | IngestionConsumer
): Server {
Expand Down Expand Up @@ -47,7 +46,7 @@ export function createHttpServer(
// }
// }
const checkResults = await Promise.all(
// Note that we do not ues `Promise.allSettled` here so we can
// Note that we do not use `Promise.allSettled` here so we can
// assume that all promises have resolved. If there was a
// rejected promise, the http server should catch it and return
// a 500 status code.
Expand Down Expand Up @@ -118,8 +117,8 @@ export function createHttpServer(
}
})

server.listen(HTTP_SERVER_PORT, () => {
status.info('🩺', `Status server listening on port ${HTTP_SERVER_PORT}`)
server.listen(port, () => {
status.info('🩺', `Status server listening on port ${port}`)
})

return server
Expand Down
1 change: 1 addition & 0 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface PluginsServerConfig {
SENTRY_DSN: string | null
SENTRY_PLUGIN_SERVER_TRACING_SAMPLE_RATE: number // Rate of tracing in plugin server (between 0 and 1)
SENTRY_PLUGIN_SERVER_PROFILING_SAMPLE_RATE: number // Rate of profiling in plugin server (between 0 and 1)
HTTP_SERVER_PORT: number
STATSD_HOST: string | null
STATSD_PORT: number
STATSD_PREFIX: string
Expand Down
6 changes: 3 additions & 3 deletions plugin-server/tests/http-server.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http'

import { DEFAULT_HTTP_SERVER_PORT } from '../src/config/config'
import { startPluginsServer } from '../src/main/pluginsServer'
import { HTTP_SERVER_PORT } from '../src/main/services/http-server'
import { makePiscina } from '../src/worker/piscina'
import { resetTestDatabase } from './helpers/sql'

Expand Down Expand Up @@ -40,7 +40,7 @@ describe('http server', () => {
)

await new Promise((resolve) =>
http.get(`http://localhost:${HTTP_SERVER_PORT}/_health`, (res) => {
http.get(`http://localhost:${DEFAULT_HTTP_SERVER_PORT}/_health`, (res) => {
const { statusCode } = res
expect(statusCode).toEqual(200)
resolve(null)
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('http server', () => {
)

await new Promise((resolve) =>
http.get(`http://localhost:${HTTP_SERVER_PORT}/_ready`, (res) => {
http.get(`http://localhost:${DEFAULT_HTTP_SERVER_PORT}/_ready`, (res) => {
const { statusCode } = res
expect(statusCode).toEqual(200)
resolve(null)
Expand Down

0 comments on commit 5b5d0d4

Please sign in to comment.