public DetermineMainJob()

in Tasks/JenkinsQueueJobV2/jobsearch.ts [106:181]


    public DetermineMainJob(executableNumber: number, callback): void {
        const thisSearch: JobSearch = this;
        if (!thisSearch.foundCauses[executableNumber]) {
            util.fail('No known exeuction number: ' + executableNumber + ' for job: ' + thisSearch.identifier);
        } else {
            const causes : any = thisSearch.foundCauses[executableNumber];
            const causesThatRan: Job[] = []; // these are all the causes for this executableNumber that are running/ran
            const causesThatMayRun: Job[] = []; // these are the causes for this executableNumber that could run in the future
            const causesThatWontRun: Job[] = []; // these are the causes for this executableNumber that will never run
            for (const i in causes) {
                const job: Job = thisSearch.queue.FindJob(causes[i].upstreamUrl, causes[i].upstreamBuild);
                if (job) { // we know about it
                    if (job.State === JobState.Streaming ||
                        job.State === JobState.Finishing ||
                        job.State === JobState.Downloading ||
                        job.State === JobState.Queued ||
                        job.State === JobState.Done) {
                        causesThatRan.push(job);
                    } else if (job.State === JobState.New || job.State === JobState.Locating) {
                        causesThatMayRun.push(job);
                    } else if (job.State === JobState.Joined || job.State === JobState.Cut) {
                        causesThatWontRun.push(job);
                    } else {
                        util.fail('Illegal state: ' + job);
                    }
                }
            }

            let masterJob: Job = null; // there can be only one
            let potentialMasterJobs: Job[] = []; // the list of all potential jobs that could be master
            for (const i in causesThatRan) {
                const causeThatRan: Job = causesThatRan[i];
                const child: Job = findChild(causeThatRan);
                if (child != null) {
                    if (child.State === JobState.Streaming || child.State === JobState.Finishing || child.State === JobState.Done) {
                        if (masterJob == null) {
                            masterJob = child;
                        } else {
                            util.fail('Can not have more than one master: ' + child);
                        }
                    } else {
                        potentialMasterJobs.push(child);
                    }
                }
            }

            if (masterJob == null && potentialMasterJobs.length > 0) {
                masterJob = potentialMasterJobs[0]; // simply assign the first one to master
                potentialMasterJobs = potentialMasterJobs.slice(1); // and remove it from here
            }

            let secondaryJobs: Job[] = [];
            if (masterJob != null) { // secondaryJobs are only possible once a master is assigned
                secondaryJobs = potentialMasterJobs;
                for (const i in causesThatWontRun) {
                    const causeThatWontRun: Job = causesThatWontRun[i];
                    const child: Job = findChild(causeThatWontRun);
                    if (child != null) {
                        secondaryJobs.push(child);
                    }
                }
            }

            callback(masterJob, secondaryJobs);

            function findChild(parent: Job): Job {
                for (const i in parent.Children) {
                    const child: Job = parent.Children[i];
                    if (thisSearch.identifier === child.Identifier) {
                        return child;
                    }
                }
                return null;
            }
        }
    }