-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasksClient.js
111 lines (93 loc) · 3.77 KB
/
tasksClient.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
const { CloudTasksClient } = require("@google-cloud/tasks");
const tracerApi = require("@opentelemetry/api");
module.exports = class TasksClient {
/**
* Construct variables for this cloud tasks client for use with Google Cloud Tasks.
* Undefined values may be replaced with defaults
*
* @param projectId Google projectId
* @param keyPath path to service account json
* @param context trace
*
* Constructs the actual Google cloud tasks client for internal use
*/
constructor(config) {
this.projectId = config.projectId;
this.keyPath = config.keyPath;
// TRACER CLIENT
this.context = config.context;
this.TasksClient = new CloudTasksClient({
projectId: this.projectId,
keyFilename: this.keyPath
});
// LOGGER CLIENT
if(config.hasOwnProperty('loggerInstance') && config.loggerInstance !== undefined){
this.loggerInstance = config.loggerInstance
this.logger = config.loggerInstance.createLogger({level: process.env.LOGLEVEL ? process.env.LOGLEVEL : 'trace'});
}
}
/**
* Create Task and send it to queue
*
* @param method Google projectId
* @param url path to service account json
* @param body hosted queue location
* @param queue hosted queue location
*
*/
async sendTask(config) {
// determin
let span;
if(config.spanName !== null){
span = this.context.startSpan(config.spanName, {
parent: tracerApi.getSpan(tracerApi.context.active()) ? tracerApi.getSpan(tracerApi.context.active()) : this.context.startSpan().updateName("sendTask")
});
if(this.logger){
let labelObject = {
function: `taskClient.sendTask()`,
spanId: span.spanContext.spanId,
spanTraceId: span.spanContext.traceId
};
let loggerKey = await this.loggerInstance.getLoggerKey(span.spanContext.spanId, {
labels: labelObject
});
this.logger.debug(loggerKey, `KICK CLOUD TASK: ${span.spanContext.spanId}`);
}
span.setAttributes(config)
// creat trace parent
config.traceparent = '00-' + span.spanContext.traceId + '-' + span.spanContext.spanId + '-0' + span.spanContext.traceFlags
}
// set headers on task
if(config.hasOwnProperty('headers')){
config.headers['Content-Type'] = 'application/json';
config.headers['traceparent'] = config.traceparent ? config.traceparent : null;
}else{
config.headers = {
'Content-Type': 'application/json',
'traceparent': config.traceparent ? config.traceparent : null,
}
}
// configuration for the task
const request = {
parent: this.TasksClient.queuePath(this.projectId, config.location, config.queue),
task: {
httpRequest: {
httpMethod: config.method,
url: config.url,
headers: config.headers,
},
scheduleTime: {
seconds: config.scheduleTime ? config.scheduleTime + (Math.round(new Date() / 1000)) : Math.round(new Date() / 1000),
}
}
};
if(config.method === 'POST' || config.method === 'PUT'){
request.task.httpRequest.body = Buffer.from(JSON.stringify(config.body)).toString('base64');
}
let createdTask = await this.TasksClient.createTask(request);
if(config.spanName !== null){
span.end()
}
return createdTask;
}
}