-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecuteWorkitem.js
More file actions
200 lines (189 loc) · 7.63 KB
/
executeWorkitem.js
File metadata and controls
200 lines (189 loc) · 7.63 KB
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
const async = require('async');
const AutodeskForgeDesignAutomation = require('autodesk.forge.designautomation');
const Authenticator = require('./lib/authenticator');
const configFile = require('./config');
const DownloadQueue = require('./lib/downloadQueue');
const fs = require('fs');
const handlebar = require('handlebars');
const logger = require('./lib/logger');
const storageUtils = require('./lib/storageUtils');
const UploadQueue = require('./lib/uploadQueue');
const uuidv4 = require('uuid/v4');
const workitemTemplateFileContent = fs.readFileSync('./templates/Workitem.hbs', 'utf8');
const workitemTemplate = handlebar.compile(workitemTemplateFileContent);
let config = {
"retry" : {
"maxNumberOfRetries" : 7,
"backoffDelay" : 4000,
"backoffPolicy" : "exponentialBackoffWithJitter"
}
};
let DesignAutomationClient = new AutodeskForgeDesignAutomation.AutodeskForgeDesignAutomationClient(config);
let DesignAutomationApi = new AutodeskForgeDesignAutomation.AutodeskForgeDesignAutomationApi(DesignAutomationClient);
const inputStoragePrefix = 'input-';
const outputStoragePrefix = 'output-';
function initializeStorage(jobId, forgeOAuth2TwoLegged, callback) {
logger.log('Creating OSS bucket if it does not exist...');
storageUtils.createBucketIfDoesNotExist(
forgeOAuth2TwoLegged,
(err, bucketDetails) => {
if (err && err.statusCode === 403) {
logger.log('Error creating the OSS bucket. This is most likely because the name you chose for the bucket is already used by someone else. Change ossBucketName in the config file and try again');
process.exit(1);
}
if (err && err.statusCode === 400) {
logger.log('Error creating the OSS bucket. This is most likely because the name you chose for the bucket contains illegal characters. Make sure ossBucketName in the config file is of that form [-_.a-z0-9]{3,128} ');
process.exit(1);
}
callback(err);
}
);
}
function waitForWorkItemToComplete(accessToken, workItemId, callback) {
const startWait = new Date();
let workitemStatus;
async.doWhilst(
function checkForCompletionStatus(next) {
setTimeout(() => {
DesignAutomationApi.getWorkitemStatus(workItemId).then((data) => {
workitemStatus = data;
next();
}, next);
}, configFile.timeBetweenPolls);
},
function checkWorkItemStatusComplete() {
logger.log('Checking status: ' + workitemStatus.status + ' ' + (Date.now() - startWait) + ' ms');
return workitemStatus.status === 'pending' || workitemStatus.status === 'inprogress';
},
() => {
if (workitemStatus.reportUrl) {
logger.log('Log file available here: ' + workitemStatus.reportUrl)
}
if (workitemStatus.status !== 'success') {
callback('Workitem finished with status: ' + workitemStatus.status);
return;
}
callback();
}
);
}
function executeWorkitem() {
let nickname;
let oAuth2TwoLegged;
const jobId = uuidv4();
let downloadQueue = new DownloadQueue();
let uploadQueue = new UploadQueue();
async.waterfall([
function getAccessToken(next)
{
logger.log('Getting Access Token...');
const authenticator = new Authenticator(configFile.forge.clientId, configFile.forge.clientSecret);
authenticator.getForgeOAuth2TwoLeggedObject((error, forgeOAuth2TwoLegged) => {
if (error)
{
next(error);
return;
}
let oauth = DesignAutomationClient.authManager.authentications['2-legged'];
oAuth2TwoLegged = forgeOAuth2TwoLegged;
oauth.accessToken = oAuth2TwoLegged.getCredentials().access_token;
next();
});
},
function getNickname(next) {
logger.log('Getting Nickname...');
DesignAutomationApi.getNickname("me").then((data) => {
nickname = data;
next();
}, next);
},
function initializeOSSStorage(next) {
logger.log('Initializing storage...');
initializeStorage(jobId, oAuth2TwoLegged, next);
},
function getInputUploadUrl(next) {
logger.log('Generating input upload url');
storageUtils.getSignedUrl(oAuth2TwoLegged, inputStoragePrefix + jobId, 'write', next);
},
function queueUploads(uploadUrl, next)
{
logger.log('Queueing upload...');
uploadQueue.queue(uploadUrl, process.argv[2]);
next();
},
function waitForUploadsToFinish(next)
{
logger.log('Waiting for uploads to finish...');
uploadQueue.waitForQueueToBeProcessed(next);
},
function getPresignedUrlsForWorkitem(next)
{
logger.log('Generating signed urls for workitem submission...');
async.parallel([
function getInputPresignedUrl(next)
{
storageUtils.getSignedUrl(oAuth2TwoLegged, inputStoragePrefix + jobId, 'read', next)
},
function getOutputPresignedUrl(next)
{
storageUtils.getSignedUrl(oAuth2TwoLegged, outputStoragePrefix + jobId, 'write', next);
}
], (error, results) => {
if (error)
{
next(error);
}
next(null, results[0], results[1]);
});
},
function createWorkitem(presignedInputUrl, presignedOutputUrl, next)
{
logger.log('Submitting workitem...');
const workitemObjectJson = workitemTemplate({
activityId: nickname + '.' + configFile.designAutomation.activityId + '+' + configFile.designAutomation.activityAlias,
inputUrl: presignedInputUrl,
outputUrl: presignedOutputUrl
});
const workitemObject = JSON.parse(workitemObjectJson);
DesignAutomationApi.createWorkItem(workitemObject).then((data) => {
next(null, data.id)
}, next);
},
function waitForWorkitemToComplete(workitemId, next)
{
logger.log('waiting for workitem ' + workitemId + ' to complete...');
waitForWorkItemToComplete(oAuth2TwoLegged.getCredentials().access_token, workitemId, next)
},
function getPresignedDownloadOutputUrl(next)
{
storageUtils.getSignedUrl(oAuth2TwoLegged, outputStoragePrefix + jobId, 'read', next);
},
function queueDownload(signedOutputUrl, next)
{
logger.log('Queueing downloads...');
downloadQueue.queue(signedOutputUrl, jobId);
next();
},
function waitForDownloadToFinish(next)
{
logger.log('Waiting for downloads to finish...');
downloadQueue.waitForQueueToBeProcessed(next);
}
], function (err) {
if (err) {
logger.log(err);
if (err && err.status)
{
logger.log('Status: ' + err.status);
}
if (err && err.response && err.response.text)
{
logger.log('Text: ' + err.response.text);
}
process.exit(-1);
}
logger.log('Finished executing workitem');
process.exit(0);
});
}
executeWorkitem();