Skip to content

Commit

Permalink
Merge pull request #55 from ainize-team/feat/woojae/deployEventHandler
Browse files Browse the repository at this point in the history
feat: add eventHandler
  • Loading branch information
akastercomcom authored Sep 21, 2023
2 parents 97a6915 + 1718fc0 commit 4e92490
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
33 changes: 20 additions & 13 deletions src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,28 @@ export default class Ainize {
}

// FIXME(yoojin): add config type and change param type.
// TODO(yoojin, woojae): Deploy container, advanced.
async deploy({modelName, billingConfig, serviceUrl}: deployConfig): Promise<Model> {
// TODO(yoojin, woojae): Deploy container, advanced.
const deployer = this.ain.getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
};
if(!this.ain.isDefaultAccountExist()) {
throw new Error('you should login first');
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
serviceUrl = `https://${modelName}.ainetwork.xyz`;
}

await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
const result = await new Promise(async (resolve, reject) => {
const deployer = this.ain.getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
};
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
serviceUrl = `https://${modelName}.ainetwork.xyz`;
}
const modelPath = Path.app(modelName).root();
await this.handler.subscribe(modelPath, resolve);
await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
});
console.log(`${modelName} deploy success!`);
return this.model(modelName);
}

Expand Down
3 changes: 2 additions & 1 deletion src/controllers/modelController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export default class ModelController {
const result = await new Promise(async (resolve, reject) => {
const requestKey = Date.now();
const requesterAddress = this.ain.getAddress();
await this.handler.subscribe(requesterAddress, requestKey.toString(), modelName, resolve);
const responsePath = Path.app(modelName).response(requesterAddress, requestKey.toString());
await this.handler.subscribe(responsePath, resolve);
const requestPath = Path.app(modelName).request(requesterAddress, requestKey);
const requestOp = buildSetOperation("SET_VALUE", requestPath, {prompt: requestData});
const txBody = buildTxBody(requestOp);
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export default class Handler {
}
}

async subscribe(requester:string, recordId:string, appName: string, resolve: any) {
async subscribe(subscribePath: string, resolve: any) {
this.checkEventManager();
const responsePath = Path.app(appName).response(requester, recordId);

const subscribeId = await this.ain.getEventManager().subscribe(
"VALUE_CHANGED",
{
path: responsePath,
path: subscribePath,
event_source: "USER",
},
(valueChangedEvent: any) => {
Expand Down

0 comments on commit 4e92490

Please sign in to comment.