-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathtasks.ts
More file actions
199 lines (180 loc) · 6.84 KB
/
Copy pathtasks.ts
File metadata and controls
199 lines (180 loc) · 6.84 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Trigger.dev scheduled tasks for data fetching.
*
* Weekly tasks run on Sundays at midnight UTC.
* Daily tasks run at midnight UTC.
* Hourly tasks run every hour.
*/
import * as Sentry from "@sentry/nextjs"
import { logger, schedules, task, tasks } from "@trigger.dev/sdk/v3"
import { fetchDeveloperTools } from "./fetchers/developer-tools"
import { fetchAccountHolders } from "./fetchers/fetchAccountHolders"
import { fetchApps } from "./fetchers/fetchApps"
import { fetchBlobStats } from "./fetchers/fetchBlobStats"
import { fetchCalendarEvents } from "./fetchers/fetchCalendarEvents"
import { fetchCommunityPicks } from "./fetchers/fetchCommunityPicks"
import { fetchEthereumStablecoinsMcap } from "./fetchers/fetchEthereumStablecoinsMcap"
import { fetchEthereumMetrics } from "./fetchers/fetchEthereumMetrics"
import { fetchEvents } from "./fetchers/fetchEvents"
import { fetchGasPrice } from "./fetchers/fetchGasPrice"
import { fetchGFIs } from "./fetchers/fetchGFIs"
import { fetchGitHistory } from "./fetchers/fetchGitHistory"
import { fetchGitHubContributors } from "./fetchers/fetchGitHubContributors"
import { fetchGithubRepoData } from "./fetchers/fetchGithubRepoData"
import { fetchGrowThePie } from "./fetchers/fetchGrowThePie"
import { fetchGrowThePieBlockspace } from "./fetchers/fetchGrowThePieBlockspace"
import { fetchGrowThePieMaster } from "./fetchers/fetchGrowThePieMaster"
import { fetchL2beat } from "./fetchers/fetchL2beat"
import { fetchAttestantPosts } from "./fetchers/fetchPosts"
import { fetchRSS } from "./fetchers/fetchRSS"
import { fetchStablecoinsData } from "./fetchers/fetchStablecoinsData"
import { fetchStakedPercentage } from "./fetchers/fetchStakedPercentage"
import { fetchTotalEthStaked } from "./fetchers/fetchTotalEthStaked"
import { fetchTotalValueLocked } from "./fetchers/fetchTotalValueLocked"
import { fetchTranslationGlossary } from "./fetchers/fetchTranslationGlossary"
import { fetchVideoThumbnails } from "./fetchers/fetchVideoThumbnails"
import { set } from "./storage"
export const KEYS = {
APPS: "fetch-apps",
CALENDAR_EVENTS: "fetch-calendar-events",
GITHUB_CONTRIBUTORS: "fetch-github-contributors",
COMMUNITY_PICKS: "fetch-community-picks",
DEVELOPER_TOOLS: "fetch-developer-tools",
GFIS: "fetch-gfis",
GIT_HISTORY: "fetch-git-history",
GROW_THE_PIE: "fetch-grow-the-pie",
GROW_THE_PIE_BLOCKSPACE: "fetch-grow-the-pie-blockspace",
GROW_THE_PIE_MASTER: "fetch-grow-the-pie-master",
L2BEAT: "fetch-l2beat",
POSTS: "fetch-posts",
RSS: "fetch-rss",
GITHUB_REPO_DATA: "fetch-github-repo-data",
EVENTS: "fetch-events",
BLOB_STATS: "fetch-blob-stats",
ETHEREUM_MARKETCAP: "fetch-ethereum-marketcap",
ETHEREUM_STABLECOINS_MCAP: "fetch-ethereum-stablecoins-mcap",
ETH_PRICE: "fetch-eth-price",
GAS_PRICE: "fetch-gas-price",
STAKED_PERCENTAGE: "fetch-staked-percentage",
TOTAL_ETH_STAKED: "fetch-total-eth-staked",
TOTAL_VALUE_LOCKED: "fetch-total-value-locked",
STABLECOINS_DATA: "fetch-stablecoins-data",
ACCOUNT_HOLDERS: "fetch-account-holders",
TRANSLATION_GLOSSARY: "fetch-translation-glossary",
VIDEO_THUMBNAILS: "fetch-video-thumbnails",
} as const
// Task definition: storage key + fetch function
type TaskDef = [string, () => Promise<unknown>]
const WEEKLY: TaskDef[] = [[KEYS.GITHUB_CONTRIBUTORS, fetchGitHubContributors]]
const DAILY: TaskDef[] = [
[KEYS.ACCOUNT_HOLDERS, fetchAccountHolders],
[KEYS.APPS, fetchApps],
[KEYS.BLOB_STATS, fetchBlobStats],
[KEYS.CALENDAR_EVENTS, fetchCalendarEvents],
[KEYS.COMMUNITY_PICKS, fetchCommunityPicks],
[KEYS.GFIS, fetchGFIs],
[KEYS.GIT_HISTORY, fetchGitHistory],
[KEYS.GROW_THE_PIE, fetchGrowThePie],
[KEYS.GROW_THE_PIE_BLOCKSPACE, fetchGrowThePieBlockspace],
[KEYS.GROW_THE_PIE_MASTER, fetchGrowThePieMaster],
[KEYS.L2BEAT, fetchL2beat],
[KEYS.POSTS, fetchAttestantPosts],
[KEYS.RSS, fetchRSS],
[KEYS.GITHUB_REPO_DATA, fetchGithubRepoData],
[KEYS.EVENTS, fetchEvents],
[KEYS.DEVELOPER_TOOLS, fetchDeveloperTools],
[KEYS.TRANSLATION_GLOSSARY, fetchTranslationGlossary],
[KEYS.STAKED_PERCENTAGE, fetchStakedPercentage],
[KEYS.VIDEO_THUMBNAILS, fetchVideoThumbnails],
]
const HOURLY: TaskDef[] = [
[KEYS.ETHEREUM_STABLECOINS_MCAP, fetchEthereumStablecoinsMcap],
[KEYS.GAS_PRICE, fetchGasPrice],
[KEYS.TOTAL_ETH_STAKED, fetchTotalEthStaked],
[KEYS.TOTAL_VALUE_LOCKED, fetchTotalValueLocked],
[KEYS.STABLECOINS_DATA, fetchStablecoinsData],
]
// ─── Dynamic task creation ───
function createDataTask([key, fetchFn]: TaskDef) {
return task({
id: key,
retry: {
maxAttempts: 2,
},
catchError: async ({ error }) => {
logger.error(`[${key}] failed`, { error })
},
run: async () => {
const data = await fetchFn()
await set(key, data)
logger.info(`✓ ${key}`)
return { key }
},
})
}
const ethereumMetricsFetchTask = task({
id: KEYS.ETH_PRICE,
retry: {
maxAttempts: 2,
},
catchError: async ({ error }) => {
logger.error(`[${KEYS.ETH_PRICE}] failed`, { error })
},
run: async () => {
const { ethPrice, ethereumMarketcap } = await fetchEthereumMetrics()
await Promise.all([
set(KEYS.ETH_PRICE, ethPrice),
set(KEYS.ETHEREUM_MARKETCAP, ethereumMarketcap),
])
logger.info(`Stored ${KEYS.ETH_PRICE} + ${KEYS.ETHEREUM_MARKETCAP}`)
return { keys: [KEYS.ETH_PRICE, KEYS.ETHEREUM_MARKETCAP] }
},
})
const weeklyFetchTasks = WEEKLY.map(createDataTask)
const dailyFetchTasks = DAILY.map(createDataTask)
const hourlyFetchTasks = [ethereumMetricsFetchTask, ...HOURLY.map(createDataTask)]
// Must export for trigger.dev to discover
export const allFetchTasks = [
...weeklyFetchTasks,
...dailyFetchTasks,
...hourlyFetchTasks,
]
// ─── Scheduled orchestrators ───
export const weeklyTask = schedules.task({
id: "weekly-data-fetch",
cron: "0 0 * * 0", // Sundays at midnight UTC
run: () => Promise.all(weeklyFetchTasks.map((t) => t.trigger())),
})
export const dailyTask = schedules.task({
id: "daily-data-fetch",
cron: "0 0 * * *",
run: () => Promise.all(dailyFetchTasks.map((t) => t.trigger())),
})
export const hourlyTask = schedules.task({
id: "hourly-data-fetch",
cron: "0 * * * *",
run: () => Promise.all(hourlyFetchTasks.map((t) => t.trigger())),
})
// ─── Global failure handler → Sentry + Discord ───
tasks.onFailure(async ({ ctx, error }) => {
Sentry.captureException(error, {
tags: { module: "data-layer" },
extra: { taskId: ctx.task.id },
})
const webhookUrl = process.env.DISCORD_WEBHOOK_URL
if (!webhookUrl) return
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
embeds: [
{
title: `Data Fetch Failed: ${ctx.task.id}`,
color: 0xff0000,
description: String(error).slice(0, 2000),
timestamp: new Date().toISOString(),
},
],
}),
})
})