Skip to content

Commit

Permalink
feat(runtime): add __init__ hook in runtime (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Apr 25, 2023
1 parent dc25ae3 commit db62f1a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8,898 deletions.
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

0 comments on commit db62f1a

Please sign in to comment.