Skip to content

Commit

Permalink
Merge b8e1972 into faad066
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Oct 16, 2020
2 parents faad066 + b8e1972 commit afd5718
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
env:
REDIS_URL: redis://redis:6379
QUIRREL_DISABLE_TELEMETRY: true
DISABLE_TELEMETRY: true

steps:
- uses: actions/checkout@v2
Expand Down
96 changes: 96 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"bin": "./dist/src/cli.js",
"scripts": {
"start": "node dist/src/main.js",
"test": "QUIRREL_DISABLE_TELEMETRY=true jest --runInBand --forceExit",
"test": "DISABLE_TELEMETRY=true jest --runInBand --forceExit",
"build": "run-s build:schemas build:tsc",
"build:schemas": "json2ts -i src/scheduler/schemas/ -o src/scheduler/types/",
"build:tsc": "tsc",
Expand All @@ -42,6 +42,8 @@
},
"dependencies": {
"@quirrel/bullmq": "^1.9.0-6",
"@sentry/node": "^5.26.0",
"@sentry/tracing": "^5.26.0",
"@types/ioredis": "^4.17.5",
"axios": "^0.20.0",
"commander": "^6.1.0",
Expand Down
2 changes: 2 additions & 0 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
HOST,
PASSPHRASES,
RUNNING_IN_DOCKER,
DISABLE_TELEMETRY,
} = process.env;

process.on("unhandledRejection", (reason, promise) => {
Expand All @@ -20,6 +21,7 @@ async function main() {
redis: !!REDIS_URL ? REDIS_URL : undefined,
passphrases: !!PASSPHRASES ? PASSPHRASES.split(":") : undefined,
runningInDocker: Boolean(RUNNING_IN_DOCKER),
disableTelemetry: Boolean(DISABLE_TELEMETRY),
});

async function teardown(signal: string) {
Expand Down
9 changes: 4 additions & 5 deletions api/src/scheduler/basic-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ interface BasicAuthPluginOpts {

const basicAuthPlugin: FastifyPluginCallback<BasicAuthPluginOpts> = async (
fastify,
opts,
done
opts
) => {
fastify.register(fastifyBasicAuth, {
validate(username, password, req, reply, done) {
Expand All @@ -20,8 +19,8 @@ const basicAuthPlugin: FastifyPluginCallback<BasicAuthPluginOpts> = async (
}
},
});

done();
};

export default (fp as any)(basicAuthPlugin);
export default (fp as any)(basicAuthPlugin) as FastifyPluginCallback<
BasicAuthPluginOpts
>;
17 changes: 13 additions & 4 deletions api/src/scheduler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import activityPlugin from "./routes/activity";
import blipp from "fastify-blipp";
import cors from "fastify-cors";
import telemetry from "./telemetry";
import sentryPlugin from "./sentry";

export interface QuirrelServerConfig {
port?: number;
host?: string;
redis?: RedisOptions | string;
passphrases?: string[];
runningInDocker?: boolean;
disableTelemetry?: boolean;
}

export async function createServer({
Expand All @@ -30,11 +32,16 @@ export async function createServer({
runningInDocker = false,
redis,
passphrases,
disableTelemetry,
}: QuirrelServerConfig) {
const app = fastify({
logger: true,
});

if (!disableTelemetry) {
app.register(sentryPlugin);
}

app.register(blipp);

app.register(cors, {
Expand Down Expand Up @@ -65,7 +72,9 @@ export async function createServer({

app.register(tokenAuthPlugin, { auth: enableAuth });

app.register(telemetry, { runningInDocker });
if (!disableTelemetry) {
app.register(telemetry, { runningInDocker });
}

if (passphrases) {
app.register(basicAuthPlugin, { passphrases });
Expand All @@ -86,14 +95,14 @@ export async function createServer({

await app.oas();

app.telemetrist.dispatch("ready");
app.telemetrist?.dispatch("ready");

if (passphrases) {
app.telemetrist.dispatch("auth enabled");
app.telemetrist?.dispatch("auth enabled");
}

if (port !== 9181) {
app.telemetrist.dispatch("custom port");
app.telemetrist?.dispatch("custom port");
}
});

Expand Down
5 changes: 3 additions & 2 deletions api/src/scheduler/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServer } from ".";

const { PORT = 9181, REDIS_URL, HOST, PASSPHRASES, RUNNING_IN_DOCKER } = process.env;
const { PORT = 9181, REDIS_URL, HOST, PASSPHRASES, RUNNING_IN_DOCKER, DISABLE_TELEMETRY } = process.env;

process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
Expand All @@ -13,7 +13,8 @@ async function main() {
host: HOST,
redis: !!REDIS_URL ? REDIS_URL : undefined,
passphrases: !!PASSPHRASES ? PASSPHRASES.split(":") : undefined,
runningInDocker: Boolean(RUNNING_IN_DOCKER)
runningInDocker: Boolean(RUNNING_IN_DOCKER),
disableTelemetry: Boolean(DISABLE_TELEMETRY)
});

async function teardown(signal: string) {
Expand Down
24 changes: 14 additions & 10 deletions api/src/scheduler/redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FastifyPluginCallback } from "fastify";
import * as fp from "fastify-plugin";

import * as Redis from "ioredis";

declare module "fastify" {
Expand All @@ -9,20 +8,25 @@ declare module "fastify" {
}
}

const redisPlugin: FastifyPluginCallback<{
opts: Redis.RedisOptions | string;
}> = async (fastify, { opts }, done) => {
interface RedisPluginOpts {
opts?: Redis.RedisOptions | string;
}

const redisPlugin: FastifyPluginCallback<RedisPluginOpts> = (
fastify,
{ opts },
done
) => {
const client = new Redis(opts as any);
fastify.decorate("redis", client);

fastify.addHook("onClose", async (instance, done) => {
fastify.addHook("onClose", async () => {
await client.quit();
done();
});

client.addListener("ready", () => {
done();
});
client.addListener("ready", done);
};

export default (fp as any)(redisPlugin);
export default (fp as any)(redisPlugin) as FastifyPluginCallback<
RedisPluginOpts
>;
6 changes: 3 additions & 3 deletions api/src/scheduler/routes/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { FastifyPluginCallback } from "fastify";
import * as fastifyWebsocket from "fastify-websocket";
import { JobsRepo } from "../jobs-repo";

const activityPlugin: FastifyPluginCallback = async (fastify, _opts, done) => {
const activityPlugin: FastifyPluginCallback = (fastify, _opts, done) => {
fastify.register(fastifyWebsocket);

const jobsRepo = new JobsRepo(fastify.redis);

fastify.get("/", { websocket: true }, async (connection, req) => {
const [tokenId, done] = await fastify.tokenAuth.authenticate(req);
const tokenId = await fastify.tokenAuth.authenticate(req);

if (done) {
if (!tokenId) {
connection.socket.close();
return;
}
Expand Down
Loading

0 comments on commit afd5718

Please sign in to comment.