@dremio/js-sdk 0.12.2 → 0.12.3
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.
|
@@ -16,11 +16,37 @@
|
|
|
16
16
|
import { Job } from "./Job.js";
|
|
17
17
|
import { Query } from "../../common/Query.js";
|
|
18
18
|
import { AsyncResult } from "ts-results-es";
|
|
19
|
-
import {
|
|
19
|
+
import { from, repeat, shareReplay, takeWhile, tap } from "rxjs";
|
|
20
20
|
import { fromAbortable } from "../../common/fromAbortable.js";
|
|
21
21
|
import { jobEntityToProperties } from "./utils/jobEntityToProperties.js";
|
|
22
|
+
import moize from "moize";
|
|
22
23
|
export class JobsResource {
|
|
23
24
|
#config;
|
|
25
|
+
#createJobObservable = (id) => from(fromAbortable(({ signal }) => this.retrieve(id, { signal }))).pipe(repeat({ delay: 1000 }), takeWhile((jobResult) => jobResult.isOk() && !jobResult.value.settled, true));
|
|
26
|
+
/**
|
|
27
|
+
* Reuse the same observable every time the `observe` method is called.
|
|
28
|
+
*/
|
|
29
|
+
#jobObserverCache = moize.default((id) => this.#createJobObservable(id).pipe(tap({
|
|
30
|
+
finalize: () => {
|
|
31
|
+
this.#jobObserverCache.remove([id]);
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
/**
|
|
35
|
+
* Reuse the same polling for multiple subscribers, buffering only the latest result.
|
|
36
|
+
* Make sure to unsubscribe from the source to stop polling when there are no subscribers (refCount: true)
|
|
37
|
+
*/
|
|
38
|
+
shareReplay({
|
|
39
|
+
bufferSize: 1,
|
|
40
|
+
refCount: true,
|
|
41
|
+
})), {
|
|
42
|
+
/**
|
|
43
|
+
* Evict cached observables after 60s. We need some way to prevent a memory
|
|
44
|
+
* leak for long-running SDK processes, and a maxAge of 60 seconds should be
|
|
45
|
+
* enough to handle multiple subscribers attached programmatically to a job.
|
|
46
|
+
* If it's not, then the worst case is that we'd make additional API calls.
|
|
47
|
+
*/
|
|
48
|
+
maxAge: 60_000,
|
|
49
|
+
});
|
|
24
50
|
constructor(config) {
|
|
25
51
|
this.#config = config;
|
|
26
52
|
}
|
|
@@ -51,29 +77,7 @@ export class JobsResource {
|
|
|
51
77
|
return new AsyncResult(this.create(query)).andThen((jobId) => this.retrieve(jobId)).promise;
|
|
52
78
|
}
|
|
53
79
|
observe(id) {
|
|
54
|
-
|
|
55
|
-
* Check every 1000ms for job status changes. 1000ms may seem too frequent,
|
|
56
|
-
* but it helps keep the client feeling snappy, especially for jobs that complete quickly.
|
|
57
|
-
*/
|
|
58
|
-
return timer(0, 1000).pipe(
|
|
59
|
-
/**
|
|
60
|
-
* Use `concatMap` instead of `switchMap` to avoid a situation where the call takes longer than
|
|
61
|
-
* 1000ms (possibly due to network conditions or API slowdowns) which would cause switchMap to
|
|
62
|
-
* constantly cancel unfinished requests (in which case, no requests would ever complete)
|
|
63
|
-
*/
|
|
64
|
-
concatMap(() => fromAbortable(({ signal }) => this.retrieve(id, { signal }))),
|
|
65
|
-
/**
|
|
66
|
-
* Continue polling until the job has reached a settled state
|
|
67
|
-
*/
|
|
68
|
-
takeWhile((jobResult) => jobResult.isOk() && !jobResult.value.settled, true),
|
|
69
|
-
/**
|
|
70
|
-
* Reuse the same polling for multiple subscribers, buffering only the latest result.
|
|
71
|
-
* Make sure to unsubscribe from the source to stop polling when there are no subscribers (refCount: true)
|
|
72
|
-
*/
|
|
73
|
-
shareReplay({
|
|
74
|
-
bufferSize: 1,
|
|
75
|
-
refCount: true,
|
|
76
|
-
}));
|
|
80
|
+
return this.#jobObserverCache(id);
|
|
77
81
|
}
|
|
78
82
|
retrieve(id, { signal } = {}) {
|
|
79
83
|
return this.#config
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JobsResource.js","sourceRoot":"","sources":["../../../src/oss/jobs/JobsResource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE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C,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC;IAC9F,CAAC;IAED,OAAO,CAAC,EAAU;QAChB,OAAO,IAAI,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;IACpC,CAAC;IAED,QAAQ,CAAC,EAAU,EAAE,EAAE,MAAM,KAAkB,EAAE;QAC/C,OAAO,IAAI,CAAC,OAAO;aAChB,cAAc,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;aACvC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAwB,CAAC;aAC9C,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;IAC9D,CAAC;CACF","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { SonarV3Config } from \"../../common/Config.ts\";\nimport { Job } from \"./Job.ts\";\nimport { Query } from \"../../common/Query.ts\";\nimport { AsyncResult } from \"ts-results-es\";\nimport type { SignalParam } from \"../../common/Params.ts\";\nimport { from, repeat, shareReplay, takeWhile, tap } from \"rxjs\";\nimport { fromAbortable } from \"../../common/fromAbortable.ts\";\nimport { jobEntityToProperties, type JobEntity } from \"./utils/jobEntityToProperties.ts\";\nimport moize from \"moize\";\n\nexport class JobsResource {\n #config: SonarV3Config;\n\n #createJobObservable = (id: string) =>\n from(fromAbortable(({ signal }) => this.retrieve(id, { signal }))).pipe(\n repeat({ delay: 1000 }),\n takeWhile((jobResult) => jobResult.isOk() && !jobResult.value.settled, true),\n );\n\n /**\n * Reuse the same observable every time the `observe` method is called.\n */\n #jobObserverCache = moize.default(\n (id: string) =>\n this.#createJobObservable(id).pipe(\n tap({\n finalize: () => {\n this.#jobObserverCache.remove([id]);\n },\n }),\n /**\n * Reuse the same polling for multiple subscribers, buffering only the latest result.\n * Make sure to unsubscribe from the source to stop polling when there are no subscribers (refCount: true)\n */\n shareReplay({\n bufferSize: 1,\n refCount: true,\n }),\n ),\n {\n /**\n * Evict cached observables after 60s. We need some way to prevent a memory\n * leak for long-running SDK processes, and a maxAge of 60 seconds should be\n * enough to handle multiple subscribers attached programmatically to a job.\n * If it's not, then the worst case is that we'd make additional API calls.\n */\n maxAge: 60_000,\n },\n );\n\n constructor(config: SonarV3Config) {\n this.#config = config;\n }\n\n _jobFromEntity(id: string, entity: JobEntity): Job {\n return new Job(jobEntityToProperties(id, entity), this.#config, this.observe.bind(this));\n }\n\n create(query: Query) {\n return this.#config\n .sonarV3Request(`sql`, {\n body: JSON.stringify({\n context: query.context,\n sql: query.sql,\n }),\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n keepalive: true,\n method: \"POST\",\n })\n .map((response) => response.json() as Promise<{ id: string }>)\n .map((response) => response.id).promise;\n }\n\n /**\n * A convenience method to combine job creation and retrieval\n */\n createAndRetrieve(query: Query) {\n return new AsyncResult(this.create(query)).andThen((jobId) => this.retrieve(jobId)).promise;\n }\n\n observe(id: string) {\n return this.#jobObserverCache(id);\n }\n\n retrieve(id: string, { signal }: SignalParam = {}) {\n return this.#config\n .sonarV3Request(`job/${id}`, { signal })\n .map((res) => res.json() as Promise<JobEntity>)\n .map((entity) => this._jobFromEntity(id, entity)).promise;\n }\n}\n"]}
|