-
Notifications
You must be signed in to change notification settings - Fork 6
/
CommunityContributionMaintainer.ts
211 lines (194 loc) · 6.81 KB
/
CommunityContributionMaintainer.ts
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
import SingularityConnector from "../connectors/SingularityConnector";
import BaseMaintainer from "./BaseMaintainer";
import XSEDEUtil from "../lib/XSEDEUtil";
import { ResultFolderContentManager } from "../lib/JobUtil";
import { executableManifest, GitFolder } from "../types";
import GitUtil from "../lib/GitUtil";
import { Git } from "../models/Git";
import { Folder } from "../models/Folder";
import { FolderUploaderHelper } from "../FolderUploader";
class CommunityContributionMaintainer extends BaseMaintainer {
public connector: SingularityConnector;
public resultFolderContentManager: ResultFolderContentManager =
new ResultFolderContentManager();
public executableManifest: executableManifest;
onDefine() {
this.connector = this.getSingularityConnector();
}
/**
* On maintainer initialization, set executableManifest, and give it to the connector. Update the event log to reflect the job being initialized or encountering a system error.
*
* @async
*/
async onInit() {
try {
const connection = await this.db.connect();
// check if local executable file is git
const localExecutableFolder = this.job.localExecutableFolder;
if (localExecutableFolder.type != "git")
throw new Error(
"community contribution currently don't accept non-git code"
);
// get executable manifest
const git = await connection
.getRepository(Git)
.findOne((localExecutableFolder as GitFolder).gitId);
if (!git)
throw new Error("could not find git repo executable in this job");
this.executableManifest = await GitUtil.getExecutableManifest(git);
if (this.executableManifest.connector == "SingCVMFSConnector"){
this.connector = this.getSingCVMFSConnector();
}
// upload executable folder
if (!this.job.localExecutableFolder)
throw new Error("job.localExecutableFolder is required");
this.emitEvent("SLURM_UPLOAD_EXECUTABLE", `uploading executable folder`);
var uploader = await FolderUploaderHelper.upload(
this.job.localExecutableFolder,
this.job.hpc,
this.job.userId,
this.job.id,
this.connector
);
this.connector.setRemoteExecutableFolderPath(uploader.hpcPath);
this.job.remoteExecutableFolder = await connection
.getRepository(Folder)
.findOne(uploader.id);
// upload data folder
if (this.job.localDataFolder) {
this.emitEvent("SLURM_UPLOAD_DATA", `uploading data folder`);
uploader = await FolderUploaderHelper.upload(
this.job.localDataFolder,
this.job.hpc,
this.job.userId,
this.job.id,
this.connector
);
this.connector.setRemoteDataFolderPath(uploader.hpcPath);
this.job.remoteDataFolder = await connection
.getRepository(Folder)
.findOne(uploader.id);
} else if (this.job.remoteDataFolder) {
this.connector.setRemoteDataFolderPath(
this.job.remoteDataFolder.hpcPath
);
}
// create empty result folder
this.emitEvent("SLURM_CREATE_RESULT", `create result folder`);
uploader = await FolderUploaderHelper.upload(
{ type: "empty" },
this.job.hpc,
this.job.userId,
this.job.id,
this.connector
);
this.connector.setRemoteResultFolderPath(uploader.hpcPath);
this.job.remoteResultFolder = await connection
.getRepository(Folder)
.findOne(uploader.id);
// update job
await this.updateJob({
remoteDataFolder: this.job.remoteDataFolder,
remoteExecutableFolder: this.job.remoteExecutableFolder,
remoteResultFolder: this.job.remoteResultFolder,
});
// submit
await this.connector.execExecutableManifestWithinImage(
this.executableManifest,
this.slurm
);
await this.connector.submit();
this.jobOnHpc = true;
this.emitEvent(
"JOB_INIT",
"job [" + this.id + "] is initialized, waiting for job completion"
);
XSEDEUtil.jobLog(this.connector.slurm_id, this.hpc, this.job);
} catch (e) {
this.emitEvent(
"JOB_RETRY",
"job [" + this.id + "] encountered system error " + e.toString()
);
}
}
/**
* If the job is complete, download the results to the remote result file path, and if it encounters an error, update the event log to reflect this.
*
* @async
*/
async onMaintain() {
try {
var status = await this.connector.getStatus();
// failing condition
if (status == "ERROR" || status == "F" || status == "NF") {
this.emitEvent(
"JOB_FAILED",
"job [" + this.id + "] failed with status " + status
);
return;
}
// complete condition
if (status == "C" || status == "CD" || status == "UNKNOWN") {
// collect logs
await this.connector.getSlurmStdout();
await this.connector.getSlurmStderr();
// update job usage
this.emitEvent("JOB_ENDED", "job [" + this.id + "] finished");
const usage = await this.connector.getUsage();
this.updateJob(usage);
// submit again to XSEDE
XSEDEUtil.jobLog(this.connector.slurm_id, this.hpc, this.job); // for backup submit
// fetch result folder content
// TODO: make this shorter
var contents = await this.connector.getRemoteResultFolderContent();
var defaultResultFolderDownloadablePath =
this.executableManifest.default_result_folder_downloadable_path;
if (defaultResultFolderDownloadablePath) {
// bring default downloadable to front (for frontend display)
contents.sort((a, b) =>
a == defaultResultFolderDownloadablePath
? -1
: b == defaultResultFolderDownloadablePath
? 1
: 0
);
if (defaultResultFolderDownloadablePath[0] != "/") {
defaultResultFolderDownloadablePath = `/${defaultResultFolderDownloadablePath}`;
contents.sort((a, b) =>
a == defaultResultFolderDownloadablePath
? -1
: b == defaultResultFolderDownloadablePath
? 1
: 0
);
}
}
await this.resultFolderContentManager.put(this.id, contents);
}
} catch (e) {
this.emitEvent(
"JOB_RETRY",
"job [" + this.id + "] encountered system error " + e.toString()
);
}
}
/**
* Pause the connector
*/
async onPause() {
await this.connector.pause();
}
/**
* Resume the connector
*/
async onResume() {
await this.connector.resume();
}
/**
* Cancel the connector
*/
async onCancel() {
await this.connector.cancel();
}
}
export default CommunityContributionMaintainer;