Skip to content

Commit 4148c55

Browse files
[DECO-345][DECO-341] Register Databricks task and kill task terminal if error (#237)
### Before change Running a task without sync destination created an unkillable sync task https://user-images.githubusercontent.com/88345179/203079921-70288e0f-0740-4557-958a-437bcb4db451.mov Databricks task was not registered. https://user-images.githubusercontent.com/88345179/203079963-263982da-2443-4fdd-b17b-98d333ff22eb.mov ### After change https://user-images.githubusercontent.com/88345179/203080156-2feea51e-6d19-4d21-b881-58bca25852ae.mov Also add more debug logs. Co-authored-by: Fabian Jakobs <[email protected]>
1 parent c9cec26 commit 4148c55

File tree

3 files changed

+56
-37
lines changed

3 files changed

+56
-37
lines changed

packages/databricks-sdk-js/src/logging/NamedLogger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ export class NamedLogger {
125125
context,
126126
loggingFnName,
127127
}: {
128-
context: Context;
129-
loggingFnName: string;
128+
context?: Context;
129+
loggingFnName?: string;
130130
}) {
131-
this._context = context;
132-
this._loggingFnName = loggingFnName;
131+
this._context = context ?? this._context;
132+
this._loggingFnName = loggingFnName ?? this._loggingFnName;
133133
return this;
134134
}
135135
}

packages/databricks-vscode/src/cli/BricksTasks.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "@databricks/databricks-sdk/dist";
12
import * as assert from "assert";
23
import {instance, mock, when} from "ts-mockito";
34
import {Uri} from "vscode";
@@ -59,6 +60,7 @@ describe(__filename, () => {
5960
/* eslint-disable @typescript-eslint/naming-convention */
6061
BRICKS_ROOT: Uri.file("/path/to/local/workspace").fsPath,
6162
DATABRICKS_CONFIG_PROFILE: "profile",
63+
DATABRICKS_CONFIG_FILE: undefined,
6264
HOME: process.env.HOME,
6365
PATH: process.env.PATH,
6466
/* eslint-enable @typescript-eslint/naming-convention */

packages/databricks-vscode/src/cli/BricksTasks.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ import {CliWrapper, Command, SyncType} from "./CliWrapper";
1414
import {ChildProcess, spawn, SpawnOptions} from "node:child_process";
1515
import {SyncState} from "../sync/CodeSynchronizer";
1616
import {BricksSyncParser} from "./BricksSyncParser";
17+
import {withLogContext} from "@databricks/databricks-sdk/dist/logging";
18+
import {Loggers} from "../logger";
19+
import {Context, context} from "@databricks/databricks-sdk/dist/context";
20+
21+
enum TaskSyncType {
22+
syncFull = "sync-full",
23+
sync = "sync",
24+
}
25+
const cliToTaskSyncType = new Map<SyncType, TaskSyncType>([
26+
["full", TaskSyncType.syncFull],
27+
["incremental", TaskSyncType.sync],
28+
]);
1729

1830
export class SyncTask extends Task {
1931
constructor(
@@ -25,10 +37,10 @@ export class SyncTask extends Task {
2537
super(
2638
{
2739
type: "databricks",
28-
task: syncType === "full" ? "sync-full" : "sync",
40+
task: cliToTaskSyncType.get(syncType) ?? "sync",
2941
},
3042
TaskScope.Workspace,
31-
syncType === "full" ? "sync-full" : "sync",
43+
cliToTaskSyncType.get(syncType) ?? "sync",
3244
"databricks",
3345
new CustomExecution(async (): Promise<Pseudoterminal> => {
3446
return new LazyCustomSyncTerminal(
@@ -49,14 +61,15 @@ export class SyncTask extends Task {
4961
}
5062

5163
static killAll() {
52-
let found = false;
5364
window.terminals.forEach((terminal) => {
54-
if (terminal.name === "sync") {
55-
found = true;
65+
if (
66+
Object.values(TaskSyncType)
67+
.map((e) => e as string)
68+
.includes(terminal.name)
69+
) {
5670
terminal.dispose();
5771
}
5872
});
59-
return found;
6073
}
6174
}
6275

@@ -168,41 +181,55 @@ export class LazyCustomSyncTerminal extends CustomSyncTerminal {
168181
) {
169182
super("", [], {}, syncStateCallback);
170183

184+
const ctx: Context = new Context({
185+
rootClassName: "LazyCustomSyncTerminal",
186+
rootFnName: "constructor",
187+
});
188+
171189
// hacky way to override properties with getters
172190
Object.defineProperties(this, {
173191
cmd: {
174192
get: () => {
175-
return this.getSyncCommand().command;
193+
return this.getSyncCommand(ctx).command;
176194
},
177195
},
178196
args: {
179197
get: () => {
180-
return this.getSyncCommand().args;
198+
return this.getSyncCommand(ctx).args;
181199
},
182200
},
183201
options: {
184202
get: () => {
185-
return this.getProcessOptions();
203+
return this.getProcessOptions(ctx);
186204
},
187205
},
188206
});
189207
}
190208

191-
getProcessOptions(): SpawnOptions {
209+
@withLogContext(Loggers.Extension)
210+
showErrorAndKillThis(msg: string, @context ctx?: Context) {
211+
ctx?.logger?.error(msg);
212+
window.showErrorMessage(msg);
213+
SyncTask.killAll();
214+
return new Error(msg);
215+
}
216+
217+
@withLogContext(Loggers.Extension)
218+
getProcessOptions(@context ctx?: Context): SpawnOptions {
192219
const workspacePath =
193220
this.connection.syncDestination?.vscodeWorkspacePath.fsPath;
194221
if (!workspacePath) {
195-
window.showErrorMessage("Can't start sync: No workspace opened!");
196-
throw new Error("Can't start sync: No workspace opened!");
222+
throw this.showErrorAndKillThis(
223+
"Can't start sync: No workspace opened!",
224+
ctx
225+
);
197226
}
198227

199228
const dbWorkspace = this.connection.databricksWorkspace;
200229
if (!dbWorkspace) {
201-
window.showErrorMessage(
202-
"Can't start sync: Databricks connection not configured!"
203-
);
204-
throw new Error(
205-
"Can't start sync: Databricks connection not configured!"
230+
throw this.showErrorAndKillThis(
231+
"Can't start sync: Databricks connection not configured!",
232+
ctx
206233
);
207234
}
208235

@@ -212,35 +239,25 @@ export class LazyCustomSyncTerminal extends CustomSyncTerminal {
212239
/* eslint-disable @typescript-eslint/naming-convention */
213240
BRICKS_ROOT: workspacePath,
214241
DATABRICKS_CONFIG_PROFILE: dbWorkspace.profile,
242+
DATABRICKS_CONFIG_FILE: process.env.DATABRICKS_CONFIG_FILE,
215243
HOME: process.env.HOME,
216244
PATH: process.env.PATH,
217245
/* eslint-enable @typescript-eslint/naming-convention */
218246
},
219247
} as SpawnOptions;
220248
}
221249

222-
getSyncCommand(): Command {
223-
if (
224-
this.connection.state !== "CONNECTED" &&
225-
(SyncTask.killAll() || this.killThis)
226-
) {
227-
this.killThis = true;
228-
return {
229-
args: [],
230-
command: "",
231-
};
232-
}
250+
@withLogContext(Loggers.Extension)
251+
getSyncCommand(@context ctx?: Context): Command {
233252
if (this.command) {
234253
return this.command;
235254
}
236255
const syncDestination = this.connection.syncDestination;
237256

238257
if (!syncDestination) {
239-
window.showErrorMessage(
240-
"Can't start sync: Databricks synchronization destination not configured!"
241-
);
242-
throw new Error(
243-
"Can't start sync: Databricks synchronization destination not configured!"
258+
throw this.showErrorAndKillThis(
259+
"Can't start sync: Databricks synchronization destination not configured!",
260+
ctx
244261
);
245262
}
246263

0 commit comments

Comments
 (0)