-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: force cleanup at boot with
SYNC_FORCE_REMOVE=true
(#956)
#### Migration notes None - [ ] The change comes with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added optional `SYNC_FORCE_REMOVE` configuration variable for typegate synchronization. - Introduced ability to forcefully remove cached typegraphs at boot. - Added a new method to retrieve all history entries from the Redis replicated map. - Introduced a new function to return a greeting based on a provided name. - Added a synchronization feature test suite for validating cleanup logic. - **Documentation** - Updated documentation to reflect new synchronization configuration option. - **Improvements** - Enhanced the `Typegate` class with a method to facilitate bulk removal of typegraphs during initialization. - Made the `replicatedMap` parameter publicly accessible in the `ReplicatedRegister` class constructor. - Updated configuration retrieval to include the new `forceRemove` property. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Yohe-Am <[email protected]>
- Loading branch information
1 parent
5bb03ef
commit 2ac0fdc
Showing
12 changed files
with
203 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
export function hello({ name }: { name: string }) { | ||
return `Hello ${name}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
from typegraph import t, typegraph, Policy, Graph | ||
from typegraph.runtimes.deno import DenoRuntime | ||
|
||
|
||
@typegraph() | ||
def sync(g: Graph): | ||
deno = DenoRuntime() | ||
public = Policy.public() | ||
|
||
g.expose( | ||
hello=deno.import_( | ||
t.struct({"name": t.string()}), | ||
t.string(), | ||
name="hello", | ||
module="scripts/hello.ts", | ||
secrets=["ULTRA_SECRET"], | ||
).with_policy(public) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
import { gql, Meta } from "test-utils/mod.ts"; | ||
import { connect } from "redis"; | ||
import { S3Client } from "aws-sdk/client-s3"; | ||
import { createBucket, listObjects, tryDeleteBucket } from "test-utils/s3.ts"; | ||
import { assertEquals } from "@std/assert"; | ||
import { clearSyncData, setupSync } from "test-utils/hooks.ts"; | ||
import { Typegate } from "@metatype/typegate/typegate/mod.ts"; | ||
import { | ||
defaultTypegateConfigBase, | ||
getTypegateConfig, | ||
SyncConfig, | ||
} from "@metatype/typegate/config.ts"; | ||
|
||
const redisKey = "typegraph"; | ||
const redisEventKey = "typegraph_event"; | ||
|
||
async function cleanUp(config: typeof syncConfig) { | ||
using redis = await connect(config.redis); | ||
await redis.del(redisKey); | ||
await redis.del(redisEventKey); | ||
|
||
const s3 = new S3Client(config.s3); | ||
await tryDeleteBucket(s3, config.s3Bucket); | ||
await createBucket(s3, config.s3Bucket); | ||
s3.destroy(); | ||
await redis.quit(); | ||
} | ||
|
||
const syncConfig = { | ||
redis: { | ||
hostname: "localhost", | ||
port: 6379, | ||
password: "password", | ||
db: 1, | ||
}, | ||
s3: { | ||
endpoint: "http://localhost:9000", | ||
region: "local", | ||
credentials: { | ||
accessKeyId: "minio", | ||
secretAccessKey: "password", | ||
}, | ||
forcePathStyle: true, | ||
}, | ||
s3Bucket: "metatype-deno-runtime-sync-test", | ||
}; | ||
|
||
async function spawnGate(syncConfig: SyncConfig) { | ||
const config = getTypegateConfig({ | ||
base: { | ||
...defaultTypegateConfigBase, | ||
}, | ||
}); | ||
|
||
return await Typegate.init({ | ||
...config, | ||
sync: syncConfig, | ||
}); | ||
} | ||
|
||
Meta.test( | ||
{ | ||
name: "Force cleanup at boot on sync mode", | ||
syncConfig, | ||
async setup() { | ||
await clearSyncData(syncConfig); | ||
await setupSync(syncConfig); | ||
}, | ||
async teardown() { | ||
await cleanUp(syncConfig); | ||
}, | ||
}, | ||
async (t) => { | ||
await t.should( | ||
"cleanup if forceRemove is true", | ||
async () => { | ||
const _engine = await t.engine("sync/sync.py", { | ||
secrets: { | ||
ULTRA_SECRET: | ||
"if_you_can_read_me_on_an_ERROR_there_is_a_bug", | ||
}, | ||
}); | ||
|
||
const s3 = new S3Client(syncConfig.s3); | ||
const initialObjects = await listObjects(s3, syncConfig.s3Bucket); | ||
assertEquals(initialObjects?.length, 3); | ||
|
||
const gateNoRemove = await spawnGate(syncConfig); | ||
const namesNoRemove = gateNoRemove.register.list().map(({ name }) => | ||
name | ||
); | ||
|
||
const gateAfterRemove = await spawnGate({ | ||
...syncConfig, | ||
forceRemove: true, | ||
}); | ||
const namesAfterRemove = gateAfterRemove.register.list().map(( | ||
{ name }, | ||
) => name); | ||
|
||
t.addCleanup(async () => { | ||
await gateNoRemove[Symbol.asyncDispose](); | ||
await gateAfterRemove[Symbol.asyncDispose](); | ||
}); | ||
|
||
assertEquals(namesNoRemove, ["sync"]); | ||
assertEquals(namesAfterRemove, []); // ! | ||
}, | ||
); | ||
}, | ||
); |