@gitpod/gitpod-protocol 0.1.5-port-description-3059-fork.0 → 0.1.5-port-description-3059-fork.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/data/gitpod-schema.json +2 -3
  2. package/lib/gitpod-service.d.ts +2 -0
  3. package/lib/gitpod-service.d.ts.map +1 -1
  4. package/lib/gitpod-service.js.map +1 -1
  5. package/lib/messaging/node/connection.d.ts +0 -10
  6. package/lib/messaging/node/connection.d.ts.map +1 -1
  7. package/lib/messaging/node/connection.js +1 -39
  8. package/lib/messaging/node/connection.js.map +1 -1
  9. package/lib/protocol.d.ts +1 -1
  10. package/lib/protocol.d.ts.map +1 -1
  11. package/lib/util/garbage-collected-cache.d.ts +1 -0
  12. package/lib/util/garbage-collected-cache.d.ts.map +1 -1
  13. package/lib/util/garbage-collected-cache.js +5 -1
  14. package/lib/util/garbage-collected-cache.js.map +1 -1
  15. package/lib/util/jaeger-client-types.d.ts +68 -0
  16. package/lib/util/jaeger-client-types.d.ts.map +1 -0
  17. package/lib/util/jaeger-client-types.js +8 -0
  18. package/lib/util/jaeger-client-types.js.map +1 -0
  19. package/lib/util/repeat.d.ts +15 -0
  20. package/lib/util/repeat.d.ts.map +1 -0
  21. package/lib/util/repeat.js +55 -0
  22. package/lib/util/repeat.js.map +1 -0
  23. package/lib/util/tracing.d.ts +40 -8
  24. package/lib/util/tracing.d.ts.map +1 -1
  25. package/lib/util/tracing.js +103 -31
  26. package/lib/util/tracing.js.map +1 -1
  27. package/lib/util/tracing.spec.d.ts +7 -0
  28. package/lib/util/tracing.spec.d.ts.map +1 -0
  29. package/lib/util/tracing.spec.js +121 -0
  30. package/lib/util/tracing.spec.js.map +1 -0
  31. package/package.json +2 -1
  32. package/pkg-yarn.lock +1 -1
  33. package/src/gitpod-service.ts +2 -0
  34. package/src/messaging/node/connection.ts +1 -45
  35. package/src/protocol.ts +1 -1
  36. package/src/util/garbage-collected-cache.ts +7 -1
  37. package/src/util/jaeger-client-types.ts +102 -0
  38. package/src/util/repeat.ts +45 -0
  39. package/src/util/tracing.spec.ts +83 -0
  40. package/src/util/tracing.ts +102 -28
  41. package/lib/util/repeater.d.ts +0 -22
  42. package/lib/util/repeater.d.ts.map +0 -1
  43. package/lib/util/repeater.js +0 -65
  44. package/lib/util/repeater.js.map +0 -1
  45. package/src/util/jaeger-client.d.ts +0 -105
  46. package/src/util/repeater.ts +0 -49
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Copyright (c) 2021 Gitpod GmbH. All rights reserved.
3
+ * Licensed under the GNU Affero General Public License (AGPL).
4
+ * See License-AGPL.txt in the project root for license information.
5
+ */
6
+
7
+ import { suite, test } from "mocha-typescript"
8
+ import * as chai from "chai"
9
+ import { TraceContext } from "./tracing";
10
+ import { MockTracer } from "opentracing";
11
+
12
+ const expect = chai.expect
13
+
14
+ @suite class TestTracing {
15
+
16
+ @test public async testTracingContext_addNestedTags() {
17
+ const tracer = new MockTracer();
18
+ const span = tracer.startSpan('testTracingContext_addNestedTags');
19
+ TraceContext.addNestedTags({ span }, {
20
+ rpc: {
21
+ system: "jsonrpc",
22
+ jsonrpc: {
23
+ version: "1.0",
24
+ method: "test",
25
+ parameters: ["abc", "def"],
26
+ },
27
+ },
28
+ });
29
+
30
+ const mockSpan = tracer.report().spans[0];
31
+ expect(mockSpan.tags()).to.deep.equal({
32
+ "rpc.system": "jsonrpc",
33
+ "rpc.jsonrpc.version": "1.0",
34
+ "rpc.jsonrpc.method": "test",
35
+ "rpc.jsonrpc.parameters.0": "abc",
36
+ "rpc.jsonrpc.parameters.1": "def",
37
+ });
38
+ }
39
+
40
+ @test public async testTracingContext_addNestedTags_null() {
41
+ const tracer = new MockTracer();
42
+ const span = tracer.startSpan('testTracingContext_addNestedTags_null');
43
+ TraceContext.addNestedTags({ span }, {
44
+ someShape: {
45
+ thisIsNull: null,
46
+ thisIsUndefined: undefined,
47
+ },
48
+ });
49
+
50
+ const mockSpan = tracer.report().spans[0];
51
+ expect(mockSpan.tags()).to.deep.equal({
52
+ "someShape.thisIsNull": null,
53
+ "someShape.thisIsUndefined": undefined,
54
+ });
55
+ }
56
+
57
+ @test public async testTracingContext_addJsonRPCParameters() {
58
+ const tracer = new MockTracer();
59
+ const span = tracer.startSpan('testTracingContext_addJsonRPCParameters');
60
+ const ctx = { span };
61
+ TraceContext.addJsonRPCParameters(ctx, {
62
+ one: "one",
63
+ two: {
64
+ name: "two",
65
+ some: "shape",
66
+ containing: "PII",
67
+ },
68
+ three: "three",
69
+ });
70
+
71
+ const mockSpan = tracer.report().spans[0];
72
+ expect(mockSpan.tags()).to.deep.equal({
73
+ "rpc.jsonrpc.parameters.one": "one",
74
+ "rpc.jsonrpc.parameters.two.containing": "PII",
75
+ "rpc.jsonrpc.parameters.two.name": "two",
76
+ "rpc.jsonrpc.parameters.two.some": "shape",
77
+ "rpc.jsonrpc.parameters.three": "three",
78
+ "rpc.system": "jsonrpc",
79
+ });
80
+ }
81
+
82
+ }
83
+ module.exports = new TestTracing()
@@ -6,10 +6,12 @@
6
6
 
7
7
 
8
8
  import * as opentracing from 'opentracing';
9
- import { TracingConfig, initTracerFromEnv, Sampler, SamplingDecision } from 'jaeger-client';
9
+ import { TracingConfig, initTracerFromEnv } from 'jaeger-client';
10
+ import { Sampler, SamplingDecision } from './jaeger-client-types';
10
11
  import { initGlobalTracer } from 'opentracing';
11
12
  import { injectable } from 'inversify';
12
13
  import { ResponseError } from 'vscode-jsonrpc';
14
+ import { log, LogContext } from './logging';
13
15
 
14
16
  export interface TraceContext {
15
17
  span?: opentracing.Span
@@ -20,49 +22,62 @@ export type TraceContextWithSpan = TraceContext & {
20
22
 
21
23
 
22
24
  export namespace TraceContext {
23
- export function startSpan(operation: string, parentCtx: TraceContext): opentracing.Span {
24
- const options: opentracing.SpanOptions = {
25
- childOf: parentCtx.span
25
+ export function startSpan(operation: string, parentCtx?: TraceContext): opentracing.Span {
26
+ let options: opentracing.SpanOptions | undefined = undefined;
27
+ if (parentCtx) {
28
+ options = {
29
+ childOf: parentCtx.span
30
+ };
26
31
  }
27
32
  return opentracing.globalTracer().startSpan(operation, options);
28
33
  }
29
34
 
30
- export function childContextWithSpan(operation: string, parentCtx: TraceContext): TraceContextWithSpan {
35
+ export function childContext(operation: string, parentCtx: TraceContext): TraceContextWithSpan {
31
36
  const span = startSpan(operation, parentCtx);
32
37
  return { span };
33
38
  }
34
39
 
35
- export function startAsyncSpan(operation: string, ctx: TraceContext): opentracing.Span {
36
- const options: opentracing.SpanOptions = {};
37
- if (!!ctx.span) {
38
- options.references = [opentracing.followsFrom(ctx.span.context())];
40
+ export function setError(ctx: TraceContext, err: Error) {
41
+ if (!ctx.span) {
42
+ return;
39
43
  }
40
- return opentracing.globalTracer().startSpan(operation, options);
44
+
45
+ TraceContext.addNestedTags(ctx, {
46
+ error: {
47
+ message: err.message,
48
+ stacktrace: err.stack,
49
+ },
50
+ });
51
+ ctx.span.setTag("error", true);
41
52
  }
42
53
 
43
- export function logError(ctx: TraceContext, err: Error) {
54
+ export function setJsonRPCMetadata(ctx: TraceContext, method?: string) {
44
55
  if (!ctx.span) {
45
56
  return;
46
57
  }
47
58
 
48
- ctx.span.log({
49
- "error": err.message,
50
- "stacktrace": err.stack
51
- })
52
- ctx.span.setTag("error", true);
59
+ const tags: { [key: string]: any } = {
60
+ rpc: {
61
+ system: "jsonrpc",
62
+ // version,
63
+ },
64
+ };
65
+ if (method) {
66
+ tags.rpc.method = method;
67
+ }
68
+ addNestedTags(ctx, tags);
53
69
  }
54
70
 
55
- export function logJsonRPCError(ctx: TraceContext, method: string, err: ResponseError<any>) {
71
+ export function setJsonRPCError(ctx: TraceContext, method: string, err: ResponseError<any>) {
56
72
  if (!ctx.span) {
57
73
  return;
58
74
  }
59
- logError(ctx, err);
75
+ // not use setError bc this is (most likely) a working operation
60
76
 
77
+ setJsonRPCMetadata(ctx);
61
78
  // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/rpc.md#json-rpc
62
- ctx.span.addTags({
79
+ addNestedTags(ctx, {
63
80
  rpc: {
64
- system: "jsonrpc",
65
- method,
66
81
  jsonrpc: {
67
82
  error_code: err.code,
68
83
  error_message: err.message,
@@ -71,21 +86,75 @@ export namespace TraceContext {
71
86
  });
72
87
  }
73
88
 
74
- export function addJsonRPCParameters(ctx: TraceContext, method: string, args: any[]) {
89
+ export function addJsonRPCParameters(ctx: TraceContext, params: { [key: string]: any }) {
75
90
  if (!ctx.span) {
76
91
  return;
77
92
  }
78
93
 
79
- ctx.span.addTags({
94
+ setJsonRPCMetadata(ctx);
95
+ addNestedTags(ctx, {
80
96
  rpc: {
81
- system: "jsonrpc",
82
- method,
83
97
  jsonrpc: {
84
- parameters: args.slice(),
98
+ parameters: params,
85
99
  },
86
100
  },
87
101
  });
88
102
  }
103
+
104
+ /**
105
+ * Does what one would expect from `span.addTags`: Calls `span.addTag` for all keys in map, recursively for objects.
106
+ * Example:
107
+ * ```
108
+ * TraceContext.addNestedTags(ctx, {
109
+ * rpc: {
110
+ * system: "jsonrpc",
111
+ * jsonrpc: {
112
+ * version: "1.0",
113
+ * method: "test",
114
+ * parameters: ["abc", "def"],
115
+ * },
116
+ * },
117
+ * });
118
+ * ```
119
+ * gives
120
+ * rpc.system = "jsonrpc"
121
+ * rpc.jsonrpc.version = "1.0"
122
+ * rpc.jsonrpc.method = "test"
123
+ * rpc.jsonrpc.parameters.0 = "abc"
124
+ * rpc.jsonrpc.parameters.1 = "def"
125
+ * @param ctx
126
+ * @param keyValueMap
127
+ * @returns
128
+ */
129
+ export function addNestedTags(ctx: TraceContext, keyValueMap: { [key: string]: any }, _namespace?: string) {
130
+ if (!ctx.span) {
131
+ return;
132
+ }
133
+ const namespace = _namespace ? `${_namespace}.` : '';
134
+
135
+ try {
136
+ for (const k of Object.keys(keyValueMap)) {
137
+ const v = keyValueMap[k];
138
+ if (v instanceof Object) {
139
+ addNestedTags(ctx, v, `${namespace}${k}`);
140
+ } else {
141
+ ctx.span.setTag(`${namespace}${k}`, v);
142
+ }
143
+ }
144
+ } catch (err) {
145
+ // general resilience against odd shapes/parameters
146
+ log.error("Tracing.addNestedTags", err, { namespace });
147
+ }
148
+ }
149
+
150
+ export function setOWI(ctx: TraceContext, owi: LogContext) {
151
+ if (!ctx.span) {
152
+ return;
153
+ }
154
+ addNestedTags(ctx, {
155
+ context: owi,
156
+ });
157
+ }
89
158
  }
90
159
 
91
160
  @injectable()
@@ -101,11 +170,16 @@ export class TracingManager {
101
170
  reporter: {
102
171
  logSpans: false
103
172
  },
104
- serviceName
173
+ serviceName,
105
174
  }
106
175
  const t = initTracerFromEnv(config, {
107
- logger: console
176
+ logger: console,
177
+ tags: {
178
+ 'service.build.commit': process.env.GITPOD_BUILD_GIT_COMMIT,
179
+ 'service.build.version': process.env.GITPOD_BUILD_VERSION,
180
+ }
108
181
  });
182
+
109
183
  if (opts) {
110
184
  if (opts.perOpSampling) {
111
185
  (t as any)._sampler = new PerOperationSampler((t as any)._sampler, opts.perOpSampling);
@@ -1,22 +0,0 @@
1
- /**
2
- * Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
- * Licensed under the GNU Affero General Public License (AGPL).
4
- * See License-AGPL.txt in the project root for license information.
5
- */
6
- import { Deferred } from "./deferred";
7
- /**
8
- * Repeats a given function until it is stopped
9
- */
10
- export declare class Repeater {
11
- protected readonly fn: () => Promise<void> | void;
12
- protected readonly timeout: number;
13
- protected shouldRun: boolean;
14
- protected finished: Deferred<void>;
15
- protected timer?: any;
16
- constructor(fn: () => Promise<void> | void, timeout: number);
17
- start(): Promise<void>;
18
- run(): Promise<void>;
19
- protected sleep(timeout: number): Promise<unknown>;
20
- stop(): Promise<void>;
21
- }
22
- //# sourceMappingURL=repeater.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"repeater.d.ts","sourceRoot":"","sources":["../../src/util/repeater.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC;;GAEG;AACH,qBAAa,QAAQ;IAKL,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAAE,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM;IAJjG,SAAS,CAAC,SAAS,EAAE,OAAO,CAAC;IAC7B,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAA;IAClC,SAAS,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC;gBAES,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,EAAqB,OAAO,EAAE,MAAM;IAE3F,KAAK;IAIL,GAAG;cAcO,KAAK,CAAC,OAAO,EAAE,MAAM;IAI/B,IAAI;CAOb"}
@@ -1,65 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) 2020 Gitpod GmbH. All rights reserved.
4
- * Licensed under the GNU Affero General Public License (AGPL).
5
- * See License-AGPL.txt in the project root for license information.
6
- */
7
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
- return new (P || (P = Promise))(function (resolve, reject) {
10
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
- step((generator = generator.apply(thisArg, _arguments || [])).next());
14
- });
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Repeater = void 0;
18
- const deferred_1 = require("./deferred");
19
- const logging_1 = require("./logging");
20
- /**
21
- * Repeats a given function until it is stopped
22
- */
23
- class Repeater {
24
- constructor(fn, timeout) {
25
- this.fn = fn;
26
- this.timeout = timeout;
27
- }
28
- start() {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- this.run();
31
- });
32
- }
33
- run() {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- this.shouldRun = true;
36
- this.finished = new deferred_1.Deferred();
37
- while (this.shouldRun) {
38
- try {
39
- yield this.fn();
40
- }
41
- catch (err) {
42
- logging_1.log.error(err);
43
- }
44
- yield this.sleep(this.timeout);
45
- }
46
- this.finished.resolve();
47
- });
48
- }
49
- sleep(timeout) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- return new Promise(resolve => this.timer = setTimeout(resolve, timeout));
52
- });
53
- }
54
- stop() {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- this.shouldRun = false;
57
- if (this.timer) {
58
- clearTimeout(this.timer);
59
- }
60
- return this.finished.promise;
61
- });
62
- }
63
- }
64
- exports.Repeater = Repeater;
65
- //# sourceMappingURL=repeater.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"repeater.js","sourceRoot":"","sources":["../../src/util/repeater.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;;;;;;;AAEH,yCAAsC;AACtC,uCAAgC;AAEhC;;GAEG;AACH,MAAa,QAAQ;IAKjB,YAA+B,EAA8B,EAAqB,OAAe;QAAlE,OAAE,GAAF,EAAE,CAA4B;QAAqB,YAAO,GAAP,OAAO,CAAQ;IAAI,CAAC;IAEhG,KAAK;;YACP,IAAI,CAAC,GAAG,EAAE,CAAC;QACf,CAAC;KAAA;IAEK,GAAG;;YACL,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,QAAQ,GAAG,IAAI,mBAAQ,EAAQ,CAAC;YACrC,OAAO,IAAI,CAAC,SAAS,EAAE;gBACnB,IAAI;oBACA,MAAM,IAAI,CAAC,EAAE,EAAE,CAAC;iBACnB;gBAAC,OAAO,GAAG,EAAE;oBACV,aAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;iBAClB;gBACD,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAClC;YACD,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;QAC5B,CAAC;KAAA;IAEe,KAAK,CAAC,OAAe;;YACjC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;QAC7E,CAAC;KAAA;IAEK,IAAI;;YACN,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;YACvB,IAAI,IAAI,CAAC,KAAK,EAAE;gBACZ,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aAC5B;YACD,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QACjC,CAAC;KAAA;CACJ;AApCD,4BAoCC"}
@@ -1,105 +0,0 @@
1
- /**
2
- * Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
- * Licensed under the GNU Affero General Public License (AGPL).
4
- * See License-AGPL.txt in the project root for license information.
5
- */
6
-
7
- // Type definitions for jaeger-client
8
- // Project: https://github.com/uber/jaeger-client-node
9
- // Definitions by: Julian Steger <https://github.com/julianste>
10
- // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
-
12
- declare module 'jaeger-client' {
13
- import * as opentracing from "opentracing";
14
-
15
- export interface TracingConfig {
16
- serviceName?: string;
17
- disable?: boolean;
18
- sampler?: SamplerConfig;
19
- reporter?: ReporterConfig;
20
- }
21
-
22
- export interface TracingOptions {
23
- reporter?: Reporter;
24
- metrics?: MetricsFactory;
25
- logger?: Logger;
26
- tags?: any;
27
- }
28
-
29
- export interface ReporterConfig {
30
- logSpans?: boolean;
31
- agentHost?: string;
32
- agentPort?: number;
33
- flushIntervalMs?: number;
34
- }
35
-
36
- export interface SamplerConfig {
37
- type: string;
38
- param: number;
39
- host?: string;
40
- port?: number;
41
- refreshIntervalMs?: number;
42
- }
43
-
44
- export interface Logger {
45
- info(msg: string): void;
46
- error(msg: string): void;
47
- }
48
-
49
- export interface Reporter {
50
- report(span: opentracing.Span): void;
51
- close(callback?: () => void): void;
52
- setProcess(serviceName: string, tags: any): void;
53
- }
54
-
55
- export interface MetricsFactory {
56
- createCounter(name: string, tags: any): Counter;
57
- createTimer(name: string, tags: any): Timer;
58
- createGauge(name: string, tags: any): Gauge;
59
- }
60
-
61
- // Counter tracks the number of times an event has occurred
62
- export interface Counter {
63
- // Adds the given value to the counter.
64
- increment(delta: number): void;
65
- }
66
-
67
- // Timer tracks how long an operation took and also computes percentiles.
68
- export interface Timer {
69
- // Records the time passed in.
70
- record(value: number): void;
71
- }
72
-
73
- // Gauge returns instantaneous measurements of something as an int64 value
74
- export interface Gauge {
75
- // Update the gauge to the value passed in.
76
- update(value: number): void;
77
- }
78
-
79
- export function initTracer(
80
- tracingConfig: TracingConfig,
81
- tracingOptions: TracingOptions,
82
- ): opentracing.Tracer;
83
-
84
- export function initTracerFromEnv(
85
- tracingConfig: TracingConfig,
86
- tracingOptions: TracingOptions,
87
- ): opentracing.Tracer;
88
-
89
- export interface SamplingDecision {
90
- sample: boolean;
91
- retryable: boolean;
92
- tags: any;
93
- }
94
-
95
- // added by TypeFox
96
- export interface Sampler {
97
- name(): string
98
- isSampled(operation: string, tags: any): boolean;
99
- onCreateSpan(span: opentracing.Span): SamplingDecision;
100
- onSetOperationName(span: opentracing.Span, operationName: string): SamplingDecision;
101
- onSetTag(span: opentracing.Span, key: string, value: any): SamplingDecision;
102
- close(callback: () => void): void
103
- }
104
-
105
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
- * Licensed under the GNU Affero General Public License (AGPL).
4
- * See License-AGPL.txt in the project root for license information.
5
- */
6
-
7
- import { Deferred } from "./deferred";
8
- import { log } from './logging';
9
-
10
- /**
11
- * Repeats a given function until it is stopped
12
- */
13
- export class Repeater {
14
- protected shouldRun: boolean;
15
- protected finished: Deferred<void>
16
- protected timer?: any;
17
-
18
- constructor(protected readonly fn: () => Promise<void> | void, protected readonly timeout: number) { }
19
-
20
- async start() {
21
- this.run();
22
- }
23
-
24
- async run() {
25
- this.shouldRun = true;
26
- this.finished = new Deferred<void>();
27
- while (this.shouldRun) {
28
- try {
29
- await this.fn();
30
- } catch (err) {
31
- log.error(err);
32
- }
33
- await this.sleep(this.timeout);
34
- }
35
- this.finished.resolve();
36
- }
37
-
38
- protected async sleep(timeout: number) {
39
- return new Promise(resolve => this.timer = setTimeout(resolve, timeout));
40
- }
41
-
42
- async stop() {
43
- this.shouldRun = false;
44
- if (this.timer) {
45
- clearTimeout(this.timer);
46
- }
47
- return this.finished.promise;
48
- }
49
- }