Skip to content

Commit

Permalink
fix: add lock init for egg app (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 30, 2020
1 parent 0755a90 commit ccb5fe5
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/egg-layer/framework/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exports.watcher = false;
exports.development = false;
exports.logrotator = false;
exports.schedule = false;
exports.static = false;
1 change: 1 addition & 0 deletions packages/egg-layer/test/fixtures/eaas-fc/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/** @type Egg.EggPlugin */
module.exports = {
static: true,
// had enabled by egg
// static: {
// enable: true,
Expand Down
1 change: 1 addition & 0 deletions packages/egg-layer/test/fixtures/eaas-scf/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/** @type Egg.EggPlugin */
module.exports = {
static: true,
// had enabled by egg
// static: {
// enable: true,
Expand Down
7 changes: 7 additions & 0 deletions packages/faas-cli-command-core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ export class BasePlugin implements IPluginInstance {
public getStore(key: string, scope?: string) {
return this.core.store.get(`${scope || this.name}:${key}`);
}

setGlobalDependencies(name: string, version?: string) {
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies[name] = version || '*';
}
}
7 changes: 0 additions & 7 deletions packages/faas-cli-plugin-aws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,6 @@ export class AWSLambdaPlugin extends BasePlugin {
]);
return this.firstValue(values);
}

setGlobalDependencies(name: string, version?: string) {
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies[name] = version || '*';
}
}

function sleep(sec: number) {
Expand Down
8 changes: 0 additions & 8 deletions packages/faas-cli-plugin-fc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,4 @@ export class AliyunFCPlugin extends BasePlugin {
}
return func;
}

// 设置全局依赖,在package的时候会读取
setGlobalDependencies(name: string, version?: string) {
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies[name] = version || '*';
}
}
13 changes: 9 additions & 4 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,7 @@ export class PackagePlugin extends BasePlugin {
this.core.cli.log('Aggregation Deploy');

// use picomatch to match url
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies['picomatch'] = '*';
this.setGlobalDependencies('picomatch');
const allAggregationPaths = [];
let allFuncNames = Object.keys(this.core.service.functions);
for (const aggregationName in this.core.service.aggregation) {
Expand Down Expand Up @@ -678,6 +675,14 @@ export class PackagePlugin extends BasePlugin {
const service: any = this.core.service;
if (service?.deployType) {
this.core.cli.log(` - found deployType: ${service?.deployType}`);

this.setGlobalDependencies('@midwayjs/simple-lock');

if (!service.provider.initTimeout || service.provider.initTimeout < 10) {
// just for aliyun
service.provider.initTimeout = 10;
}

// add default function
if (!service.functions || Object.keys(service.functions).length === 0) {
this.core.cli.log(' - create default functions');
Expand Down
5 changes: 5 additions & 0 deletions packages/faas-cli-plugin-package/test/package-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe('/test/package-a[[.test.ts', () => {
readFileSync(join(buildPath, 'f.yml')).toString('utf8')
)
);
assert(
/initTimeout: 10/.test(
readFileSync(join(buildPath, 'f.yml')).toString('utf8')
)
);
});
});
});
8 changes: 0 additions & 8 deletions packages/faas-cli-plugin-scf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ export class TencentSCFPlugin extends BasePlugin {
return func;
}

// 设置全局依赖,在package的时候会读取
setGlobalDependencies(name: string, version?: string) {
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies[name] = version || '*';
}

async getTencentServerless(artifact) {
Object.assign(
this.core.service,
Expand Down
12 changes: 8 additions & 4 deletions packages/serverless-spec-builder/wrapper_app.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { asyncWrapper, start } = require('<%=starter %>');
const SimpleLock = require('@midwayjs/simple-lock').default;
const lock = new SimpleLock();
const layers = [];
<% layerDeps.forEach(function(layer){ %>
try {
Expand All @@ -11,10 +13,12 @@ let runtime;
let inited = false;

const initializeMethod = async (initializeContext = {}) => {
runtime = await start({
layers: layers,
isAppMode: true
});
return lock.sureOnce(async () => {
runtime = await start({
layers: layers,
isAppMode: true
});
}, 'APP_START_LOCK_KEY');
};

exports.<%=initializer%> = asyncWrapper(async (...args) => {
Expand Down

0 comments on commit ccb5fe5

Please sign in to comment.