-
Notifications
You must be signed in to change notification settings - Fork 0
/
executeWorkitem.js
148 lines (139 loc) · 5.46 KB
/
executeWorkitem.js
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
const async = require('async');
const Authenticator = require('./lib/authenticator');
const fs = require('fs');
const handlebar = require('handlebars');
const logger = require('./lib/logger');
const path = require('path');
const configFile = require('./config');
const storageUtils = require('./lib/storageUtils');
const workItemRunnerAbstract = require('./lib/workItemRunner');
const postWorkItemTemplateFileContent = fs.readFileSync('./templates/payloads/postWorkitemExportToFBX.hbs', 'utf8');
const postWorkItemTemplate = handlebar.compile(postWorkItemTemplateFileContent);
const OSSObjectNameInputPrefix = 'input-';
const OSSObjectNameOutputPrefix = 'output-';
class ExportToFBXRunner extends workItemRunnerAbstract {
/**
* Constructor
* @param forgeOAuth2TwoLegged oauth2TwoLegged object from forgeSDK
* @param config {Object}
* @param config.urls.designAutomation {String} url to design automation
*/
constructor(forgeOAuth2TwoLegged, config) {
super(forgeOAuth2TwoLegged, config);
this.nickname = config.forge.nickname;
this.activityId = config.designAutomation.activityId;
this.activityAlias = config.designAutomation.activityAlias;
}
/**
*
* @param jobId {String} job run unique identifier
* @private
*/
_getWorkItemPayload(jobId, nickname, callback) {
const self = this;
let inputUrl;
async.waterfall([
function getReadInputSignedUrl(next) {
storageUtils.getSignedUrl(self.forgeOAuth2TwoLegged, OSSObjectNameInputPrefix + jobId, 'read', next);
},
function getWriteOutputSignedUrl(url, next) {
inputUrl = url;
storageUtils.getSignedUrl(self.forgeOAuth2TwoLegged, OSSObjectNameOutputPrefix + jobId, 'write', next);
},
function resolveTemplate(url, next)
{
const resolvedTemplate = postWorkItemTemplate({
activityId: nickname + '.' + self.activityId + '+' + self.activityAlias,
inputUrl: inputUrl,
outputUrl: url
});
next(null, resolvedTemplate);
}
],
callback
);
}
/**
*
* @param jobId {String} job run unique identifier
* @param callback {Function} callback(error)
* @private
*/
_initializeStorage(jobId, callback) {
const self = this;
logger.log('Initalizing storage...');
logger.log('Creating OSS bucket if it does not exist...');
storageUtils.createBucketIfDoesNotExist(
self.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);
}
);
}
/**
*
* @param jobId {String} job run unique identifier
* @private
*/
_queueUploads(jobId, callback) {
const self = this;
async.waterfall([
function generateInputUploadUrl(next) {
storageUtils.getSignedUrl(self.forgeOAuth2TwoLegged, OSSObjectNameInputPrefix + jobId, 'write', next);
},
function addItemToUploadQueue(uploadUrl, next) {
self.uploadQueue.push(
[
{
"url": uploadUrl,
"filePath": process.argv[2]
}
]
);
next();
}
], callback);
}
/**
*
* @param jobId {String} job run unique identifier
* @param callback {Function} callback(error)
* @private
*/
_queueDownloads(jobId, callback) {
const self = this;
async.waterfall([
function generateOutputDownloadUrl(next) {
storageUtils.getSignedUrl(self.forgeOAuth2TwoLegged, OSSObjectNameOutputPrefix + jobId, 'read', next);
},
function addItemToUploadQueue(uploadUrl, next) {
self.downloadQueue.push(
[
{
"url": uploadUrl,
"destination": path.join(__dirname, 'Results', jobId + '.fbx' ).split('\\').join('/')
}
]
);
next();
}
], callback);
}
}
const authenticator = new Authenticator(configFile.forge.clientId, configFile.forge.clientSecret);
authenticator.getForgeOAuth2TwoLeggedObject((err, forgeOAuth2TwoLegged) => {
if (err) {
logger.log('Error while initializing forgeOAuth2TwoLegged object: ', err);
process.exit(1);
}
const ExportToFBXRunnerInstance = new ExportToFBXRunner(forgeOAuth2TwoLegged, configFile);
ExportToFBXRunnerInstance.run();
});