1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package com.c2kernel.process;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.InvalidTransitionException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.agent.Job;
import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.entity.proxy.EntityProxyObserver;
import com.c2kernel.entity.proxy.MemberControl;
import com.c2kernel.lifecycle.instance.stateMachine.Transitions;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.utils.Logger;
/**************************************************************************
*
* $Revision: 1.31 $
* $Date: 2004/10/21 08:02:19 $
*
* Copyright (C) 2003 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
public class UserCodeProcess extends StandardClient implements EntityProxyObserver, Runnable {
protected AgentProxy agent;
static boolean active = true;
ArrayList ignoredPaths = new ArrayList();
HashMap jobs;
public UserCodeProcess(String agentName, String agentPass) {
// login - try for a while in case server hasn't imported our user yet
for (int i=1;i<6;i++) {
try {
Logger.msg("Login attempt "+i+" of 5");
agent = Gateway.connect(agentName, agentPass);
break;
} catch (InvalidDataException ex) {
Logger.error("Could not log in.");
Logger.error(ex);
try {
Thread.sleep(5000);
} catch (InterruptedException ex2) { }
}
}
System.out.println(getDesc()+" initialised for " + agentName);
}
public void run() {
Thread.currentThread().setName("Usercode Process");
jobs = new HashMap();
// subscribe to job list
agent.subscribe(this, ClusterStorage.JOB, true);
while (active) {
Job thisJob = null;
synchronized (jobs) {
if (jobs.size() > 0) {
thisJob = getJob(jobs, Transitions.COMPLETE);
if (thisJob == null)
thisJob = getJob(jobs, Transitions.START);
if (thisJob == null)
thisJob = getJob(jobs, Transitions.SUSPEND);
if (thisJob == null)
thisJob = getJob(jobs, Transitions.RESUME);
if (thisJob == null) {
Logger.error("No supported jobs, but joblist is not empty! Discarding remaining jobs");
jobs.clear();
}
else
jobs.remove(ClusterStorage.getPath(thisJob));
}
}
if (thisJob != null) {
String jobKey = thisJob.getItemSysKey()+":"+thisJob.getStepPath();
try {
if (thisJob.getPossibleTransition()==Transitions.START) {
Logger.msg(5, "Testing start conditions");
boolean start = assessStartConditions(thisJob);
if (start) {
Logger.msg(5, "Attempting to start");
agent.execute(thisJob);
}
else {
Logger.msg(5, "Start conditions failed "+thisJob.getStepName()+" in "+thisJob.getItemSysKey());
}
}
else if (thisJob.getPossibleTransition()==Transitions.COMPLETE) {
Logger.msg(5, "Executing logic");
runUCLogic(thisJob);
if (ignoredPaths.contains(jobKey))
ignoredPaths.remove(jobKey);
}
else if (thisJob.getPossibleTransition()==Transitions.SUSPEND) {
if (ignoredPaths.contains(jobKey))
agent.execute(thisJob);
}
else if (thisJob.getPossibleTransition()==Transitions.RESUME) {
if (!ignoredPaths.contains(jobKey))
agent.execute(thisJob);
}
} catch (InvalidTransitionException ex) {
// must have already been done by someone else - ignore
} catch (Throwable ex) {
Logger.error("Error executing "+Transitions.getTransitionName(thisJob.getPossibleTransition())+" job:");
Logger.error(ex);
ignoredPaths.add(jobKey);
}
}
try {
synchronized (jobs) {
if (jobs.size() == 0) {
Logger.msg("Sleeping");
while (active && jobs.size() == 0)
jobs.wait(2000);
}
}
} catch (InterruptedException ex) { }
}
// shut down
try
{
standardTearDown();
}
catch( Exception ex )
{
Logger.error(ex);
}
}
private Job getJob(HashMap jobs, int transition) {
for (Iterator list = jobs.values().iterator();list.hasNext();) {
Job thisJob = (Job)list.next();
if (thisJob.getPossibleTransition() == transition) {
Logger.msg(1,"=================================================================");
Logger.msg(1, "Got "+Transitions.getTransitionName(transition)+" job for "+thisJob.getStepName()+" in "+thisJob.getItemSysKey());
return thisJob;
}
}
return null;
}
public boolean assessStartConditions(Job job) {
// default implementation - has no start conditions.
return true;
}
public void runUCLogic(Job job) throws Exception {
// default implementation - the agent will execute any scripts defined when we execute
agent.execute(job);
}
/**
* Receives job from the AgentProxy. Reactivates thread if sleeping.
*/
public void add(C2KLocalObject contents) {
if (contents instanceof MemberControl) {
MemberControl ctrl = (MemberControl)contents;
Logger.msg(5,ctrl.toString());
}
else
synchronized(jobs) {
Logger.msg(7, "Adding "+ClusterStorage.getPath(contents));
jobs.put(ClusterStorage.getPath(contents), contents);
jobs.notify();
}
}
/**
* Removes job removal notification from the AgentProxy.
*/
public void remove(String id) {
synchronized(jobs) {
Logger.msg(7, "Deleting "+id);
jobs.remove(id);
}
}
public static UserCodeProcess getInstance() throws UnknownHostException {
return new UserCodeProcess(InetAddress.getLocalHost().getHostName(), "uc");
}
static public void main(String[] args)
{
int status = 0;
try
{
standardSetUp(args);
UserCodeProcess proc = getInstance();
new Thread(proc).start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
shutdown();
}
}));
}
catch( Exception ex )
{
Logger.error(ex);
try
{
standardTearDown();
}
catch(Exception ex1)
{
Logger.error(ex1);
}
status = 1;
System.exit(status);
}
}
public String getDesc() {
return("Usercode Process");
}
public static void shutdown() {
active = false;
}
}
|