Attempt to stop all daemons on Job completion

This commit is contained in:
Daz DeBoer 2022-06-05 09:07:34 -06:00
parent a7f880172e
commit aea6ddad5b
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
2 changed files with 36 additions and 4 deletions

View file

@ -13,10 +13,9 @@ export interface BuildResult {
get buildScanUri(): string
}
export function writeJobSummary(cacheListener: CacheListener): void {
export function writeJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): void {
core.info('Writing job summary')
const buildResults = loadBuildResults()
if (buildResults.length === 0) {
core.debug('No Gradle build results found. Summary table will not be generated.')
} else {

View file

@ -1,9 +1,12 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import * as caches from './caches'
import {CacheListener} from './cache-reporting'
import {writeJobSummary} from './job-summary'
import {BuildResult, loadBuildResults, writeJobSummary} from './job-summary'
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
@ -38,13 +41,18 @@ export async function complete(): Promise<void> {
return
}
const buildResults = loadBuildResults()
core.info('Stopping all Gradle daemons')
await stopAllDaemons(getUniqueGradleHomes(buildResults))
core.info('In final post-action step, saving state and writing summary')
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
const gradleUserHome = core.getState(GRADLE_USER_HOME)
await caches.save(gradleUserHome, cacheListener)
writeJobSummary(cacheListener)
writeJobSummary(buildResults, cacheListener)
}
function determineGradleUserHome(rootDir: string): string {
@ -55,3 +63,28 @@ function determineGradleUserHome(rootDir: string): string {
return path.resolve(os.homedir(), '.gradle')
}
function getUniqueGradleHomes(buildResults: BuildResult[]): string[] {
const gradleHomes = buildResults.map(buildResult => buildResult.gradleHomeDir)
return Array.from(new Set(gradleHomes))
}
async function stopAllDaemons(gradleHomes: string[]): Promise<void> {
const executions: Promise<number>[] = []
const args = ['--stop']
for (const gradleHome of gradleHomes) {
const executable = path.resolve(gradleHome, 'bin', 'gradle')
if (!fs.existsSync(executable)) {
core.warning(`Gradle executable not found at ${executable}. Could not stop Gradle daemons.`)
continue
}
core.info(`Stopping Gradle daemons in ${gradleHome}`)
executions.push(
exec.exec(executable, args, {
ignoreReturnCode: true
})
)
}
await Promise.all(executions)
}