fix(core.gbapp): Review of async calls, specially on collection loops.
This commit is contained in:
parent
94993f1c56
commit
5d6dacc910
3 changed files with 38 additions and 53 deletions
|
@ -41,18 +41,15 @@ import { GuaribasConversation, GuaribasConversationMessage } from '../models';
|
||||||
* Base services for Bot Analytics.
|
* Base services for Bot Analytics.
|
||||||
*/
|
*/
|
||||||
export class AnalyticsService {
|
export class AnalyticsService {
|
||||||
|
|
||||||
public async createConversation(
|
public async createConversation(
|
||||||
user: GuaribasUser
|
user: GuaribasUser
|
||||||
): Promise<GuaribasConversation> {
|
): Promise<GuaribasConversation> {
|
||||||
return new Promise<GuaribasConversation>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
const conversation = new GuaribasConversation();
|
const conversation = new GuaribasConversation();
|
||||||
conversation.startedBy = user;
|
conversation.startedBy = user;
|
||||||
conversation.startedByUserId = user.userId;
|
conversation.startedByUserId = user.userId;
|
||||||
conversation.save().then((value: GuaribasConversation) => {
|
|
||||||
resolve(value);
|
return await conversation.save();
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createMessage(
|
public async createMessage(
|
||||||
|
@ -60,15 +57,10 @@ export class AnalyticsService {
|
||||||
user: GuaribasUser,
|
user: GuaribasUser,
|
||||||
content: string
|
content: string
|
||||||
): Promise<GuaribasConversationMessage> {
|
): Promise<GuaribasConversationMessage> {
|
||||||
return new Promise<GuaribasConversationMessage>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
const message = GuaribasConversationMessage.build();
|
const message = GuaribasConversationMessage.build();
|
||||||
message.conversation = conversation;
|
message.conversation = conversation;
|
||||||
message.user = user;
|
message.user = user;
|
||||||
message.content = content;
|
message.content = content;
|
||||||
message.save().then((value: GuaribasConversationMessage) => {
|
return await message.save();
|
||||||
resolve(value);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,7 @@ import { KBService } from './../../kb.gbapp/services/KBService';
|
||||||
import { GBConfigService } from './GBConfigService';
|
import { GBConfigService } from './GBConfigService';
|
||||||
import { GBImporter } from './GBImporterService';
|
import { GBImporter } from './GBImporterService';
|
||||||
import { GBVMService } from './GBVMService';
|
import { GBVMService } from './GBVMService';
|
||||||
import { min } from 'moment';
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||||
import { GBMinService } from './GBMinService';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -93,7 +92,7 @@ export class GBDeployer {
|
||||||
const _this = this;
|
const _this = this;
|
||||||
|
|
||||||
return new Promise(
|
return new Promise(
|
||||||
(resolve: any, reject: any): any => {
|
async (resolve: any, reject: any)=> {
|
||||||
GBLog.info(`PWD ${process.env.PWD}...`);
|
GBLog.info(`PWD ${process.env.PWD}...`);
|
||||||
let totalPackages = 0;
|
let totalPackages = 0;
|
||||||
let paths = [urlJoin(process.env.PWD, GBDeployer.deployFolder), urlJoin(process.env.PWD, GBDeployer.workFolder)];
|
let paths = [urlJoin(process.env.PWD, GBDeployer.deployFolder), urlJoin(process.env.PWD, GBDeployer.workFolder)];
|
||||||
|
@ -105,7 +104,7 @@ export class GBDeployer {
|
||||||
const gbappPackages: string[] = [];
|
const gbappPackages: string[] = [];
|
||||||
let generalPackages: string[] = [];
|
let generalPackages: string[] = [];
|
||||||
|
|
||||||
function doIt(path) {
|
async function scanPackageDirectory(path) {
|
||||||
const isDirectory = source => Fs.lstatSync(source).isDirectory();
|
const isDirectory = source => Fs.lstatSync(source).isDirectory();
|
||||||
const getDirectories = source =>
|
const getDirectories = source =>
|
||||||
Fs.readdirSync(source)
|
Fs.readdirSync(source)
|
||||||
|
@ -113,7 +112,7 @@ export class GBDeployer {
|
||||||
.filter(isDirectory);
|
.filter(isDirectory);
|
||||||
|
|
||||||
const dirs = getDirectories(path);
|
const dirs = getDirectories(path);
|
||||||
dirs.forEach(element => {
|
await CollectionUtil.asyncForEach(dirs, async element => {
|
||||||
if (element === '.') {
|
if (element === '.') {
|
||||||
GBLog.info(`Ignoring ${element}...`);
|
GBLog.info(`Ignoring ${element}...`);
|
||||||
} else {
|
} else {
|
||||||
|
@ -129,14 +128,14 @@ export class GBDeployer {
|
||||||
}
|
}
|
||||||
|
|
||||||
GBLog.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
|
GBLog.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
|
||||||
paths.forEach(e => {
|
await CollectionUtil.asyncForEach(paths, async e => {
|
||||||
GBLog.info(`Looking in: ${e}...`);
|
GBLog.info(`Looking in: ${e}...`);
|
||||||
doIt(e);
|
await scanPackageDirectory(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Deploys all .gbapp files first.
|
// Deploys all .gbapp files first.
|
||||||
|
|
||||||
const appPackagesProcessed = this.deployAppPackages(gbappPackages, core, appPackages);
|
const appPackagesProcessed = await this.deployAppPackages(gbappPackages, core, appPackages);
|
||||||
|
|
||||||
WaitUntil()
|
WaitUntil()
|
||||||
.interval(1000)
|
.interval(1000)
|
||||||
|
@ -404,7 +403,7 @@ export class GBDeployer {
|
||||||
|
|
||||||
// Deploys all .gbot files first.
|
// Deploys all .gbot files first.
|
||||||
|
|
||||||
botPackages.forEach(async (e) => {
|
await CollectionUtil.asyncForEach(botPackages, async e => {
|
||||||
if (e !== 'packages\\boot.gbot') {
|
if (e !== 'packages\\boot.gbot') {
|
||||||
GBLog.info(`Deploying bot: ${e}...`);
|
GBLog.info(`Deploying bot: ${e}...`);
|
||||||
await _this.deployBot(e, GBServer.globals.publicAddress);
|
await _this.deployBot(e, GBServer.globals.publicAddress);
|
||||||
|
@ -415,7 +414,7 @@ export class GBDeployer {
|
||||||
// Then all remaining generalPackages are loaded.
|
// Then all remaining generalPackages are loaded.
|
||||||
|
|
||||||
generalPackages = generalPackages.filter(p => !p.endsWith('.git'));
|
generalPackages = generalPackages.filter(p => !p.endsWith('.git'));
|
||||||
generalPackages.forEach(filename => {
|
await CollectionUtil.asyncForEach(generalPackages, async filename => {
|
||||||
const filenameOnly = Path.basename(filename);
|
const filenameOnly = Path.basename(filename);
|
||||||
GBLog.info(`Deploying package: ${filename}...`);
|
GBLog.info(`Deploying package: ${filename}...`);
|
||||||
|
|
||||||
|
@ -427,7 +426,7 @@ export class GBDeployer {
|
||||||
server.use(`/themes/${filenameOnly}`, express.static(filename));
|
server.use(`/themes/${filenameOnly}`, express.static(filename));
|
||||||
GBLog.info(`Theme (.gbtheme) assets accessible at: /themes/${filenameOnly}.`);
|
GBLog.info(`Theme (.gbtheme) assets accessible at: /themes/${filenameOnly}.`);
|
||||||
} else if (Path.extname(filename) === '.gbkb') {
|
} else if (Path.extname(filename) === '.gbkb') {
|
||||||
this.mountGBKBAssets( filenameOnly, filename);
|
this.mountGBKBAssets(filenameOnly, filename);
|
||||||
} else if (Path.extname(filename) === '.gbui') {
|
} else if (Path.extname(filename) === '.gbui') {
|
||||||
// Already Handled
|
// Already Handled
|
||||||
} else if (Path.extname(filename) === '.gbdialog') {
|
} else if (Path.extname(filename) === '.gbdialog') {
|
||||||
|
@ -474,9 +473,9 @@ export class GBDeployer {
|
||||||
return names.indexOf(name) > -1;
|
return names.indexOf(name) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private deployAppPackages(gbappPackages: string[], core: any, appPackages: any[]) {
|
private async deployAppPackages(gbappPackages: string[], core: any, appPackages: any[]) {
|
||||||
let appPackagesProcessed = 0;
|
let appPackagesProcessed = 0;
|
||||||
gbappPackages.forEach(e => {
|
await CollectionUtil.asyncForEach(gbappPackages, async e => {
|
||||||
const filenameOnly = Path.basename(e);
|
const filenameOnly = Path.basename(e);
|
||||||
|
|
||||||
// Skips .gbapp inside deploy folder.
|
// Skips .gbapp inside deploy folder.
|
||||||
|
@ -496,18 +495,12 @@ export class GBDeployer {
|
||||||
try {
|
try {
|
||||||
child_process.execSync(Path.join(process.env.PWD, 'node_modules/.bin/tsc'), { cwd: e });
|
child_process.execSync(Path.join(process.env.PWD, 'node_modules/.bin/tsc'), { cwd: e });
|
||||||
GBLog.info(`Importando o pacote '${e}' on dir ${process.env.PWD}`);
|
GBLog.info(`Importando o pacote '${e}' on dir ${process.env.PWD}`);
|
||||||
import(Path.join(process.env.PWD, e))
|
const m = await import(Path.join(process.env.PWD, e));
|
||||||
.then(m => {
|
|
||||||
const p = new m.Package();
|
const p = new m.Package();
|
||||||
p.loadPackage(core, core.sequelize);
|
p.loadPackage(core, core.sequelize);
|
||||||
appPackages.push(p);
|
appPackages.push(p);
|
||||||
GBLog.info(`App (.gbapp) deployed: ${e}.`);
|
GBLog.info(`App (.gbapp) deployed: ${e}.`);
|
||||||
appPackagesProcessed++;
|
appPackagesProcessed++;
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
GBLog.error(`Error deploying .gbapp package: ${e}\n${err}`);
|
|
||||||
appPackagesProcessed++;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error compiling .gbapp package ${e}:\n${error.stdout.toString()}`);
|
GBLog.error(`Error compiling .gbapp package ${e}:\n${error.stdout.toString()}`);
|
||||||
appPackagesProcessed++;
|
appPackagesProcessed++;
|
||||||
|
|
|
@ -4,6 +4,7 @@ import urlJoin = require('url-join');
|
||||||
import { GBService, IGBInstance } from 'botlib';
|
import { GBService, IGBInstance } from 'botlib';
|
||||||
import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models';
|
import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models';
|
||||||
import { ConversationReference } from 'botbuilder';
|
import { ConversationReference } from 'botbuilder';
|
||||||
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Security service layer.
|
* Security service layer.
|
||||||
|
@ -11,25 +12,24 @@ import { ConversationReference } from 'botbuilder';
|
||||||
export class SecService extends GBService {
|
export class SecService extends GBService {
|
||||||
public async importSecurityFile(localPath: string, instance: IGBInstance) {
|
public async importSecurityFile(localPath: string, instance: IGBInstance) {
|
||||||
const security = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'security.json'), 'utf8'));
|
const security = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'security.json'), 'utf8'));
|
||||||
security.groups.forEach(group => {
|
await CollectionUtil.asyncForEach(security.groups, async group => {
|
||||||
const groupDb = GuaribasGroup.build({
|
const groupDb = GuaribasGroup.build({
|
||||||
instanceId: instance.instanceId,
|
instanceId: instance.instanceId,
|
||||||
displayName: group.displayName
|
displayName: group.displayName
|
||||||
});
|
});
|
||||||
groupDb.save().then(g1 => {
|
const g1 = await groupDb.save();
|
||||||
group.users.forEach(user => {
|
await CollectionUtil.asyncForEach(group.users, async user => {
|
||||||
const userDb = GuaribasUser.build({
|
const userDb = GuaribasUser.build({
|
||||||
instanceId: instance.instanceId,
|
instanceId: instance.instanceId,
|
||||||
groupId: g1.groupId,
|
groupId: g1.groupId,
|
||||||
userName: user.userName
|
userName: user.userName
|
||||||
});
|
});
|
||||||
userDb.save().then(user2 => {
|
const user2 = await userDb.save();
|
||||||
const userGroup = GuaribasUserGroup.build();
|
const userGroup = GuaribasUserGroup.build();
|
||||||
userGroup.groupId = g1.groupId;
|
userGroup.groupId = g1.groupId;
|
||||||
userGroup.userId = user2.userId;
|
userGroup.userId = user2.userId;
|
||||||
userGroup.save();
|
await userGroup.save();
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue