Skip to content

Commit 840aac3

Browse files
committed
feat: using containerStarted lifecycle function to import data in valkey and redis containers
1 parent fc277ca commit 840aac3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

packages/modules/redis/src/redis-container.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export class RedisContainer extends GenericContainer {
3232
return this;
3333
}
3434

35+
protected override async containerStarted(container: StartedTestContainer): Promise<void> {
36+
if (this.initialImportScriptFile) {
37+
await this.importInitialData(container);
38+
}
39+
}
40+
3541
public override async start(): Promise<StartedRedisContainer> {
3642
this.withCommand([
3743
"redis-server",
@@ -60,7 +66,7 @@ export class RedisContainer extends GenericContainer {
6066
return startedRedisContainer;
6167
}
6268

63-
private async importInitialData(container: StartedRedisContainer) {
69+
private async importInitialData(container: StartedTestContainer) {
6470
const re = await container.exec(`/tmp/import.sh ${this.password}`);
6571
if (re.exitCode != 0 || re.output.includes("ERR"))
6672
throw Error(`Could not import initial data from ${this.initialImportScriptFile}: ${re.output}`);

packages/modules/valkey/src/valkey-container.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export class ValkeyContainer extends GenericContainer {
3131
return this;
3232
}
3333

34+
protected override async containerStarted(container: StartedTestContainer): Promise<void> {
35+
if (this.initialImportScriptFile) {
36+
await this.importInitialData(container);
37+
}
38+
}
39+
3440
public override async start(): Promise<StartedValkeyContainer> {
3541
this.withCommand([
3642
"valkey-server",
@@ -54,12 +60,11 @@ export class ValkeyContainer extends GenericContainer {
5460
},
5561
]);
5662
}
57-
const startedContainer = new StartedValkeyContainer(await super.start(), this.password);
58-
if (this.initialImportScriptFile) await this.importInitialData(startedContainer);
59-
return startedContainer;
63+
64+
return new StartedValkeyContainer(await super.start(), this.password);
6065
}
6166

62-
private async importInitialData(container: StartedValkeyContainer) {
67+
private async importInitialData(container: StartedTestContainer) {
6368
const re = await container.exec(`/tmp/import.sh ${this.password || ""}`);
6469
if (re.exitCode !== 0 || re.output.includes("ERR")) {
6570
throw Error(`Could not import initial data from ${this.initialImportScriptFile}: ${re.output}`);

0 commit comments

Comments
 (0)