async function collapseBuilds({ waitingKey, buildId, blockingBuildIds }) {
    let waitingBuilds = await this.queueObject.connection.redis.lrange(waitingKey, 0, -1);

    if (waitingBuilds.length > 0) {
        waitingBuilds = waitingBuilds.map(bId => parseInt(bId, 10));
        waitingBuilds.sort((a, b) => a - b);
        const lastWaitingBuild = waitingBuilds.slice(-1)[0];
        let buildsToCollapse = waitingBuilds;

        winston.info('current buildId:', buildId);
        winston.info('lastWaitingBuild:', lastWaitingBuild);

        // Current build is an older build, do not re-enqueued, return immediately
        if (buildId < lastWaitingBuild) return;

        // If buildId == lastWaitingBuild, keep the last one in the waiting queue
        if (buildId === lastWaitingBuild) {
            buildsToCollapse = buildsToCollapse.slice(0, -1);
        }

        winston.info('buildsToCollapse:', buildsToCollapse);

        const rmBuilds = buildsToCollapse.map(async (bId) => {
            const count = await this.queueObject.connection.redis.lrem(waitingKey, 0, bId);

            // if the build is no longer in the waiting queue, don't collapse it
            if (count > 0) {
                await helper.updateBuildStatus({
                    redisInstance: this.queueObject.connection.redis,
                    buildId: bId,
                    status: 'COLLAPSED',
                    statusMessage: `Collapsed to build: ${buildId}`
                }, () => {});
                await this.queueObject.connection.redis.hdel(`${queuePrefix}buildConfigs`, bId);
            }
        });

        await Promise.all(rmBuilds);
    }

    // re-enqueue the current build after collapse
    await this.reEnqueue(waitingKey, buildId, blockingBuildIds);
}
/**
 * collapse waiting builds and re-enequeue the current build if it is the latest one
 * @method collapseBuilds
 * @param  {String}  waitingKey           ${waitingJobsPrefix}${jobId}
 * @param  {Number}  buildId              Current build Id
 * @param  {Array}   blockingBuildIds     List of build Ids that are blocking this current build
 */
collapse waiting builds and re-enequeue the current build if it is the latest one
@method collapseBuilds
@param  {String}  waitingKey           ${waitingJobsPrefix}${jobId}
@param  {Number}  buildId              Current build Id
@param  {Array}   blockingBuildIds     List of build Ids that are blocking this current build
collapse waiting builds and re - enequeue the current build if it is the latest one @method collapseBuilds @param { String } waitingKey $ { waitingJobsPrefix } $ { jobId } @param { Number } buildId Current build Id @param { Array } blockingBuildIds List of build Ids that are blocking this current build
async function collapseBuilds({ waitingKey, buildId, blockingBuildIds }) {
    let waitingBuilds = await this.queueObject.connection.redis.lrange(waitingKey, 0, -1);
    if (waitingBuilds.length > 0) {
        waitingBuilds = waitingBuilds.map(bId => parseInt(bId, 10));
        waitingBuilds.sort((a, b) => a - b);
        const lastWaitingBuild = waitingBuilds.slice(-1)[0];
        let buildsToCollapse = waitingBuilds;
        winston.info('current buildId:', buildId);
        winston.info('lastWaitingBuild:', lastWaitingBuild);
        if (buildId < lastWaitingBuild) return;
        if (buildId === lastWaitingBuild) {
            buildsToCollapse = buildsToCollapse.slice(0, -1);
        }
        winston.info('buildsToCollapse:', buildsToCollapse);
        const rmBuilds = buildsToCollapse.map(async (bId) => {
            const count = await this.queueObject.connection.redis.lrem(waitingKey, 0, bId);
            if (count > 0) {
                await helper.updateBuildStatus({
                    redisInstance: this.queueObject.connection.redis,
                    buildId: bId,
                    status: 'COLLAPSED',
                    statusMessage: `Collapsed to build: ${buildId}`
                }, () => {});
                await this.queueObject.connection.redis.hdel(`${queuePrefix}buildConfigs`, bId);
            }
        });
        await Promise.all(rmBuilds);
    }
    await this.reEnqueue(waitingKey, buildId, blockingBuildIds);
}
async function collapseBuilds ( { waitingKey , buildId , blockingBuildIds } ) { let waitingBuilds = await this . queueObject . connection . redis . lrange ( waitingKey , 0 , - 1 ) ; if ( waitingBuilds . length > 0 ) { waitingBuilds = waitingBuilds . map ( bId => parseInt ( bId , 10 ) ) ; waitingBuilds . sort ( ( a , b ) => a - b ) ; const lastWaitingBuild = waitingBuilds . slice ( - 1 ) [ 0 ] ; let buildsToCollapse = waitingBuilds ; winston . info ( 'current buildId:' , buildId ) ; winston . info ( 'lastWaitingBuild:' , lastWaitingBuild ) ; if ( buildId < lastWaitingBuild ) return ; if ( buildId === lastWaitingBuild ) { buildsToCollapse = buildsToCollapse . slice ( 0 , - 1 ) ; } winston . info ( 'buildsToCollapse:' , buildsToCollapse ) ; const rmBuilds = buildsToCollapse . map ( async ( bId ) => { const count = await this . queueObject . connection . redis . lrem ( waitingKey , 0 , bId ) ; if ( count > 0 ) { await helper . updateBuildStatus ( { redisInstance : this . queueObject . connection . redis , buildId : bId , status : 'COLLAPSED' , statusMessage : ` ${ buildId } ` } , ( ) => { } ) ; await this . queueObject . connection . redis . hdel ( ` ${ queuePrefix } ` , bId ) ; } } ) ; await Promise . all ( rmBuilds ) ; } await this . reEnqueue ( waitingKey , buildId , blockingBuildIds ) ; }
collapse waiting builds and re-enequeue the current build if it is the latest one
 @method collapseBuilds
 @param  {String}  waitingKey           ${waitingJobsPrefix}${jobId}
 @param  {Number}  buildId              Current build Id
 @param  {Array}   blockingBuildIds     List of build Ids that are blocking this current build
collapse waiting builds and re - enequeue the current build if it is the latest one @method collapseBuilds @param { String } waitingKey $ { waitingJobsPrefix } $ { jobId } @param { Number } buildId Current build Id @param { Array } blockingBuildIds List of build Ids that are blocking this current build
// Current build is an older build, do not re-enqueued, return immediately // If buildId == lastWaitingBuild, keep the last one in the waiting queue // if the build is no longer in the waiting queue, don't collapse it // re-enqueue the current build after collapse