All files / server/src/utils cron.ts

84.26% Statements 75/89
80% Branches 24/30
100% Functions 8/8
84.26% Lines 75/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                                  39x   39x   39x     16x   16x 16x     16x       16x     39x           2x     2x 2x             2x 1x   1x                 1x     2x 1x   1x                 1x       39x 3x 2x     3x             39x 14x     14x 14x                 14x 14x 10x 10x     4x   2x         2x 2x     2x         2x     39x 7x   7x 2x     5x 5x     5x 2x 2x 2x                   3x   3x 3x 1x         1x     2x     39x 7x   7x   7x 2x     5x 5x 5x 2x 2x 2x                 3x   3x   3x 3x         3x           3x   3x   3x       39x                   39x          
import { Cron } from "croner";
import dayjs from "dayjs";
import { logger } from "server/utils/logger";
import { config } from "shared/config";
import { runAssignment } from "server/features/assignment/run-assignment/runAssignment";
import { Result, makeSuccessResult } from "shared/utils/result";
import { updateProgramItems } from "server/features/program-item/programItemService";
import {
  acquireAssignmentLock,
  getLatestServerStartTime,
  releaseAssignmentLock,
  saveSettings,
  setAssignmentLastRun,
  setProgramUpdateLastRun,
} from "server/features/settings/settingsRepository";
import { MongoDbError } from "shared/types/api/errors";
 
const cronJobs: Cron[] = [];
 
let instanceStartTime = "";
 
export const setLatestServerStartTime = async (): Promise<
  Result<void, MongoDbError>
> => {
  instanceStartTime = dayjs().toISOString();
 
  logger.info(`Set latestServerStartTime ${instanceStartTime}`);
  const settingsResult = await saveSettings({
    latestServerStartTime: instanceStartTime,
  });
  Iif (!settingsResult.ok) {
    return settingsResult;
  }
 
  return makeSuccessResult();
};
 
export const startCronJobs = async (): Promise<void> => {
  const {
    autoUpdateProgramEnabled,
    programUpdateInterval,
    autoAssignAttendeesEnabled,
    autoAssignInterval,
  } = config.server();
 
  // Save latest server instance start time to limit running cronjobs to latest started instance
  const latestServerStartResult = await setLatestServerStartTime();
  Iif (!latestServerStartResult.ok) {
    // eslint-disable-next-line no-restricted-syntax -- Server startup
    throw new Error(
      `Error setting latestServerStartTime at server start: ${latestServerStartResult.error}`,
    );
  }
 
  if (autoUpdateProgramEnabled) {
    logger.info("Start cronjob: program auto update");
 
    const autoUpdateProgramItemsJob = new Cron(
      programUpdateInterval,
      {
        name: "autoUpdateProgramItems",
        protect: protectCallback,
        catch: errorHandler,
      },
      autoUpdateProgramItems,
    );
    cronJobs.push(autoUpdateProgramItemsJob);
  }
 
  if (autoAssignAttendeesEnabled) {
    logger.info("Start cronjob: automatic attendee assignment");
 
    const autoAssignAttendeesJob = new Cron(
      autoAssignInterval,
      {
        name: "autoAssignAttendees",
        protect: protectCallback,
        catch: errorHandler,
      },
      autoAssignAttendees,
    );
    cronJobs.push(autoAssignAttendeesJob);
  }
};
 
export const stopCronJobs = (): void => {
  for (const job of cronJobs) {
    job.stop();
  }
 
  logger.info("CronJobs stopped");
};
 
// A deploy starts a replacement instance which overwrites latestServerStartTime, so the
// superseded instance seeing a newer time is expected: stop its cronjobs and let it wait for
// termination. A missing or older stored time means the settings data was lost or rewound,
// which shouldn't happen and is logged as an error
const isLatestServerInstance = async (): Promise<boolean> => {
  logger.info(
    `Check if latest running server instance with start time ${instanceStartTime}`,
  );
  const dbLatestStartTimeResult = await getLatestServerStartTime();
  Iif (!dbLatestStartTimeResult.ok) {
    logger.error(
      new Error(
        `Cronjobs: Failed to get latest server start time: ${dbLatestStartTimeResult.error}`,
      ),
    );
    return false;
  }
 
  const dbLatestStartTime = dbLatestStartTimeResult.value;
  if (dbLatestStartTime === instanceStartTime) {
    logger.info("Latest server start time found, is latest");
    return true;
  }
 
  if (dayjs(dbLatestStartTime).isAfter(instanceStartTime)) {
    // TODO: Expected during deploys, change to info level once stopping cronjobs is verified to work
    logger.error(
      new Error(
        "Cronjobs: Newer server instance running, stopping cronjobs on this instance",
      ),
    );
    stopCronJobs();
    return false;
  }
 
  logger.error(
    new Error(
      `Cronjobs: Stored server start time ${dbLatestStartTime} is older than this instance's start time ${instanceStartTime}`,
    ),
  );
  return false;
};
 
export const autoUpdateProgramItems = async (): Promise<void> => {
  logger.info("----> Auto update program items");
 
  if (!(await isLatestServerInstance())) {
    return;
  }
 
  logger.info("Check if auto update already running...");
  const programUpdateLastRunResult = await setProgramUpdateLastRun(
    dayjs().toISOString(),
  );
  if (!programUpdateLastRunResult.ok) {
    Eif (programUpdateLastRunResult.error === MongoDbError.SETTINGS_NOT_FOUND) {
      logger.error(new Error("Program auto update already running, stop"));
      return;
    }
    logger.error(
      new Error(
        `***** Program items auto update failed trying to set last run time: ${programUpdateLastRunResult.error}`,
      ),
    );
    return;
  }
 
  logger.info("Auto update not running, continue");
 
  const updateProgramItemsResult = await updateProgramItems();
  if (updateProgramItemsResult.status === "error") {
    logger.error(
      new Error(
        `***** Program items auto update failed: ${updateProgramItemsResult.message}`,
      ),
    );
    return;
  }
 
  logger.info("***** Program items auto update completed");
};
 
export const autoAssignAttendees = async (): Promise<void> => {
  const { autoAssignDelay } = config.server();
 
  logger.info("----> Auto assign attendees");
 
  if (!(await isLatestServerInstance())) {
    return;
  }
 
  logger.info("Check if assignment already running...");
  const lockResult = await acquireAssignmentLock();
  if (!lockResult.ok) {
    Eif (lockResult.error === MongoDbError.ASSIGNMENT_LOCK_HELD) {
      logger.error(new Error("Auto assignment already running, stop"));
      return;
    }
    logger.error(
      new Error(
        `***** Auto assignment failed trying to acquire assignment lock: ${lockResult.error}`,
      ),
    );
    return;
  }
  const lockToken = lockResult.value;
 
  logger.info("Auto assignment not running, continue");
 
  try {
    const runAssignmentResult = await runAssignment({
      assignmentAlgorithm: config.event().assignmentAlgorithm,
      assignmentTime: null,
      assignmentDelay: autoAssignDelay,
    });
    Iif (!runAssignmentResult.ok) {
      logger.error(new Error("***** Auto assignment failed"));
      return;
    }
 
    // Record the last successful run time
    await setAssignmentLastRun(dayjs().toISOString());
 
    logger.info("***** Automatic attendee assignment completed");
  } finally {
    await releaseAssignmentLock(lockToken);
  }
};
 
const protectCallback = (job: Cron): void => {
  const timeNow = dayjs().toISOString();
  const startTime = dayjs(job.currentRun()).toISOString();
  logger.error(
    new Error(
      `Cronjob ${job.name} at ${timeNow} was blocked by call started at ${startTime}`,
    ),
  );
};
 
const errorHandler = (error: unknown, job: Cron): void => {
  logger.error(
    new Error(`Error while running cronJob ${job.name}`, { cause: error }),
  );
};