async function confirmTaskCompletion()

in packages/aws-rfdk/lib/lambdas/nodejs/export-logs/index.ts [26:62]


async function confirmTaskCompletion(taskId: string): Promise<void> {
  const cloudwatchlogs = new CloudWatchLogsClient();

  let errorCount = 0;
  let complete = false;
  while (!complete) {
    try {
      const response = await cloudwatchlogs.send(new DescribeExportTasksCommand({ taskId }));
      if (response.exportTasks?.length !== 1) {
        throw new Error(`Received ${response.exportTasks?.length} export tasks from DescribeExportTasks for task ${taskId}.`);
      }

      const task = response.exportTasks[0];
      if (!task.status || !task.status.code) {
        throw new Error(`Task ${taskId} did not return a status code.`);
      }

      const taskStatus = task.status.code;
      if (taskStatus === 'RUNNING' || taskStatus.indexOf('PENDING') !== -1) {
        await sleep(500);
      } else if (taskStatus === 'FAILED' || taskStatus === 'CANCELLED') {
        throw new Error(`Task ${taskId} failed with status code: ${taskStatus}`);
      } else {
        console.log(`${taskId}: Task has completed successfully!`);
        complete = true;
      }
    } catch (e) {
      // Retry 3 times before giving up
      if (errorCount < 3) {
        console.error(`${taskId}: Encountered failure #${errorCount} with message: ${(e as Error)?.message}`);
        errorCount++;
      } else {
        throw e;
      }
    }
  }
}