Skip to content

Commit e583350

Browse files
committed
nats: new streams were hanging on startup
1 parent 1f2a475 commit e583350

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

Diff for: src/packages/backend/nats/sync.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import { getEnv } from "@cocalc/backend/nats/env";
1010
export type { Stream, DStream, KV, DKV };
1111

1212
export async function stream(opts) {
13-
return await createStream({ ...opts, env: await getEnv() });
13+
return await createStream({ env: await getEnv(), ...opts });
1414
}
1515

1616
export async function dstream(opts) {
17-
return await createDstream({ ...opts, env: await getEnv() });
17+
return await createDstream({ env: await getEnv(), ...opts });
1818
}
1919

2020
export async function kv(opts) {
21-
return await createKV({ ...opts, env: await getEnv() });
21+
return await createKV({ env: await getEnv(), ...opts });
2222
}
2323

2424
export async function dkv(opts) {
25-
return await createDKV({ ...opts, env: await getEnv() });
25+
return await createDKV({ env: await getEnv(), ...opts });
2626
}

Diff for: src/packages/nats/sync/stream.ts

+3
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ export class Stream extends EventEmitter {
277277
// First we get info so we know how many messages
278278
// are already in the stream:
279279
const info = await consumer.info();
280+
if (info.num_pending == 0) {
281+
return consumer;
282+
}
280283
const fetch = await consumer.fetch();
281284
this.watch = fetch;
282285
let i = 0;

Diff for: src/packages/project/nats/api/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
/*
22
How to do development (so in a dev project doing cc-in-cc dev).
33
4-
0. From the browser, terminate this api server running in the project already, if any
4+
0. From the browser, terminate this api server running in the project:
55
6-
await cc.client.nats_client.projectApi({project_id:'00847397-d6a8-4cb0-96a8-6ef64ac3e6cf'}).system.terminate({service:'api'})
6+
> await cc.client.nats_client.projectApi({project_id:'00847397-d6a8-4cb0-96a8-6ef64ac3e6cf'}).system.terminate({service:'api'})
7+
8+
{status: 'terminated', service: 'api'}
79
810
1. Open a terminal in the project itself, which sets up the required environment variables, e.g.,
911
1012
- COCALC_NATS_JWT -- this has the valid JWT issued to grant the project rights to use nats
1113
- COCALC_PROJECT_ID
1214
13-
You can type the following into the miniterminal in a project and copy the output into a terminal here to
14-
setup the same environment and make starting this server act like this part of a project.
15+
You can type the following into the miniterminal in a project and copy the output into
16+
a terminal here to setup the same environment and make starting this server act like
17+
this part of a project.
1518
1619
export | grep -E "COCALC|HOME"
1720
@@ -21,7 +24,7 @@ setup the same environment and make starting this server act like this part of a
2124
2225
or just run node and paste
2326
24-
require("@cocalc/project/nats/api").init()
27+
require("@cocalc/project/nats/api/index").init()
2528
2629
if you want to easily be able to grab some state, e.g., global.x = {...} in some code.
2730

Diff for: src/packages/project/nats/sync.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ import {
66
import { kv as createKV, type KV } from "@cocalc/nats/sync/kv";
77
import { dkv as createDKV, type DKV } from "@cocalc/nats/sync/dkv";
88
import { getEnv } from "./env";
9+
import { project_id } from "@cocalc/project/data";
910

1011
export type { Stream, DStream, KV, DKV };
1112

1213
export async function stream(opts) {
13-
return await createStream({ ...opts, env: await getEnv() });
14+
return await createStream({ project_id, env: await getEnv(), ...opts });
1415
}
1516

1617
export async function dstream(opts) {
17-
return await createDstream({ ...opts, env: await getEnv() });
18+
return await createDstream({ project_id, env: await getEnv(), ...opts });
1819
}
1920

2021
export async function kv(opts) {
21-
return await createKV({ ...opts, env: await getEnv() });
22+
return await createKV({ project_id, env: await getEnv(), ...opts });
2223
}
2324

2425
export async function dkv(opts) {
25-
return await createDKV({ ...opts, env: await getEnv() });
26+
return await createDKV({ project_id, env: await getEnv(), ...opts });
2627
}

Diff for: src/packages/project/nats/terminal.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { JSONCodec } from "nats";
1414
import { getLogger } from "@cocalc/project/logger";
1515
import { readlink, realpath } from "node:fs/promises";
1616
import { dstream, type DStream } from "@cocalc/project/nats/sync";
17-
import { project_id } from "@cocalc/project/data";
1817
import { getSubject } from "./names";
1918
import getConnection from "./connection";
2019

@@ -141,7 +140,7 @@ class Session {
141140
};
142141

143142
createStream = async () => {
144-
this.stream = await dstream({ name: this.streamName, project_id });
143+
this.stream = await dstream({ name: this.streamName });
145144
};
146145

147146
init = async () => {
@@ -160,15 +159,17 @@ class Session {
160159
args.push(path_split(initFilename).tail);
161160
}
162161
const cwd = getCWD(head, this.options.cwd);
163-
logger.debug("creating pty with size", this.size);
162+
logger.debug("creating pty");
164163
this.pty = spawn(command, args, {
165164
cwd,
166165
env,
167166
rows: this.size?.rows,
168167
cols: this.size?.cols,
169168
});
170169
this.state = "running";
170+
logger.debug("creating stream");
171171
await this.createStream();
172+
logger.debug("connect stream to pty");
172173
this.pty.onData((data) => {
173174
this.handleBackendMessages(data);
174175
this.stream.publish({ data });

0 commit comments

Comments
 (0)