Run Olist sync daily at 10am
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m11s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m11s
This commit is contained in:
@@ -43,7 +43,7 @@ module.exports = {
|
|||||||
OLIST_FRONTEND_URL: process.env.OLIST_FRONTEND_URL || '',
|
OLIST_FRONTEND_URL: process.env.OLIST_FRONTEND_URL || '',
|
||||||
OLIST_TOKEN_ENCRYPTION_KEY: process.env.OLIST_TOKEN_ENCRYPTION_KEY || '',
|
OLIST_TOKEN_ENCRYPTION_KEY: process.env.OLIST_TOKEN_ENCRYPTION_KEY || '',
|
||||||
OLIST_SYNC_ENABLED: process.env.OLIST_SYNC_ENABLED === 'true',
|
OLIST_SYNC_ENABLED: process.env.OLIST_SYNC_ENABLED === 'true',
|
||||||
OLIST_SYNC_INTERVAL_MS: Number(process.env.OLIST_SYNC_INTERVAL_MS) || 24 * 60 * 60 * 1000,
|
OLIST_SYNC_HOUR: 10,
|
||||||
OLIST_SYNC_REQUEST_DELAY_MS: Math.max(2_500, Number(process.env.OLIST_SYNC_REQUEST_DELAY_MS) || 2_500),
|
OLIST_SYNC_REQUEST_DELAY_MS: Math.max(2_500, Number(process.env.OLIST_SYNC_REQUEST_DELAY_MS) || 2_500),
|
||||||
TURNSTILE_SITE_KEY,
|
TURNSTILE_SITE_KEY,
|
||||||
TURNSTILE_SECRET
|
TURNSTILE_SECRET
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const {
|
|||||||
OLIST_CLIENT_SECRET,
|
OLIST_CLIENT_SECRET,
|
||||||
OLIST_FRONTEND_URL,
|
OLIST_FRONTEND_URL,
|
||||||
OLIST_REDIRECT_URI,
|
OLIST_REDIRECT_URI,
|
||||||
OLIST_SYNC_INTERVAL_MS,
|
OLIST_SYNC_HOUR,
|
||||||
OLIST_SYNC_REQUEST_DELAY_MS,
|
OLIST_SYNC_REQUEST_DELAY_MS,
|
||||||
OLIST_TOKEN_ENCRYPTION_KEY
|
OLIST_TOKEN_ENCRYPTION_KEY
|
||||||
} = require('../config');
|
} = require('../config');
|
||||||
@@ -111,15 +111,27 @@ const listSyncRuns = async (limit = 5) => {
|
|||||||
return result.rows.map(mapRun);
|
return result.rows.map(mapRun);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLastCompletedSyncAt = async () => {
|
const hasScheduledSyncToday = async () => {
|
||||||
const result = await pool.query(`
|
const result = await pool.query(`
|
||||||
SELECT completed_at
|
SELECT EXISTS (
|
||||||
FROM olist_sync_runs
|
SELECT 1
|
||||||
WHERE status = 'completed' AND completed_at IS NOT NULL
|
FROM olist_sync_runs
|
||||||
ORDER BY completed_at DESC
|
WHERE trigger = 'scheduled'
|
||||||
LIMIT 1;
|
AND started_at >= (date_trunc('day', CURRENT_TIMESTAMP AT TIME ZONE 'America/Sao_Paulo') AT TIME ZONE 'America/Sao_Paulo')
|
||||||
|
) AS exists;
|
||||||
`);
|
`);
|
||||||
return result.rows[0]?.completed_at || null;
|
return Boolean(result.rows[0]?.exists);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getTodayOlistRunAt = () => {
|
||||||
|
const parts = new Intl.DateTimeFormat('en-CA', {
|
||||||
|
timeZone: 'America/Sao_Paulo',
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit'
|
||||||
|
}).formatToParts(new Date());
|
||||||
|
const value = (type) => parts.find(part => part.type === type)?.value;
|
||||||
|
return new Date(`${value('year')}-${value('month')}-${value('day')}T${String(OLIST_SYNC_HOUR).padStart(2, '0')}:00:00-03:00`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const logRunEvent = async (runId, {
|
const logRunEvent = async (runId, {
|
||||||
@@ -707,15 +719,15 @@ const requestOlistSyncStop = async () => {
|
|||||||
const scheduleOlistSync = async () => {
|
const scheduleOlistSync = async () => {
|
||||||
if (scheduledSync) clearTimeout(scheduledSync);
|
if (scheduledSync) clearTimeout(scheduledSync);
|
||||||
|
|
||||||
let delay = OLIST_SYNC_INTERVAL_MS;
|
let scheduledRunAt = getTodayOlistRunAt();
|
||||||
|
let delay = Math.max(0, scheduledRunAt.getTime() - Date.now());
|
||||||
try {
|
try {
|
||||||
const lastCompletedAt = await getLastCompletedSyncAt();
|
if (delay === 0 && await hasScheduledSyncToday()) {
|
||||||
const lastCompletedMs = lastCompletedAt ? new Date(lastCompletedAt).getTime() : Number.NaN;
|
scheduledRunAt.setUTCDate(scheduledRunAt.getUTCDate() + 1);
|
||||||
if (!Number.isNaN(lastCompletedMs)) {
|
delay = scheduledRunAt.getTime() - Date.now();
|
||||||
delay = Math.max(0, lastCompletedMs + OLIST_SYNC_INTERVAL_MS - Date.now());
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Could not calculate the next Olist sync:', error.message);
|
console.error('Could not calculate the next daily Olist sync:', error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
nextScheduledSyncAt = new Date(Date.now() + delay).toISOString();
|
nextScheduledSyncAt = new Date(Date.now() + delay).toISOString();
|
||||||
|
|||||||
Reference in New Issue
Block a user