-
Notifications
You must be signed in to change notification settings - Fork 2
/
jobs.mjs
82 lines (69 loc) · 2.39 KB
/
jobs.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Retrieve Job-defined env vars
const { CLOUD_RUN_TASK_INDEX = 0, CLOUD_RUN_TASK_ATTEMPT = 0 } = process.env;
// Retrieve User-defined env vars
const { JOB_NAME, CONTINUOUS_JOB } = process.env;
import { JOBS } from "./variables.mjs";
import { cacheCoingeckoTickersJob } from "./jobs/cache_coingecko_tickers_job.mjs";
import { cacheHydrationWebStatsJob } from "./jobs/cache_hydration-web_stats_job.mjs";
import { cacheHydradxUiStatsTvlJob } from "./jobs/cache_hydradx-ui_stats_tvl_job.mjs";
import { cacheHydradxUiV2StatsVolumeJob } from "./jobs/cache_hydradx-ui_v2_stats_volume_job.mjs";
import { cacheCoinmarketcapSummaryJob } from "./jobs/cache_coinmarketcap_summary_job.mjs";
import { newSqlClient } from "./clients/sql.mjs";
import { newRedisClient } from "./clients/redis.mjs";
const main = async () => {
console.log(
`Cloud Run Job ${JOB_NAME} started (task index #${CLOUD_RUN_TASK_INDEX}, attempt #${CLOUD_RUN_TASK_ATTEMPT})`
);
console.log(`CONTINUOUS_JOB: ${isContinuousJob()}`);
await executeJob(JOB_NAME);
console.log(
`Cloud Run Job ${JOB_NAME} completed (task index #${CLOUD_RUN_TASK_INDEX})`
);
process.exit();
};
async function executeJob(job_name) {
console.log(`Executing ${job_name}..`);
const sqlClient = await newSqlClient();
const redisClient = await newRedisClient();
switch (job_name) {
case JOBS["cacheCoingeckoTickersJob"]: {
await cacheCoingeckoTickersJob(sqlClient, redisClient);
break;
}
case JOBS["cacheHydrationWebStatsJob"]: {
await cacheHydrationWebStatsJob(sqlClient, redisClient);
break;
}
case JOBS["cacheHydradxUiStatsTvlJob"]: {
await cacheHydradxUiStatsTvlJob(sqlClient, redisClient);
break;
}
case JOBS["cacheHydradxUiV2StatsVolumeJob"]: {
await cacheHydradxUiV2StatsVolumeJob(sqlClient, redisClient);
break;
}
case JOBS["cacheCoinmarketcapSummaryJob"]: {
await cacheCoinmarketcapSummaryJob(sqlClient, redisClient);
break;
}
default: {
throw new Error(`Job not found: ${JOB_NAME}`);
}
}
console.log(`Executed ${job_name}`);
if (CONTINUOUS_JOB == "true") {
return executeJob(job_name);
} else {
await sqlClient.release();
await redisClient.disconnect();
return true;
}
}
function isContinuousJob() {
return CONTINUOUS_JOB == "true";
}
// Start script
main().catch((err) => {
console.error(err);
process.exit(1);
});