Skip to content

Commit

Permalink
[JENKINS-70351] Fix initialization and deprecate obsolete SupportContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohbedoh committed Dec 29, 2022
1 parent dd0bed6 commit c613c40
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
* The implementation of {@link SupportContext}
*
* @author Stephen Connolly
* @deprecated usage removed
*/
@Deprecated
public class SupportContextImpl implements SupportContext {

@Deprecated
public SupportContextImpl() {
}

Expand All @@ -57,6 +60,7 @@ public HealthCheckRegistry getHealthCheckRegistry() {
return Metrics.healthCheckRegistry();
}

@Deprecated
public void shutdown() {
}
}
26 changes: 5 additions & 21 deletions src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import hudson.Plugin;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.AbstractCIBase;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Node;
Expand Down Expand Up @@ -645,31 +644,16 @@ public synchronized void start() throws Exception {
context = new SupportContextImpl();
}

/**
* @return the {@link com.cloudbees.jenkins.support.api.SupportContext}
* @deprecated usage removed
*/
@NonNull
@Deprecated
public synchronized SupportContextImpl getContext() {
return context;
}

@Override
public void postInitialize() throws Exception {
super.postInitialize();
for (Component component : getComponents()) {
try {
long initializerStart = System.currentTimeMillis();
component.start(getContext());
if (AbstractCIBase.LOG_STARTUP_PERFORMANCE) {
long delta = System.currentTimeMillis() - initializerStart;
logger.info(String.format("Took %dms for support component %s startup", delta, component.getId()));
}
} catch (Throwable t) {
LogRecord logRecord = new LogRecord(Level.WARNING, "Exception propagated from component: {0}");
logRecord.setThrown(t);
logRecord.setParameters(new Object[]{component.getDisplayName()});
logger.log(logRecord);
}
}
}

@Override
public synchronized void stop() throws Exception {
if (rootLogger != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public <C extends AbstractModelObject> boolean isApplicable(Class<C> clazz) {
return getClass().getSimpleName();
}

@Deprecated
public void start(@NonNull SupportContext context) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
* The context that a {@link Component} is being instantiated in.
*
* @author Stephen Connolly
* @deprecated usage removed
*/
@Deprecated
public interface SupportContext {
/**
* Returns the {@link MetricRegistry} for the current Jenkins.
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/cloudbees/jenkins/support/impl/CustomLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.FileContent;
import com.cloudbees.jenkins.support.api.SupportContext;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.init.Terminator;
import hudson.logging.LogRecorder;
import hudson.model.PeriodicWork;
Expand All @@ -17,6 +18,8 @@
import io.jenkins.lib.support_log_formatter.SupportLogFormatter;
import java.util.List;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -56,11 +59,6 @@ public String getDisplayName() {
return "Controller Custom Log Recorders";
}

@Override
public void start(@NonNull SupportContext context) {
Logger.getLogger("").addHandler(new CustomHandler());
}

@Override
public void addContents(@NonNull Container result) {
addLogRecorders(result);
Expand Down Expand Up @@ -125,6 +123,12 @@ void publish(LogRecord record) throws IOException {
}
}

@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.SYSTEM_CONFIG_LOADED)
@Restricted(NoExternalUse.class)
public void startCustomHandler() {
Logger.getLogger("").addHandler(new CustomHandler());
}

private final class CustomHandler extends Handler {

private final Map<String,LogFile> logFiles = new HashMap<String,LogFile>();
Expand Down

0 comments on commit c613c40

Please sign in to comment.