Skip to content

Commit

Permalink
email-server, job-server: refactor email-server and job-server (pingc…
Browse files Browse the repository at this point in the history
…ap#1054)

refactor email-server and job-server
  • Loading branch information
Mini256 authored Dec 23, 2022
1 parent 94c5b66 commit be2dfec
Show file tree
Hide file tree
Showing 51 changed files with 3,266 additions and 995 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import IORedis from "ioredis";
import {CacheProvider} from "../../../core/cache/provider/CacheProvider";
import {AST, Parser, Select} from "node-sql-parser";

export const PLAYGROUND_SQL_QUEUE_NAME = "playground-sql";
export const PLAYGROUND_SQL_CACHE_TTL = 5 * 60; // 5 minutes.
export const PLAYGROUND_QUEUE_NAME = "playground";
export const PLAYGROUND_CACHE_TTL = 5 * 60; // 5 minutes.
export const MAX_SELECT_LIMIT = 200;

declare module 'fastify' {
Expand Down Expand Up @@ -104,7 +104,7 @@ export class PlaygroundService {
private readonly playgroundQueryExecutor: TiDBPlaygroundQueryExecutor,
redisClient: IORedis
) {
this.playgroundSQLQueue = new Queue(PLAYGROUND_SQL_QUEUE_NAME, {
this.playgroundSQLQueue = new Queue(PLAYGROUND_QUEUE_NAME, {
connection: redisClient
});
this.logger = pino().child({
Expand Down Expand Up @@ -296,7 +296,7 @@ export class PlaygroundService {
}

async executeSQLInQueue(queue: Queue, execution: QueryExecution): Promise<QueryResult> {
const job = await queue.add(PLAYGROUND_SQL_QUEUE_NAME, execution);
const job = await queue.add(PLAYGROUND_QUEUE_NAME, execution);
if (!job.id) {
throw new APIError(500, 'Failed to add query job to queue.');
}
Expand Down Expand Up @@ -365,7 +365,7 @@ export class PlaygroundService {
}
};
await cache.set(cacheKey, JSON.stringify(res), {
EX: PLAYGROUND_SQL_CACHE_TTL,
EX: PLAYGROUND_CACHE_TTL,
});
await this.queryExecutionService.finishQueryExecution(conn, {
id: executionId,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@ossinsight/feeds-server",
"version": "0.0.0",
"name": "@ossinsight/email-server",
"version": "0.0.1",
"private": true,
"description": "The feeds server of OSSInsight.",
"description": "The email server of OSSInsight.",
"homepage": "https://github.com/pingcap/ossinsight#readme",
"bugs": {
"url": "https://github.com/pingcap/ossinsight/issues"
Expand All @@ -11,8 +11,9 @@
"type": "git",
"url": "git+https://github.com/pingcap/ossinsight.git"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"main": "app.ts",
"scripts": {
"build:ts": "tsc",
"dev": "npm run build:ts && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:watch:ts\" \"npm:dev:start\"",
Expand All @@ -33,6 +34,7 @@
"@mailchimp/mailchimp_transactional": "^1.0.50",
"@types/cron": "^2.0.0",
"async": "^3.2.4",
"axios": "^0.27.2",
"bufferutil": "^4.0.7",
"canvas": "^2.10.2",
"cron": "^2.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@ import fastifyMySQL, { MySQLPromisePool } from '@fastify/mysql';

import { EmailServerEnvSchema } from './env';
import { FastifyPluginAsync } from 'fastify';
import { JobName } from './types';
import fastifyCron from 'fastify-cron';
import fastifyEnv from '@fastify/env';
import { join } from 'path';

export type AppOptions = {
calcHistoryRepoMilestones: boolean;
} & Partial<AutoloadPluginOptions>;

// Pass --options via CLI arguments in command to enable these options.
const options: AppOptions = {
calcHistoryRepoMilestones: false,
};

declare module 'fastify' {
interface FastifyInstance {
config: {
DATABASE_URL: string;
SEND_REPO_FEEDS_CRON: string;
CALC_REPO_MILESTONES_CRON: string;
MAX_CONCURRENT: number;
};
mysql: MySQLPromisePool;
jobParameters: Map<JobName, Record<string, any>>;
jobStatuses: Map<JobName, Record<string, any>>;
}
}

Expand All @@ -45,8 +36,6 @@ const app: FastifyPluginAsync<AppOptions> = async (
await fastify
.register(fastifyMySQL, {
promise: true,
connectionString: fastify.config.DATABASE_URL,
connectionLimit: fastify.config.MAX_CONCURRENT,
})
.ready(async () => {
try {
Expand All @@ -57,21 +46,6 @@ const app: FastifyPluginAsync<AppOptions> = async (
}
});

// Load jobs.
await fastify.register(fastifyCron);
await fastify.decorate(
'jobParameters',
new Map<JobName, Record<string, any>>(),
);
await fastify.decorate(
'jobStatuses',
new Map<JobName, Record<string, any>>(),
);
await fastify.register(AutoLoad, {
dir: join(__dirname, 'jobs'),
options: opts,
});

// Load API routes.
await fastify.register(AutoLoad, {
dir: join(__dirname, 'routes'),
Expand Down
27 changes: 27 additions & 0 deletions packages/email-server/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios, { AxiosInstance } from 'axios';

export interface SendEmailPayload {
to: string;
subject: string;
templateName: EmailTemplateNames;
templateData: Record<string, any>;
}

export enum EmailTemplateNames {
REPO_FEEDS = 'repo-feeds',
}

export class EmailClient {
private readonly httpClient: AxiosInstance;

constructor (readonly baseURL: string) {
this.httpClient = axios.create({
baseURL: baseURL,
timeout: 3000,
});
}

async sendEmail (payload: SendEmailPayload): Promise<void> {
return this.httpClient.post('/email', payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@ import Footer from './components/Footer';
import Head from './components/Head';
import Header from './components/Header';
import React from 'react';
import { RepoMilestoneToSent } from '../types';

type RepoMilestoneFeedsProps = {
export type RepoMilestoneFeedsProps = {
name: string;
repoMilestones: RepoMilestoneToSent[];
};

export interface RepoMilestoneToSent {
repoId: number;
repoName: string;
milestoneId: number;
milestoneTypeId: number;
milestoneTypeName: string;
milestoneNumber: number;
milestonePayload?: object;
watchedUserId: number;
}

const RepoMilestoneFeeds: React.FC<RepoMilestoneFeedsProps> = ({
name,
repoMilestones,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ export const EmailServerEnvSchema = {
type: 'object',
required: [
'DATABASE_URL',
'SEND_REPO_FEEDS_CRON',
'CALC_REPO_MILESTONES_CRON',
'MAX_CONCURRENT',
'POSTMARK_API_KEY',
],
properties: {
DATABASE_URL: {
type: 'string',
},
SEND_REPO_FEEDS_CRON: {
type: 'string',
default: '50 * * * *',
},
CALC_REPO_MILESTONES_CRON: {
type: 'string',
default: '20 * * * *',
},
MAX_CONCURRENT: {
type: 'number',
default: 10,
Expand Down
1 change: 1 addition & 0 deletions packages/email-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SendEmailPayload, EmailTemplateNames, EmailClient } from './client';
48 changes: 48 additions & 0 deletions packages/email-server/src/routes/email/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { FastifyPluginAsync } from 'fastify';
import React from 'react';
import RepoMilestoneFeeds, { RepoMilestoneFeedsProps } from '../../emails/RepositoryFeeds';
import sendMail from '../../emails';
import { EmailTemplateNames, SendEmailPayload } from '../../client';

const schema = {
body: {
type: 'object',
properties: {
to: { type: 'string' },
subject: { type: 'string' },
templateName: { type: 'string' },
templateData: { type: 'object' },
},
},
};

const send: FastifyPluginAsync = async (app, opts): Promise<void> => {
app.post<{
Body: SendEmailPayload;
}>('/', {
schema,
}, async function (req, reply) {
const {
to, subject, templateName, templateData,
} = req.body;

let component = null;
switch (templateName) {
case EmailTemplateNames.REPO_FEEDS:
component = React.createElement(RepoMilestoneFeeds, templateData as RepoMilestoneFeedsProps);
break;
default:
throw new Error(`Unknown template name: ${String(templateName)}`);
}

await sendMail({
subject,
to,
component,
});

void reply.send({ success: true });
});
};

export default send;
10 changes: 10 additions & 0 deletions packages/email-server/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FastifyBaseLogger, FastifyInstance, FastifyTypeProviderDefault, RawServerDefault } from 'fastify';
import { IncomingMessage, ServerResponse } from 'http';

import { Params } from 'fastify-cron';

export type FastifyServer = FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;

export type JobName = string;

export type CronJobDef = Params;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"declaration": true,
"outDir": "dist",
"sourceMap": true
},
Expand Down
Loading

0 comments on commit be2dfec

Please sign in to comment.