From aea6ddad5b4548221fb7865cd543501be2a68d9e Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 5 Jun 2022 09:07:34 -0600 Subject: [PATCH] Attempt to stop all daemons on Job completion --- src/job-summary.ts | 3 +-- src/setup-gradle.ts | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/job-summary.ts b/src/job-summary.ts index dbed552..4948c89 100644 --- a/src/job-summary.ts +++ b/src/job-summary.ts @@ -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 { diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index 993dd65..1170015 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -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 { 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 { + const executions: Promise[] = [] + 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) +}