Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many Improvements #24

Merged
merged 16 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Truths which we believe to be self-evident (adapted from [TextSecure's](https://
1. **The answer is not more options.** If you feel compelled to add a preference that's exposed to the user, it's very possible you've made a wrong turn somewhere.
2. **There are no power users.** The idea that some users "understand" concepts better than others has proven to be, for the most part, false. If anything, "power users" are more dangerous than the rest, and we should avoid exposing dangerous functionality to them.
3. **If it's "like PGP," it's wrong.** PGP is our guide for what not to do.
4. **It's an asynchronous world.** We wary of anything that is anti-asynchronous: ACKs, protocol confirmations, or anly protocol-level "advisory" message.
5. **There is no such thing as time**. Protocol ideas that require synchonized clocks are doomed to failure.
4. **It's an asynchronous world.** We wary of anything that is anti-asynchronous: ACKs, protocol confirmations, or any protocol-level "advisory" message.
5. **There is no such thing as time**. Protocol ideas that require synchronized clocks are doomed to failure.

# Code Style Guidelines

Expand All @@ -26,7 +26,7 @@ Truths which we believe to be self-evident (adapted from [TextSecure's](https://
1. "hungarian"-style notation is banned (i.e. instance variable names preceded by an 'm', etc)
2. If the field is `static final` then it shall be named in `ALL_CAPS_WITH_UNDERSCORES`.
3. Start variable names with a lowercase letter and use camelCase rather than under_scores.
4. Spelling and abreviations: If the word is widely used in the JVM runtime, stick with the spelling/abreviation in the JVM runtime, e.g. `color` over `colour`, `sync` over `synch`, `async` over `asynch`, etc.
4. Spelling and abbreviations: If the word is widely used in the JVM runtime, stick with the spelling/abbreviation in the JVM runtime, e.g. `color` over `colour`, `sync` over `synch`, `async` over `asynch`, etc.
5. It is acceptable to use `i`, `j`, `k` for loop indices and iterators. If you need more than three, you are likely doing something wrong and as such you shall either use full descriptive names or refactor.
6. It is acceptable to use `e` for the exception in a `try...catch` block.
7. You shall never use `l` (i.e. lower case `L`) as a variable name.
Expand Down Expand Up @@ -96,7 +96,7 @@ To the greatest extent possible, please wrap lines to ensure that they do not ex
*/
@Override
```
* Getters and Setters shall have a Javadoc comment. The following is prefered
* Getters and Setters shall have a Javadoc comment. The following is preferred
```
/**
* The count of widgets
Expand Down Expand Up @@ -137,7 +137,7 @@ To the greatest extent possible, please wrap lines to ensure that they do not ex
+ Indent case statements in switch
- Wrapping
+ Change all the `Never` values to `If Long`
+ Select the checkbox for Wrap After Assignement Operators
+ Select the checkbox for Wrap After Assignment Operators
* IntelliJ, by and large the IDE defaults are acceptable with the following changes:
- Wrapping and Braces
+ Change `Do not wrap` to `Wrap if long`
Expand Down
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buildPlugin()
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ This is a simple disk usage plugin that calculates disk usage while limiting the
To use this plugin visit the `Manage Jenkins` -> `Disk usage` page.

# Resources
* Continuous Integration: [![Build Status](https://jenkins.ci.cloudbees.com/buildStatus/icon?job=plugins/cloudbees-disk-usage-simple-plugin)](https://jenkins.ci.cloudbees.com/job/plugins/job/cloudbees-disk-usage-simple-plugin)
* Issues Tracking: [Jira](https://issues.jenkins-ci.org/browse/JENKINS/component/20652)
* Continuous Integration: [![Build Status](https://ci.jenkins.io/job/Plugins/job/cloudbees-disk-usage-simple-plugin/job/master/badge/icon)](https://ci.jenkins.io/job/Plugins/job/cloudbees-disk-usage-simple-plugin/job/master/)
* Issues Tracking: [Jira](https://issues.jenkins-ci.org/issues/?jql=project+%3D+JENKINS+AND+component+%3D+cloudbees-disk-usage-simple-plugin)
54 changes: 7 additions & 47 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.22</version>
<version>3.50</version>
</parent>

<artifactId>cloudbees-disk-usage-simple</artifactId>
Expand All @@ -39,17 +39,18 @@

<name>CloudBees Disk Usage Simple Plugin</name>
<description>This will use du to estimate job disk usage in an efficient way that doesn't slow down the host system</description>
<url>https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Simple+Disk+Usage+Plugin</url>
<url>https://wiki.jenkins.io/display/JENKINS/CloudBees+Simple+Disk+Usage+Plugin</url>

<properties>
<findbugs-maven-plugin.version>3.0.1</findbugs-maven-plugin.version>
<jenkins.version>2.60.3</jenkins.version>
<java.level>8</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
</properties>

<licenses>
<license>
<name>The MIT license</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand Down Expand Up @@ -87,58 +88,17 @@
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs-maven-plugin.version}</version>
<configuration>
<xmlOutput>true</xmlOutput>
<failOnError>${findbugs.failOnError}</failOnError>
</configuration>
<executions>
<execution>
<id>run-findbugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/cloudbees/simplediskusage/DiskItem.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, CloudBees, Inc.
Expand Down Expand Up @@ -33,11 +33,9 @@
*/
public class DiskItem implements Comparable<DiskItem> {

final String displayName;

final File path;

final Long usage;
private final String displayName;
private final File path;
private final Long usage;

public DiskItem(String displayName, File path, Long usage) {
this.displayName = displayName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, CloudBees, Inc.
Expand Down Expand Up @@ -29,8 +29,9 @@
* A job directory path on the disk with its usage information
*/
public class JobDiskItem extends DiskItem {
final String fullName;
final String url;

private final String fullName;
private final String url;

public JobDiskItem(Job<?, ?> job, Long size) {
super(job.getFullDisplayName(), job.getRootDir(), size);
Expand All @@ -45,5 +46,4 @@ public String getFullName() {
public String getUrl() {
return url;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, CloudBees, Inc.
Expand Down Expand Up @@ -37,14 +37,9 @@ public class QuickDiskUsageInitializer {
*/
@Initializer(after = InitMilestone.JOB_LOADED)
public static void initialize() {
// TODO switch to Jenkins.getActiveInstance() once 1.590+ is the baseline
Jenkins jenkins = Jenkins.getInstance();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to be compatible with 2.60+ (it's changing too often :-) )

if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
QuickDiskUsagePlugin plugin = jenkins.getPlugin(QuickDiskUsagePlugin.class);
if (plugin == null) return;
plugin.refreshDataOnStartup();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, CloudBees, Inc.
Expand Down Expand Up @@ -55,13 +55,8 @@ public String getUrlName() {
*/
@Override
public Object getTarget() {
// TODO switch to Jenkins.getActiveInstance() once 1.590+ is the baseline
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
jenkins.checkPermission(Jenkins.ADMINISTER);
return jenkins.getPlugin(QuickDiskUsagePlugin.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2015, CloudBees, Inc.
Expand Down Expand Up @@ -28,11 +28,10 @@
import hudson.model.Job;
import hudson.model.TopLevelItem;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.util.NamingThreadFactory;
import jenkins.model.Jenkins;
import jenkins.util.Timer;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
Expand All @@ -59,24 +58,22 @@ public class QuickDiskUsagePlugin extends Plugin {

public static final int QUIET_PERIOD = 15 * 60 * 1000;

private static Executor singleExecutorService = Executors.newSingleThreadExecutor(
private static final Executor singleExecutorService = Executors.newSingleThreadExecutor(
new NamingThreadFactory(Executors.defaultThreadFactory(),"Simple disk usage computation"));

private static final Logger logger = Logger.getLogger(QuickDiskUsagePlugin.class.getName());

private CopyOnWriteArrayList<DiskItem> directoriesUsages = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<DiskItem> directoriesUsages = new CopyOnWriteArrayList<>();

private CopyOnWriteArrayList<JobDiskItem> jobsUsages = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<JobDiskItem> jobsUsages = new CopyOnWriteArrayList<>();

private long lastRunStart = 0;

private long lastRunEnd = 0;

protected AtomicInteger progress = new AtomicInteger();
private final AtomicInteger progress = new AtomicInteger();

protected AtomicInteger total = new AtomicInteger();


private final AtomicInteger total = new AtomicInteger();

@Override
public void start() throws Exception {
Expand Down Expand Up @@ -143,11 +140,7 @@ public void doRefresh(StaplerRequest req, StaplerResponse res) throws IOExceptio

@RequirePOST
public void doClean(StaplerRequest req, StaplerResponse res) throws IOException, ServletException {
// TODO switch to Jenkins.getActiveInstance() once 1.590+ is the baseline
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
final Job job = jenkins.getItemByFullName(req.getParameter("job"), Job.class);
Timer.get().submit(new Runnable() {
@Override
Expand All @@ -172,9 +165,7 @@ class JobUsageListener implements UsageComputation.CompletionListener {
@Override
public void onCompleted(Path dir, long usage) {
JobDiskItem jobDiskItem = new JobDiskItem(job, usage / 1024);
if (jobsUsages.contains(jobDiskItem)) {
jobsUsages.remove(jobDiskItem);
}
jobsUsages.remove(jobDiskItem);
jobsUsages.add(jobDiskItem);
progress.incrementAndGet();
}
Expand All @@ -190,19 +181,14 @@ class DirectoryUsageListener implements UsageComputation.CompletionListener {
@Override
public void onCompleted(Path dir, long usage) {
DiskItem diskItem = new DiskItem(displayName, dir.toFile(), usage / 1024);
if (directoriesUsages.contains(diskItem)) {
directoriesUsages.remove(diskItem);
}
directoriesUsages.remove(diskItem);
directoriesUsages.add(diskItem);
progress.incrementAndGet();
}
}

private void registerJobs(UsageComputation uc) throws IOException, InterruptedException {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}

// Remove useless entries for jobs
for (JobDiskItem item : jobsUsages) {
Expand All @@ -221,9 +207,6 @@ private void registerJobs(UsageComputation uc) throws IOException, InterruptedEx

private void registerDirectories(UsageComputation uc) throws IOException, InterruptedException {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
Map<File, String> directoriesToProcess = new HashMap<>();
// Display JENKINS_HOME size
directoriesToProcess.put(jenkins.getRootDir(), "JENKINS_HOME");
Expand Down Expand Up @@ -252,7 +235,6 @@ private void registerDirectories(UsageComputation uc) throws IOException, Interr
}
}


public int getItemsCount() {
return total.intValue();
}
Expand All @@ -267,13 +249,8 @@ public void run() {
logger.info("Re-estimating disk usage");
progress.set(0);
lastRunStart = System.currentTimeMillis();
SecurityContext impersonate = ACL.impersonate(ACL.SYSTEM);
// TODO switch to Jenkins.getActiveInstance() once 1.590+ is the baseline
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
try {
try (ACLContext old = ACL.as(ACL.SYSTEM)) {
UsageComputation uc = new UsageComputation(Arrays.asList(Paths.get(System.getProperty("java.io.tmpdir")), jenkins.getRootDir().toPath()));
registerJobs(uc);
registerDirectories(uc);
Expand All @@ -284,8 +261,6 @@ public void run() {
} catch (IOException | InterruptedException e) {
logger.log(Level.INFO, "Unable to run disk usage check", e);
lastRunEnd = lastRunStart;
} finally {
SecurityContextHolder.setContext(impersonate);
}
try {
// Save data
Expand All @@ -298,11 +273,7 @@ public void run() {

private transient final Runnable computeDiskUsageOnStartup = new Runnable() {
public void run() {
// TODO switch to Jenkins.getActiveInstance() once 1.590+ is the baseline
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
while (jenkins.getInitLevel() != InitMilestone.COMPLETED) {
try {
logger.log(Level.INFO, "Waiting for Jenkins to be up before computing disk usage");
Expand Down
Loading