File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\AsyncAperiodicWork.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Vojtech Juranek
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.security.ACL;
27 import hudson.util.StreamTaskListener;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.logging.Level;
32
33 import jenkins.model.Jenkins;
34 import org.acegisecurity.context.SecurityContext;
35 import org.acegisecurity.context.SecurityContextHolder;
36
37 /**
38 * {@link AperiodicWork} that takes a long time to run. Similar to {@link AsyncPeriodicWork}, see {@link AsyncPeriodicWork} for
39 * details and {@link AperiodicWork} for differences between {@link AperiodicWork} and {@link PeriodicWork}.
40 *
41 * @author vjuranek
42 * @since 1.410
43 */
44 public abstract class AsyncAperiodicWork extends AperiodicWork {
45
46 /**
47 * Name of the work.
48 */
49 public final String name;
50
51 private Thread thread;
52
53 protected AsyncAperiodicWork(String name) {
54 this.name = name;
55 }
56
57 /**
58 * Schedules this periodic work now in a new thread, if one isn't already running.
59 */
60 @Override
61 public final void doAperiodicRun() {
62 try {
63 if(thread!=null && thread.isAlive()) {
64 logger.log(Level.INFO, name+" thread is still running. Execution aborted.");
65 return;
66 }
67 thread = new Thread(new Runnable() {
68 public void run() {
69 logger.log(Level.INFO, "Started "+name);
70 long startTime = System.currentTimeMillis();
71
72 StreamTaskListener l = createListener();
73 try {
74 ACL.impersonate(ACL.SYSTEM);
75
76 execute(l);
77 } catch (IOException e) {
78 e.printStackTrace(l.fatalError(e.getMessage()));
79 } catch (InterruptedException e) {
80 e.printStackTrace(l.fatalError("aborted"));
81 } finally {
82 l.closeQuietly();
83 }
84
85 logger.log(Level.INFO, "Finished "+name+". "+
86 (System.currentTimeMillis()-startTime)+" ms");
87 }
88 },name+" thread");
89 thread.start();
90 } catch (Throwable t) {
91 logger.log(Level.SEVERE, name+" thread failed with error", t);
92 }
93 }
94
95 protected StreamTaskListener createListener() {
96 try {
97 return new StreamTaskListener(getLogFile());
98 } catch (IOException e) {
99 throw new Error(e);
100 }
101 }
102
103 /**
104 * Determines the log file that records the result of this task.
105 */
106 protected File getLogFile() {
107 return new File(Jenkins.getInstance().getRootDir(),name+".log");
108 }
109
110 /**
111 * Executes the task.
112 *
113 * @param listener
114 * Output sent will be reported to the users. (this work is TBD.)
115 * @throws InterruptedException
116 * The caller will record the exception and moves on.
117 * @throws IOException
118 * The caller will record the exception and moves on.
119 */
120 protected abstract void execute(TaskListener listener) throws IOException, InterruptedException;
121
122 }
123