Repository navigation
Expand file tree
/
Copy pathrefresh-runner.js
More file actions
59 lines (48 loc) · 1.7 KB
/
Copy pathrefresh-runner.js
File metadata and controls
59 lines (48 loc) · 1.7 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
const cron = require('node-cron');
const dotenv = require('dotenv');
const storage = require('./lib/storage');
const refresh = require('./lib/refresh');
dotenv.config();
const SCHEDULE = process.env.SCHEDULE_CRON || '0 3 * * *';
console.log(`🔄 Token Refresh Runner started`);
console.log(`📅 Schedule: ${SCHEDULE}`);
// Scheduled job: refresh all tokens at specified time
cron.schedule(SCHEDULE, async () => {
console.log(`\n⏰ [${new Date().toISOString()}] Running scheduled token refresh...`);
try {
const tokenIds = await storage.listIds();
console.log(`📊 Found ${tokenIds.length} tokens to refresh`);
for (const id of tokenIds) {
try {
await refresh.refreshTokenById(id);
console.log(`✅ Refreshed token: ${id}`);
} catch (error) {
console.error(`❌ Failed to refresh token ${id}:`, error.message);
}
}
console.log(`✨ Scheduled refresh completed\n`);
} catch (error) {
console.error(`❌ Scheduled refresh failed:`, error);
}
});
// Manual refresh endpoint for testing
const express = require('express');
const app = express();
app.use(express.json());
app.post('/refresh/:id', async (req, res) => {
try {
const { id } = req.params;
await refresh.refreshTokenById(id);
res.json({ success: true, message: `Token ${id} refreshed` });
} catch (error) {
console.error('Error refreshing token:', error);
res.status(500).json({ error: error.message });
}
});
app.get('/health', (req, res) => {
res.json({ status: 'ok', service: 'refresh-runner' });
});
const PORT = process.env.REFRESH_PORT || 3001;
app.listen(PORT, () => {
console.log(`🔄 Refresh manual endpoint available at http://localhost:${PORT}`);
});