1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.job.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Objects; |
25 |
|
import java.util.concurrent.BlockingQueue; |
26 |
|
import java.util.concurrent.LinkedBlockingQueue; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.component.phase.Initializable; |
37 |
|
import org.xwiki.component.phase.InitializationException; |
38 |
|
import org.xwiki.context.Execution; |
39 |
|
import org.xwiki.context.ExecutionContext; |
40 |
|
import org.xwiki.context.ExecutionContextException; |
41 |
|
import org.xwiki.context.ExecutionContextManager; |
42 |
|
import org.xwiki.job.Job; |
43 |
|
import org.xwiki.job.JobException; |
44 |
|
import org.xwiki.job.JobManager; |
45 |
|
import org.xwiki.job.JobStatusStore; |
46 |
|
import org.xwiki.job.Request; |
47 |
|
import org.xwiki.job.event.status.JobStatus; |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
@deprecated@link |
55 |
|
|
56 |
|
@Component |
57 |
|
@Singleton |
58 |
|
@Deprecated |
|
|
| 35.2% |
Uncovered Elements: 35 (54) |
Complexity: 19 |
Complexity Density: 0.51 |
|
59 |
|
public class DefaultJobManager implements JobManager, Runnable, Initializable |
60 |
|
{ |
61 |
|
|
62 |
|
@link |
63 |
|
|
64 |
|
@Inject |
65 |
|
@Named("context") |
66 |
|
private Provider<ComponentManager> componentManager; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@Inject |
72 |
|
private JobStatusStore store; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private Execution execution; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
private ExecutionContextManager executionContextManager; |
85 |
|
|
86 |
|
|
87 |
|
@see |
88 |
|
|
89 |
|
private volatile Job currentJob; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
private BlockingQueue<Job> jobQueue = new LinkedBlockingQueue<Job>(); |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
private Thread thread; |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
1 |
@Override... |
102 |
|
public void initialize() throws InitializationException |
103 |
|
{ |
104 |
1 |
initialize("Job Manager daemon thread"); |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
107 |
1 |
protected void initialize(String threadName) throws InitializationException... |
108 |
|
{ |
109 |
1 |
this.thread = new Thread(this); |
110 |
1 |
this.thread.setDaemon(true); |
111 |
1 |
this.thread.start(); |
112 |
1 |
this.thread.setName(threadName); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
117 |
1 |
@Override... |
118 |
|
public void run() |
119 |
|
{ |
120 |
1 |
while (!this.thread.isInterrupted()) { |
121 |
1 |
runJob(); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
|
|
| 25% |
Uncovered Elements: 6 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
|
128 |
1 |
public void runJob()... |
129 |
|
{ |
130 |
1 |
try { |
131 |
1 |
this.currentJob = this.jobQueue.take(); |
132 |
|
|
133 |
|
|
134 |
0 |
ExecutionContext context = new ExecutionContext(); |
135 |
|
|
136 |
0 |
try { |
137 |
0 |
this.executionContextManager.initialize(context); |
138 |
|
} catch (ExecutionContextException e) { |
139 |
0 |
throw new RuntimeException("Failed to initialize Job " + this.currentJob + " execution context", e); |
140 |
|
} |
141 |
|
|
142 |
0 |
this.currentJob.run(); |
143 |
|
} catch (InterruptedException e) { |
144 |
|
|
145 |
|
} finally { |
146 |
0 |
this.execution.removeContext(); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
0 |
@Override... |
153 |
|
public Job getCurrentJob() |
154 |
|
{ |
155 |
0 |
return this.currentJob; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@return |
161 |
|
@throws |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
163 |
0 |
private Job createJob(String jobType) throws JobException... |
164 |
|
{ |
165 |
0 |
Job job; |
166 |
0 |
try { |
167 |
0 |
job = this.componentManager.get().getInstance(Job.class, jobType); |
168 |
|
} catch (ComponentLookupException e) { |
169 |
0 |
throw new JobException("Failed to lookup any Job for role hint [" + jobType + "]", e); |
170 |
|
} |
171 |
|
|
172 |
0 |
return job; |
173 |
|
} |
174 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
175 |
0 |
@Override... |
176 |
|
public Job executeJob(String jobType, Request request) throws JobException |
177 |
|
{ |
178 |
0 |
Job job = addJob(jobType, request); |
179 |
|
|
180 |
0 |
try { |
181 |
0 |
job.join(); |
182 |
|
} catch (InterruptedException e) { |
183 |
|
|
184 |
|
} |
185 |
|
|
186 |
0 |
return job; |
187 |
|
} |
188 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
189 |
0 |
@Override... |
190 |
|
public Job addJob(String jobType, Request request) throws JobException |
191 |
|
{ |
192 |
0 |
Job job = createJob(jobType); |
193 |
|
|
194 |
0 |
job.initialize(request); |
195 |
|
|
196 |
0 |
this.jobQueue.offer(job); |
197 |
|
|
198 |
0 |
return job; |
199 |
|
} |
200 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
201 |
0 |
@Override... |
202 |
|
public void addJob(Job job) |
203 |
|
{ |
204 |
0 |
this.jobQueue.offer(job); |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0 |
@Override... |
208 |
|
public JobStatus getJobStatus(String id) |
209 |
|
{ |
210 |
0 |
return getJobStatus(Arrays.asList(id)); |
211 |
|
} |
212 |
|
|
|
|
| 40% |
Uncovered Elements: 6 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
213 |
1 |
@Override... |
214 |
|
public JobStatus getJobStatus(List<String> id) |
215 |
|
{ |
216 |
|
|
217 |
1 |
if (this.currentJob != null && Objects.equals(id, this.currentJob.getRequest().getId())) { |
218 |
0 |
return this.currentJob.getStatus(); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
1 |
for (Job job : this.jobQueue) { |
223 |
0 |
if (Objects.equals(id, job.getRequest().getId())) { |
224 |
0 |
return job.getStatus(); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
1 |
return this.store.getJobStatus(id); |
230 |
|
} |
231 |
|
} |