Skip to content

Commit

Permalink
feat: 增加跨云函数的全局配置对象;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 11, 2021
1 parent f0c04ab commit 8f753b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export const accessor = new MongoAccessor(Config.db.database, Config.db.uri, {
})

accessor.setLogger(getLogger('server:db', 'warning'))

accessor.init()

// 获取 db 对象
export const db = getDb(accessor)
export const db = getDb(accessor)

// 创建新 db 实例
export function createDb() {
return getDb(accessor)
}
41 changes: 23 additions & 18 deletions src/lib/faas/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { FunctionEngine, scheduler } from "."
import { db } from '../../lib/db'
import { createDb, db } from '../../lib/db'
import { LocalFileStorage } from "../storage/local_file_storage"
import request from 'axios'
import Config from "../../config"
import { CloudFunctionStruct, CloudSdkInterface, FunctionContext } from "./types"

/**
* 创建云函数 cloud sdk
* @returns
*/
function createCloudSdk(): CloudSdkInterface {

const less: CloudSdkInterface = {
database: () => db,
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
fetch: request,
invoke: _invokeInFunction,
emit: (event: string, param: any) => scheduler.emit(event, param)
}

return less
}

/**
* 调用云函数
*/
Expand Down Expand Up @@ -80,6 +63,28 @@ export async function getCloudFunctionById(func_id: string): Promise<CloudFuncti
}



// 跨请求、跨函数的全局配置对象,单例(in memory)
const _shared_preference = new Map<string, any>()

/**
* 创建云函数 cloud sdk
* @returns
*/
function createCloudSdk(): CloudSdkInterface {

const less: CloudSdkInterface = {
database: () => createDb(),
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
fetch: request,
invoke: _invokeInFunction,
emit: (event: string, param: any) => scheduler.emit(event, param),
shared: _shared_preference
}

return less
}

/**
* 在云函数中[调用云函数]的函数
*/
Expand Down
2 changes: 2 additions & 0 deletions src/lib/faas/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export class TriggerScheduler {
this._triggers = await this.loadTriggers()

this.scheduleTimer()

this.emit('triggers.init')
}

public destroy() {
Expand Down
1 change: 1 addition & 0 deletions src/lib/faas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CloudSdkInterface {
database(): Db,
invoke: InvokeFunctionType
emit: EmitFunctionType
shared: Map<string, any>
}

// vm run context (global)
Expand Down

0 comments on commit 8f753b8

Please sign in to comment.