Skip to content

Commit 9920a67

Browse files
authored
Merge pull request #113 from halcyon-tech/fix/startup_error
Fix/startup error
2 parents 2488a3d + 5d4b4d7 commit 9920a67

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

src/connection/sqlJob.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,58 @@ export class SQLJob {
4646

4747
id: string | undefined;
4848

49-
5049
public static getNewUniqueId(prefix: string = `id`): string {
5150
return prefix + (++SQLJob.uniqueIdCounter);
5251
}
5352

53+
public static async useExec() {
54+
let useExec = false;
55+
56+
const instance = getInstance();
57+
const connection = instance.getConnection();
58+
59+
const bashPathAvailable = connection.remoteFeatures[`bash`];
60+
if (bashPathAvailable) {
61+
const commandShellResult = await connection.sendCommand({
62+
command: `echo $SHELL`
63+
});
64+
if (!commandShellResult.stderr) {
65+
let userDefaultShell = commandShellResult.stdout.trim();
66+
if (userDefaultShell === bashPathAvailable) {
67+
useExec = true;
68+
}
69+
}
70+
}
71+
72+
return useExec;
73+
}
74+
5475
constructor(public options: JDBCOptions = {}) {}
5576
private async getChannel() {
5677
const instance = getInstance();
5778
const connection = instance.getConnection();
79+
80+
let useExec = await SQLJob.useExec();
81+
5882
return new Promise((resolve, reject) => {
5983
// Setting QIBM_JAVA_STDIO_CONVERT and QIBM_PASE_DESCRIPTOR_STDIO to make sure all PASE and Java converters are off
60-
connection.client.connection.exec(`QIBM_JAVA_STDIO_CONVERT=N QIBM_PASE_DESCRIPTOR_STDIO=B exec `+ServerComponent.getInitCommand(), {}, (err: any, stream: any, options: {encoding: `binary`}) => {
61-
if (err)
84+
const startingCommand = `QIBM_JAVA_STDIO_CONVERT=N QIBM_PASE_DESCRIPTOR_STDIO=B QIBM_USE_DESCRIPTOR_STDIO=Y QIBM_MULTI_THREADED=Y ${useExec ? `exec ` : ``}` + ServerComponent.getInitCommand();
85+
86+
ServerComponent.writeOutput(startingCommand);
87+
88+
const a = connection.client.connection.exec(startingCommand, (err: any, stream: any, options: {encoding: `binary`}) => {
89+
if (err) {
6290
reject(err);
91+
ServerComponent.writeOutput(err);
92+
}
93+
6394
let outString = ``;
64-
stream.on(`data`, (data: Buffer) => {
95+
96+
stream.stderr.on(`data`, (data: Buffer) => {
97+
ServerComponent.writeOutput(data.toString());
98+
})
99+
100+
stream.stdout.on(`data`, (data: Buffer) => {
65101
outString += String(data);
66102
if (outString.endsWith(`\n`)) {
67103
let thisMsg = outString;
@@ -76,8 +112,11 @@ export class SQLJob {
76112
}
77113
}
78114
});
115+
79116
resolve(stream);
80-
})
117+
});
118+
119+
console.log(a);
81120
})
82121
}
83122

@@ -101,7 +140,15 @@ export class SQLJob {
101140
async connect(): Promise<ConnectionResult> {
102141
this.channel = await this.getChannel();
103142

104-
this.status = JobStatus.Ready;
143+
this.channel.on(`error`, (err) => {
144+
ServerComponent.writeOutput(err);
145+
this.dispose();
146+
})
147+
148+
this.channel.on(`close`, (code: number) => {
149+
ServerComponent.writeOutput(`Exited with code ${code}.`)
150+
this.dispose();
151+
})
105152

106153
const props = Object
107154
.keys(this.options)
@@ -132,13 +179,10 @@ export class SQLJob {
132179
throw new Error(connectResult.error || `Failed to connect to server.`);
133180
}
134181

135-
this.channel.on(`error`, () => {
136-
this.dispose();
137-
})
182+
if (this.status === JobStatus.Ended) {
183+
throw new Error(`Failed to connect properly.`);
184+
}
138185

139-
this.channel.on(`close`, () => {
140-
this.dispose();
141-
})
142186
this.id = connectResult.job;
143187
this.status = JobStatus.Ready;
144188

0 commit comments

Comments
 (0)