1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
18 | return new (P || (P = Promise))(function (resolve, reject) {
|
19 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
22 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23 | });
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const automation_client_1 = require("@atomist/automation-client");
|
27 | const retry_1 = require("@atomist/automation-client/util/retry");
|
28 | const axios_1 = require("axios");
|
29 | const _ = require("lodash");
|
30 | const os = require("os");
|
31 | function* timestampGenerator() {
|
32 | while (true) {
|
33 | yield new Date();
|
34 | }
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 | class RolarProgressLog {
|
40 | constructor(rolarBaseUrl, logPath, bufferSizeLimit = 1000, timerInterval = 0, logLevel = "info", timestamper = timestampGenerator(), retryOptions = {}, axiosInstance = axios_1.default) {
|
41 | this.rolarBaseUrl = rolarBaseUrl;
|
42 | this.logPath = logPath;
|
43 | this.bufferSizeLimit = bufferSizeLimit;
|
44 | this.timerInterval = timerInterval;
|
45 | this.logLevel = logLevel;
|
46 | this.timestamper = timestamper;
|
47 | this.retryOptions = retryOptions;
|
48 | this.axiosInstance = axiosInstance;
|
49 | this.localLogs = [];
|
50 | if (this.timerInterval > 0) {
|
51 | this.timer = setInterval(() => this.flush(), 2000);
|
52 | }
|
53 | }
|
54 | get name() {
|
55 | return this.logPath.join("/");
|
56 | }
|
57 | get url() {
|
58 | return `${this.rolarBaseUrl}/logs/${this.name}`;
|
59 | }
|
60 | isAvailable() {
|
61 | return __awaiter(this, void 0, void 0, function* () {
|
62 | const url = `${this.rolarBaseUrl}/api/logs`;
|
63 | try {
|
64 | yield retry_1.doWithRetry(() => this.axiosInstance.head(url), `check if Rolar service is available`, this.retryOptions);
|
65 | return true;
|
66 | }
|
67 | catch (e) {
|
68 | automation_client_1.logger.warn(`Rolar logger is NOT available at ${url}: ${e}`);
|
69 | return false;
|
70 | }
|
71 | });
|
72 | }
|
73 | write(what) {
|
74 | const line = what || "";
|
75 | const now = this.timestamper.next().value;
|
76 | this.localLogs.push({
|
77 | level: this.logLevel,
|
78 | message: line,
|
79 | timestamp: this.constructUtcTimestamp(now),
|
80 | timestampMillis: this.constructMillisTimestamp(now),
|
81 | });
|
82 | const bufferSize = this.localLogs.reduce((acc, logData) => acc + logData.message.length, 0);
|
83 | if (bufferSize > this.bufferSizeLimit) {
|
84 |
|
85 | this.flush();
|
86 | }
|
87 | }
|
88 | flush() {
|
89 | return this.postLogs(false);
|
90 | }
|
91 | close() {
|
92 | if (this.timer) {
|
93 | clearInterval(this.timer);
|
94 | }
|
95 | return this.postLogs(true);
|
96 | }
|
97 | postLogs(isClosed) {
|
98 | return __awaiter(this, void 0, void 0, function* () {
|
99 | const postingLogs = this.localLogs;
|
100 | this.localLogs = [];
|
101 | if (postingLogs && postingLogs.length > 0) {
|
102 | const closedRequestParam = isClosed ? "?closed=true" : "";
|
103 | const url = `${this.rolarBaseUrl}/api/logs/${this.logPath.join("/")}${closedRequestParam}`;
|
104 | const result = yield retry_1.doWithRetry(() => this.axiosInstance.post(url, {
|
105 | host: os.hostname(),
|
106 | content: postingLogs,
|
107 | }, {
|
108 | headers: { "Content-Type": "application/json" },
|
109 | }).catch(axiosError => Promise.reject(new Error(`Failure post to ${url}: ${axiosError.message}`))), `post log to Rolar`, this.retryOptions).catch(e => {
|
110 | this.localLogs = postingLogs.concat(this.localLogs);
|
111 | automation_client_1.logger.error(e);
|
112 | });
|
113 | return result;
|
114 | }
|
115 | return Promise.resolve();
|
116 | });
|
117 | }
|
118 | constructUtcTimestamp(d) {
|
119 | const now = d;
|
120 | const date = [now.getUTCMonth() + 1, now.getUTCDate(), now.getUTCFullYear()]
|
121 | .map(t => _.padStart(t.toString(), 2, "0"));
|
122 | const time = [now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()]
|
123 | .map(t => _.padStart(t.toString(), 2, "0"));
|
124 | return `${date.join("/")} ${time.join(":")}.${_.padStart(now.getUTCMilliseconds().toString(), 3, "0")}`;
|
125 | }
|
126 | constructMillisTimestamp(d) {
|
127 | return d.valueOf();
|
128 | }
|
129 | }
|
130 | exports.RolarProgressLog = RolarProgressLog;
|
131 |
|
\ | No newline at end of file |