Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): add __init__ hook in runtime #1081

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
"buildah",
"buildx",
"Builtins",
"casbin",
"casdoor",
"chakra",
"Chakra",
"chatgpt",
Expand Down
4 changes: 0 additions & 4 deletions runtimes/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,3 @@ npm start
```bash
telepresence leave $APPID-$APPID
```

## Troubleshooting

- `telepresence helm install` failed for `arm64 / Apple Chip` cluster, please upgrade your telepresence to `v2.11.1` or later.
2 changes: 2 additions & 0 deletions runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import './support/function-log'
import './support/cloud-sdk'
import { FunctionCache } from './support/function-engine/cache'
import { DatabaseChangeStream } from './support/db-change-stream'
import { InitHook } from './support/init-hook'

const app = express()

DatabaseAgent.accessor.ready.then(() => {
FunctionCache.initialize()
DatabaseChangeStream.initialize()
InitHook.invoke()
})

if (process.env.NODE_ENV === 'development') {
Expand Down
41 changes: 41 additions & 0 deletions runtimes/nodejs/src/support/init-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { CLOUD_FUNCTION_COLLECTION, INIT_FUNCTION_NAME } from '../constants'
import { DatabaseAgent } from '../db'
import { CloudFunction, ICloudFunctionData } from './function-engine'
import { logger } from './logger'
import { generateUUID } from './utils'

/**
* Init hook for `__init__` cloud function
*/
export class InitHook {
static async invoke() {
const func = await this.getInitCloudFunction()
if (!func) {
return
}

const cf = new CloudFunction(func)
await cf.invoke({
method: 'INIT',
requestId: generateUUID(),
__function_name: func.name,
})

logger.info('Init hook invoked')
}

/**
* Get init hook cloud function
* @returns
*/
private static async getInitCloudFunction() {
const db = DatabaseAgent.db
const doc = await db
.collection<ICloudFunctionData>(CLOUD_FUNCTION_COLLECTION)
.findOne({
name: INIT_FUNCTION_NAME,
})

return doc
}
}
5 changes: 0 additions & 5 deletions server/.vscode/settings.json

This file was deleted.

1 change: 0 additions & 1 deletion server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
- [Prisma](https://www.prisma.io)
- [MinIO](https://min.io) object storage
- [APISIX](https://apisix.apache.org) gateway
- [Casdoor](https://casdoor.org/)

## Prerequisites

Expand Down
Loading