Skip to content

Commit

Permalink
Update: cronjob trigger via env var, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mojcaostir committed Nov 1, 2022
1 parent bb9b045 commit 56894ac
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 49 deletions.
8 changes: 5 additions & 3 deletions setup/.env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Possible log levels: trace, debug, info, warn, error
LOG_LEVEL=info

# Cron expression
CRON_EXPRESSION=*/10 * * * * *
# Cron
CRON=<boolean>
CRON_EXPRESSION=<expression>

# Databox
DATABOX_TOKEN=<token>
DATABOX_TOKEN_GITHUB=<token>
DATABOX_TOKEN_SPOTIFY=<token>

# Github
GITHUB_PERSONAL_ACCESS_TOKEN=<token>
Expand Down
14 changes: 8 additions & 6 deletions src/adapters/databox.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { config } from "../config";
import { createLogger } from "../util/logger";
import { IDataboxResponse, IMetrics } from "../models";
import { IDataboxResponse, IMetrics, ServiceProvider } from "../models";

const Databox = require("databox");

const logger = createLogger("Databox Adapter Logger");

const client = new Databox({
push_token: config.databoxToken,
});

export async function sendMetricsToDatabox(metrics: IMetrics[]): Promise<IDataboxResponse> {
export async function sendMetricsToDatabox(
metrics: IMetrics[],
serviceProvider: ServiceProvider
): Promise<IDataboxResponse> {
logger.debug("Sending metrics to Databox", { metrics });
const client = new Databox({
push_token: serviceProvider === "Github" ? config.databoxTokenGithub : config.databoxTokenSpotify,
});
return new Promise((resolve, reject) => {
client.insertAll(metrics, (response: IDataboxResponse) => {
if (response.status !== "OK") {
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export enum LogLevel {

export const config = {
logLevel: getAllowedConfig<LogLevel>("LOG_LEVEL", Object.values(LogLevel), LogLevel.Info),
cron: getConfig("CRON"),
cronExpression: getConfig("CRON_EXPRESSION"),
gitHubToken: getConfig("GITHUB_PERSONAL_ACCESS_TOKEN"),
spotifyClientId: getConfig("SPOTIFY_CLIENT_ID"),
spotifyClientSecret: getConfig("SPOTIFY_CLIENT_SECRET"),
databoxToken: getConfig("DATABOX_TOKEN"),
databoxTokenGithub: getConfig("DATABOX_TOKEN_GITHUB"),
databoxTokenSpotify: getConfig("DATABOX_TOKEN_SPOTIFY"),
};

export function getAllowedConfig<T>(envKey: string, allowedValues: readonly T[], fallbackValue: T): T {
Expand Down
30 changes: 6 additions & 24 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
import express from "express";
import cron from "node-cron";
import bodyParser from "body-parser";
import { githubRouter } from "./github/githubRouter";
import { spotifyRouter } from "./spotify/spotifyRouter";
import { githubService } from "./github/githubService";
import { spotifyService } from "./spotify/spotifyService";
import { createLogger } from "./util/logger";
import { config } from "./config";
import { spotifyRouter } from "./services/spotify/spotifyRouter";
import { cronjobService } from "./services/cronjob/cronjob";
import { githubRouter } from "./services/github/githubRouter";

const logger = createLogger("DataChallengeBox Server");

export async function initServer(): Promise<void> {
try {
const app = express();

cron.schedule(config.cronExpression, async function () {
logger.info("Cronjob starting github Service");
await githubService([
{ endpoint: "issues/comments", owner: "nodejs", repo: "nodejs.dev", key: "comments", dateTime: true },
{ endpoint: "releases", owner: "nodejs", repo: "postject", key: "releases", dateTime: false },
{ endpoint: "stargazers", owner: "nodejs", repo: "postject", key: "stars", dateTime: false },
]);

logger.info("Cronjob starting spotify Service");
await spotifyService([
{
url: "https://api.spotify.com/v1/browse/categories/0JQ5DAqbMKFEC4WFtoNRpw/playlists",
key: "categoryPlaylists",
},
{ url: "https://api.spotify.com/v1/playlists/3Na18bFHq9tQOpcBx8dYET/tracks", key: "playlistItems" },
{ url: "https://api.spotify.com/v1/users/user_id/playlists", key: "userPlaylists" },
]);
logger.info("Cronjob completed");
});
if (config.cron) {
await cronjobService();
}

app
.get("/", (_req, res) => {
Expand Down
29 changes: 29 additions & 0 deletions src/services/cronjob/cronjob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cron from "node-cron";
import { createLogger } from "../../util/logger";
import { spotifyService } from "../spotify/spotifyService";
import { githubService } from "../github/githubService";
import { config } from "../../config";

const logger = createLogger("Cronjob Service");

export async function cronjobService() {
cron.schedule(config.cronExpression, async function () {
logger.info("Cronjob starting github Service");
await githubService([
{ endpoint: "issues/comments", owner: "nodejs", repo: "nodejs.dev", key: "comments", dateTime: true },
{ endpoint: "releases", owner: "nodejs", repo: "postject", key: "releases", dateTime: false },
{ endpoint: "stargazers", owner: "nodejs", repo: "postject", key: "stars", dateTime: false },
]);

logger.info("Cronjob starting spotify Service");
await spotifyService([
{
url: "https://api.spotify.com/v1/browse/categories/0JQ5DAqbMKFEC4WFtoNRpw/playlists",
key: "categoryPlaylists",
},
{ url: "https://api.spotify.com/v1/playlists/3Na18bFHq9tQOpcBx8dYET/tracks", key: "playlistItems" },
{ url: "https://api.spotify.com/v1/users/user_id/playlists", key: "userPlaylists" },
]);
logger.info("Cronjob completed");
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { githubService } from "./githubService";
import { createLogger } from "../util/logger";
import { Request, Response } from "express";
import { IResponseBody, IResponseData } from "../models";
import { createLogger } from "../../util/logger";
import { IResponseBody, IResponseData } from "../../models";

const logger = createLogger("Github Controller Logger");

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sendMetricsToDatabox } from "../adapters/databox";
import { getGithubMetric } from "../adapters/github";
import { getDateTimeYesterdayMidnight } from "../util/dateTime";
import { createLogger } from "../util/logger";
import { DateTime } from "luxon";
import { GithubMetricKey, IMetrics, IResponseData } from "../models";
import { createLogger } from "../../util/logger";
import { GithubMetricKey, IMetrics, IResponseData } from "../../models";
import { sendMetricsToDatabox } from "../../adapters/databox";
import { getDateTimeYesterdayMidnight } from "../../util/dateTime";
import { getGithubMetric } from "../../adapters/github";

const logger = createLogger("Github Service Logger");

Expand Down Expand Up @@ -43,7 +43,7 @@ export async function githubService(
metrics.push({ key: input.key, value: githubMetric.length });
}
try {
await sendMetricsToDatabox(metrics);
await sendMetricsToDatabox(metrics, "Github");
return {
serviceProvider: "Github",
sentAt: DateTime.utc().toISO(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spotifyService } from "./spotifyService";
import { createLogger } from "../util/logger";
import { Request, Response } from "express";
import { IResponseBody, IResponseData } from "../models";
import { createLogger } from "../../util/logger";
import { IResponseBody, IResponseData } from "../../models";

const logger = createLogger("Spotify Controller Logger");

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sendMetricsToDatabox } from "../adapters/databox";
import { getSpotifyMetric } from "../adapters/spotify";
import { createLogger } from "../util/logger";
import { IMetrics, IResponseData, SpotifyMetricKey } from "../models";
import { DateTime } from "luxon";
import { createLogger } from "../../util/logger";
import { sendMetricsToDatabox } from "../../adapters/databox";
import { IMetrics, IResponseData, SpotifyMetricKey } from "../../models";
import { getSpotifyMetric } from "../../adapters/spotify";

const logger = createLogger("Spotify Service Logger");

Expand Down Expand Up @@ -36,7 +36,7 @@ export async function spotifyService(inputArray: { url: string; key: SpotifyMetr
}
}
try {
await sendMetricsToDatabox(metrics);
await sendMetricsToDatabox(metrics, "Spotify");
return {
serviceProvider: "Spotify",
sentAt: DateTime.utc().toISO(),
Expand Down

0 comments on commit 56894ac

Please sign in to comment.