async function waitForApplyArtifacts()

in tasks/Node/src/tasks/AzureDtlCreateVM/task.ts [318:354]


async function waitForApplyArtifacts(client: DevTestLabsClient, labVmId: string, waitMinutes: number): Promise<void> {
    if (waitMinutes <= 0) {
        return;
    }

    console.log(`Waiting for a maximum of ${convertToMinutesString(waitMinutes)} for apply artifacts operation to complete.`);

    const startWait: number = Date.now();
    let totalWaitMinutes: number = 0;
    let provisioningState: any;
    let continueWaiting: boolean = true;

    do {
        const waitSpanMinutes: number = new Date(Date.now() - startWait).getMinutes();
        totalWaitMinutes = Math.round(waitSpanMinutes);

        const expired: boolean = waitSpanMinutes >= waitMinutes;
        if (expired) {
            throw `Waited for more than ${convertToMinutesString(totalWaitMinutes)}. Failing the task.`;
        }

        const vm: DevTestLabsModels.VirtualMachinesGetResponse = await getLabVm(client, labVmId);

        provisioningState = vm.provisioningState;
        continueWaiting = areArtifactsInstalling(vm.artifacts);

        if (continueWaiting) {
            // The only time we have seen we possibly need to wait is if the ARM deployment completed prematurely,
            // for some unknown error, and the virtual machine is still applying artifacts. So, it is reasonable to
            // recheck at a maximum every 5 minutes.
            const ms: number = 1000 * Math.min(waitMinutes * 60, 300);
            await deployutil.sleep(ms);
        }
    } while (continueWaiting);

    console.log(`Waited for a total of ${convertToMinutesString(totalWaitMinutes)}. Latest provisioning state is ${provisioningState}.`);
}