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: support Python and refactoring #13

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
tmp commit
oraluben committed Dec 1, 2022

Verified

This commit was signed with the committer’s verified signature.
oraluben Yichen Yan
commit 5ebc5633da40d2f31bec84f7e960c2edc7d3e28c
4 changes: 4 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -179,4 +179,8 @@ export abstract class AbstractPGO {
.once('error', rej);
});
}

async downloadArchiveStream() {

}
}
2 changes: 1 addition & 1 deletion src/javaIndex.ts
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ export class JavaStartupAcceleration {
if (ossEndpoint) {
this.ossEndpoint = ossEndpoint;
} else {
this.ossEndpoint = 'oss-${FC_REGION}-internal.aliyuncs.com'.replace('${FC_REGION}', this.region);
this.ossEndpoint = `oss-${this.region}-internal.aliyuncs.com`;
}
if (ossUtilUrl) {
this.ossUtilUrl = ossUtilUrl;
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -6,9 +6,9 @@ import { NodePGO } from './node';
import * as common from "./common"
import * as pycds from "./pycds"

const NODE_RUNTIME = 'node'
const JAVA_RUNTIME = 'java'

const SUPPORTED_NODE_RUNTIMES = ['nodejs14']
const SUPPORTED_PYTHON_RUNTIMES = ['python3.9']

/**
@@ -117,7 +117,7 @@ async function parseOptions(args: ComponentProps): Promise<PGOOptions> {
export default class PGOComponent {
defaultAccess = 'default';

constructor(params: any = {}) {}
constructor(params: any = {}) { }

async gen(params: ComponentProps) {
await this.index(params);
@@ -128,7 +128,7 @@ export default class PGOComponent {

var pgoInstance: common.AbstractPGO

if (options.lang === NODE_RUNTIME) {
if (SUPPORTED_NODE_RUNTIMES.indexOf(options.lang) != -1) {
pgoInstance = new NodePGO(options)
} else if (options.lang === JAVA_RUNTIME) {
// pgoInstance = new JavaStartupAccelerationComponent(options)
@@ -138,6 +138,6 @@ export default class PGOComponent {
common.error("cannot parse runtime language, try to specific `--module` or `--lang`.");
}

await pgoInstance.run();
await pgoInstance.run().catch(err => { common.error(`创建失败,跳过生成: ${err}`) });
}
}
3 changes: 2 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@ export class NodePGO extends AbstractPGO {
const tmpName = `${pgoFunctionName}-${Date.now()}`;
const tmpDir = join(tmpdir(), tmpName);
await ensureDir(tmpDir);
const entry = this.options.initializer.split('.');

const entry = this.options.initializer?.split('.');

// 拷贝 pgo 工具库
await copy(join(__dirname, '../pgoCommonUtils.js'), join(this.options.codeUri, 'pgoCommonUtils.js'));
41 changes: 24 additions & 17 deletions src/pycds.ts
Original file line number Diff line number Diff line change
@@ -15,7 +15,9 @@ export class PyCDS extends common.AbstractPGO {
.then(this.create_tmp_function.bind(this))
.then(this.download_from_tmp_function.bind(this))
.then(this.patch_orig_function.bind(this))
.finally(this.cleanup_tmp_function.bind(this))
.then(this.cleanup_tmp_function.bind(this), (reason) =>
this.cleanup_tmp_function.bind(this)().finally(_ => Promise.reject(reason))
)
}

async cleanup_artifacts(): Promise<void> {
@@ -34,24 +36,28 @@ export class PyCDS extends common.AbstractPGO {
}

async download_from_tmp_function(): Promise<void> {
common.debug('download')
return this.get_fcclient()
.then(client => client.get(`/proxy/${this.tmpContext.service}/${this.tmpContext.function}/pgo_dump/download`))
.then(data => writeFile(join(this.options.codeUri, 'cds.img'), data))
}

patch_orig_function(): Promise<any> {
throw new Error("Method not implemented.");
// throw new Error("Method not implemented.");
return Promise.resolve(undefined)
}

async create_tmp_function(): Promise<any> {
const tmpFunc = await common.copyToTmp('.')
common.debug(`copy original project to ${tmpFunc}`)

try {
execSync(
's build --use-docker' +
' --command "PYTHONUSERBASE=/code/.s/python pip3 install --user --upgrade code-data-share"',
{ cwd: tmpFunc }
const cmd = `s ${this.options.service}` + ' build --use-docker' +
' --command "PYTHONUSERBASE=/code/.s/python pip3 install --user --upgrade code-data-share"'
common.debug("正在使用 docker 环境安装启动加速组件,首次使用可能较慢。")
common.debug("如长时间无响应,可使用如下命令拉取成功后重新运行")
common.debug(`cd "${tmpFunc}" && ${cmd}`)
execSync(cmd, { cwd: tmpFunc }
)
} catch (error) {
common.error(error.message)
@@ -71,13 +77,18 @@ export class PyCDS extends common.AbstractPGO {

// create service
console.debug('create service')
const serviceName = "tmp-service-0"
const functionName = `dump-${uuid.v1()}`;
const triggerName = 't1'

const tmpSession = uuid.v1()
const serviceName = `pgo-service-${tmpSession}`
const functionName = `pgo-function-${tmpSession}`
const triggerName = `pgo-function-${tmpSession}`

this.tmpContext.service = serviceName
this.tmpContext.function = functionName
this.tmpContext.trigger = triggerName

return client.createService(serviceName, { description: '用于 Alinode Cloud Require Cache 生成', })
.catch(err => Promise.reject('service 创建失败,跳过生成'))
.then(_ => { this.tmpContext.service = serviceName })
.catch((err: any) => Promise.reject(new Error(`service 创建失败: ${err}`)))
.then(_ => {
return client.createFunction(serviceName, {
code: {
@@ -96,10 +107,8 @@ export class PyCDS extends common.AbstractPGO {
PYCDSMODE: 'TRACE',
PYCDSLIST: '/tmp/cds.lst'
},
})
}).catch((err: any) => Promise.reject(new Error(`function 创建失败: ${err}`)))
})
.catch(err => Promise.reject('function 创建失败,跳过生成'))
.then(_ => { this.tmpContext.function = functionName })
.then(_ => {
return client.createTrigger(serviceName, functionName, {
invocationRole: '',
@@ -108,9 +117,7 @@ export class PyCDS extends common.AbstractPGO {
triggerConfig: { authType: "anonymous", methods: ["GET"] },
triggerName: triggerName,
triggerType: 'http'
})
}).catch((err: any) => Promise.reject(new Error(`trigger 创建失败: ${err}`)))
})
.catch(err => Promise.reject('trigger 创建失败,跳过生成'))
.then(_ => { this.tmpContext.trigger = triggerName });
}
}