1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2025-03-06 16:16:24 +02:00

refactor coverage cleanup

This commit is contained in:
Dima 2024-12-16 10:18:51 +03:00
parent 25dc5e412c
commit ba1dd4606d
9 changed files with 57 additions and 95 deletions

View File

@ -61,6 +61,10 @@ class Bdd implements Serializable, Coverable {
}
String getStageSlug() {
return "bdd"
}
String getCoverageStashPath() {
return COVERAGE_STASH_PATH
}

View File

@ -2,6 +2,7 @@ package ru.pulsar.jenkins.library.steps
interface Coverable {
String getStageSlug();
String getCoverageStashPath();
String getCoverageStashName();
String getCoveragePidsPath();

View File

@ -1,52 +0,0 @@
package ru.pulsar.jenkins.library.steps
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.Logger
class CoverageCleanup implements Serializable {
private final JobConfiguration config
private final String stageName
CoverageCleanup(JobConfiguration config, String stageName = "") {
this.config = config
this.stageName = stageName
}
def run() {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
Logger.printLocation()
String pidsFilePath = "build${File.separator}${stageName}-pids"
def pids = ""
if (steps.fileExists(pidsFilePath)) {
pids = steps.readFile(pidsFilePath)
}
if (pids.isEmpty()) {
Logger.println("Нет запущенных процессов dbgs и Coverage41C")
return
}
Logger.println("Завершение процессов dbgs и Coverage41C с pid: $pids")
def command
if (steps.isUnix()) {
command = "kill $pids"
} else {
def pidsForCmd = ''
def pidsArray = pids.split(" ")
pidsArray.each {
pidsForCmd += "/PID $it"
}
pidsForCmd = pidsForCmd.trim()
command = "taskkill $pidsForCmd /F"
}
steps.cmd(command, true, false)
}
}

View File

@ -121,6 +121,10 @@ class SmokeTest implements Serializable, Coverable {
}
}
String getStageSlug() {
return "bdd"
}
String getCoverageStashPath() {
return COVERAGE_STASH_PATH
}

View File

@ -5,6 +5,7 @@ import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
import ru.pulsar.jenkins.library.utils.Logger
class WithCoverage implements Serializable {
@ -22,28 +23,58 @@ class WithCoverage implements Serializable {
def run() {
def context = CoverageUtils.prepareContext(config, coverageOptions)
if (!coverageOptions.coverage) {
body()
return
}
def context = CoverageUtils.prepareContext(config, coverageOptions)
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
//noinspection GroovyMissingReturnStatement
steps.lock(context.lockableResource) {
try {
if (coverageOptions.coverage) {
CoverageUtils.startCoverage(steps, config, context, stage)
}
CoverageUtils.startCoverage(steps, config, context, stage)
body()
steps.stash(stage.getCoverageStashName(), stage.getCoverageStashPath(), true)
} catch (Exception e) {
throw new Exception("При выполнении блока произошла ошибка: ${e}")
} finally {
if (coverageOptions.coverage) {
CoverageUtils.stopCoverage(steps, config, context)
}
}
if (coverageOptions.coverage) {
steps.stash(stage.getCoverageStashName(), stage.getCoverageStashPath(), true)
CoverageUtils.stopCoverage(steps, config, context)
String pidsFilePath = "build/${stage.getStageSlug()}-pids"
def pids = ""
if (steps.fileExists(pidsFilePath)) {
pids = steps.readFile(pidsFilePath)
}
if (pids.isEmpty()) {
Logger.println("Нет запущенных процессов dbgs и Coverage41C")
return
}
Logger.println("Завершение процессов dbgs и Coverage41C с pid: $pids")
def command
if (steps.isUnix()) {
command = "kill $pids || true"
} else {
def pidsForCmd = ''
def pidsArray = pids.split(" ")
pidsArray.each {
pidsForCmd += "/PID $it"
}
pidsForCmd = pidsForCmd.trim()
command = "taskkill $pidsForCmd /F > nul"
}
steps.cmd(command, false, false)
}
}
}

View File

@ -96,6 +96,10 @@ class Yaxunit implements Serializable, Coverable {
}
}
String getStageSlug() {
return "yaxunit"
}
String getCoverageStashPath() {
return COVERAGE_STASH_PATH
}

View File

@ -1,6 +1,5 @@
package ru.pulsar.jenkins.library.utils
import org.apache.commons.lang3.RandomStringUtils
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
@ -42,10 +41,7 @@ class CoverageUtils {
def port = options.dbgsPort
def currentDbgsPids = getPIDs("dbgs")
def currentCoverage41CPids = getPIDs("Coverage41C")
def lockableResource = RandomStringUtils.random(9, true, false)
if (options.coverage) {
lockableResource = "${env.NODE_NAME}_$port"
}
def lockableResource = "${env.NODE_NAME}_$port"
return new CoverageContext(lockableResource, config.srcDir, coverageOpts, port, currentDbgsPids, currentCoverage41CPids)
@ -76,7 +72,7 @@ class CoverageUtils {
steps.writeFile(stage.getCoveragePidsPath(), pids, 'UTF-8')
Logger.println("Coverage PIDs for cleanup: $pids")
Logger.println("PID процессов dbgs и Coverage41C для ${stage.getStageSlug()}: $pids")
}
static void stopCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext) {

View File

@ -1,11 +0,0 @@
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.steps.CoverageCleanup
def call(JobConfiguration config, String stageName) {
ContextRegistry.registerDefaultContext(this)
def coverageCleanup = new CoverageCleanup(config, stageName)
coverageCleanup.run()
}

View File

@ -236,11 +236,6 @@ void call() {
bdd config
}
}
post {
cleanup {
coverageCleanup config, 'bdd'
}
}
}
}
}
@ -293,11 +288,6 @@ void call() {
smoke config
}
}
post {
cleanup {
coverageCleanup config, 'smoke'
}
}
}
}
}
@ -335,11 +325,6 @@ void call() {
yaxunit config
}
}
post {
cleanup {
coverageCleanup config, 'yaxunit'
}
}
}
}
}