chore: clean sentry in headless

This commit is contained in:
devthejo 2025-07-02 00:59:06 +02:00
parent 27ead01714
commit 5bf3f9b6f9

173
index.js
View file

@ -125,111 +125,24 @@ const HeadlessTask = async (event) => {
throw new Error("Invalid event name received"); throw new Error("Invalid event name received");
} }
// Add initial breadcrumb
Sentry.addBreadcrumb({
message: "HeadlessTask started",
category: "headless-task",
level: "info",
data: {
eventName: name,
params: params ? JSON.stringify(params) : null,
timestamp: Date.now(),
},
});
geolocBgLogger.info("HeadlessTask event received", { name, params }); geolocBgLogger.info("HeadlessTask event received", { name, params });
switch (name) { switch (name) {
case "heartbeat": case "heartbeat":
// Add breadcrumb for heartbeat event
Sentry.addBreadcrumb({
message: "Heartbeat event received",
category: "headless-task",
level: "info",
timestamp: Date.now() / 1000,
});
// Get persisted last sync time // Get persisted last sync time
const lastSyncTime = await getLastSyncTime(); const lastSyncTime = await getLastSyncTime();
const now = Date.now(); const now = Date.now();
const timeSinceLastSync = now - lastSyncTime; const timeSinceLastSync = now - lastSyncTime;
// Add context about sync timing
Sentry.setContext("sync-timing", {
lastSyncTime: new Date(lastSyncTime).toISOString(),
currentTime: new Date(now).toISOString(),
timeSinceLastSync: timeSinceLastSync,
timeSinceLastSyncHours: (
timeSinceLastSync /
(1000 * 60 * 60)
).toFixed(2),
needsForceSync: timeSinceLastSync >= FORCE_SYNC_INTERVAL,
});
Sentry.addBreadcrumb({
message: "Sync timing calculated",
category: "headless-task",
level: "info",
data: {
timeSinceLastSyncHours: (
timeSinceLastSync /
(1000 * 60 * 60)
).toFixed(2),
needsForceSync: timeSinceLastSync >= FORCE_SYNC_INTERVAL,
},
});
// Get current position with performance tracking // Get current position with performance tracking
const locationStartTime = Date.now();
const location = await getCurrentPosition(); const location = await getCurrentPosition();
const locationDuration = Date.now() - locationStartTime;
const isLocationError = location && location.code !== undefined;
Sentry.addBreadcrumb({
message: "getCurrentPosition completed",
category: "headless-task",
level: isLocationError ? "warning" : "info",
data: {
success: !isLocationError,
error: isLocationError ? location : undefined,
coords: !isLocationError ? location?.coords : undefined,
},
});
geolocBgLogger.debug("getCurrentPosition result", { location }); geolocBgLogger.debug("getCurrentPosition result", { location });
if (timeSinceLastSync >= FORCE_SYNC_INTERVAL) { if (timeSinceLastSync >= FORCE_SYNC_INTERVAL) {
geolocBgLogger.info("Forcing location sync after 24h"); geolocBgLogger.info("Forcing location sync after 24h");
Sentry.addBreadcrumb({
message: "Force sync triggered",
category: "headless-task",
level: "info",
data: {
timeSinceLastSyncHours: (
timeSinceLastSync /
(1000 * 60 * 60)
).toFixed(2),
},
});
try { try {
// Get pending records count before sync with timeout
const pendingCount = await Promise.race([
BackgroundGeolocation.getCount(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("getCount timeout")), 10000),
),
]);
Sentry.addBreadcrumb({
message: "Pending records count",
category: "headless-task",
level: "info",
data: { pendingCount },
});
// Change pace to ensure location updates with timeout // Change pace to ensure location updates with timeout
await Promise.race([ await Promise.race([
BackgroundGeolocation.changePace(true), BackgroundGeolocation.changePace(true),
@ -241,12 +154,6 @@ const HeadlessTask = async (event) => {
), ),
]); ]);
Sentry.addBreadcrumb({
message: "changePace completed",
category: "headless-task",
level: "info",
});
// Perform sync with timeout // Perform sync with timeout
const syncResult = await Promise.race([ const syncResult = await Promise.race([
BackgroundGeolocation.sync(), BackgroundGeolocation.sync(),
@ -255,26 +162,8 @@ const HeadlessTask = async (event) => {
), ),
]); ]);
Sentry.addBreadcrumb({
message: "Sync completed successfully",
category: "headless-task",
level: "info",
data: {
syncResult: Array.isArray(syncResult)
? `${syncResult.length} records`
: "completed",
},
});
// Update last sync time after successful sync // Update last sync time after successful sync
await setLastSyncTime(now); await setLastSyncTime(now);
Sentry.addBreadcrumb({
message: "Last sync time updated",
category: "headless-task",
level: "info",
data: { newSyncTime: new Date(now).toISOString() },
});
} catch (syncError) { } catch (syncError) {
Sentry.captureException(syncError, { Sentry.captureException(syncError, {
tags: { tags: {
@ -292,22 +181,6 @@ const HeadlessTask = async (event) => {
geolocBgLogger.error("Force sync failed", { error: syncError }); geolocBgLogger.error("Force sync failed", { error: syncError });
} }
} else {
Sentry.addBreadcrumb({
message: "Force sync not needed",
category: "headless-task",
level: "info",
data: {
timeSinceLastSyncHours: (
timeSinceLastSync /
(1000 * 60 * 60)
).toFixed(2),
nextSyncInHours: (
(FORCE_SYNC_INTERVAL - timeSinceLastSync) /
(1000 * 60 * 60)
).toFixed(2),
},
});
} }
break; break;
@ -318,17 +191,6 @@ const HeadlessTask = async (event) => {
break; break;
} }
Sentry.addBreadcrumb({
message: "Location update received",
category: "headless-task",
level: "info",
data: {
coords: params.location?.coords,
activity: params.location?.activity,
hasLocation: !!params.location,
},
});
geolocBgLogger.debug("Location update received", { geolocBgLogger.debug("Location update received", {
location: params.location, location: params.location,
}); });
@ -344,17 +206,6 @@ const HeadlessTask = async (event) => {
const httpStatus = params.response?.status; const httpStatus = params.response?.status;
const isHttpSuccess = httpStatus === 200; const isHttpSuccess = httpStatus === 200;
Sentry.addBreadcrumb({
message: "HTTP response received",
category: "headless-task",
level: isHttpSuccess ? "info" : "warning",
data: {
status: httpStatus,
success: params.response?.success,
hasResponse: !!params.response,
},
});
geolocBgLogger.debug("HTTP response received", { geolocBgLogger.debug("HTTP response received", {
response: params.response, response: params.response,
}); });
@ -364,13 +215,6 @@ const HeadlessTask = async (event) => {
try { try {
const now = Date.now(); const now = Date.now();
await setLastSyncTime(now); await setLastSyncTime(now);
Sentry.addBreadcrumb({
message: "Last sync time updated (HTTP success)",
category: "headless-task",
level: "info",
data: { newSyncTime: new Date(now).toISOString() },
});
} catch (syncTimeError) { } catch (syncTimeError) {
geolocBgLogger.error("Failed to update sync time", { geolocBgLogger.error("Failed to update sync time", {
error: syncTimeError, error: syncTimeError,
@ -387,26 +231,11 @@ const HeadlessTask = async (event) => {
break; break;
default: default:
Sentry.addBreadcrumb({ break;
message: "Unknown event type",
category: "headless-task",
level: "warning",
data: { eventName: name },
});
} }
// Task completed successfully // Task completed successfully
const taskDuration = Date.now() - taskStartTime; const taskDuration = Date.now() - taskStartTime;
Sentry.addBreadcrumb({
message: "HeadlessTask completed successfully",
category: "headless-task",
level: "info",
data: {
eventName: name,
duration: taskDuration,
},
});
} catch (error) { } catch (error) {
const taskDuration = Date.now() - taskStartTime; const taskDuration = Date.now() - taskStartTime;