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

Added async-retry package for agent spin-up retries #6

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ConnectionService } from 'apps/connection/src/connection.service';
import { ResponseMessages } from '@credebl/common/response-messages';
import { io } from 'socket.io-client';
import { WebSocketGateway } from '@nestjs/websockets';
import * as retry from 'async-retry';

@Injectable()
@WebSocketGateway()
Expand Down Expand Up @@ -320,38 +321,38 @@ export class AgentServiceService {
try {


const agentDidWriteUrl = `${payload.agentEndPoint}${CommonConstants.URL_AGENT_WRITE_DID}`;
const agentDid = await this.commonService
.httpPost(agentDidWriteUrl, { seed: payload.seed }, { headers: { 'x-api-key': payload.apiKey } })
.then(async response => response);

if (agentDid) {

const getDidMethodUrl = `${payload.agentEndPoint}${CommonConstants.URL_AGENT_GET_DIDS}`;
const getDidMethod = await this.commonService
.httpGet(getDidMethodUrl, { headers: { 'x-api-key': payload.apiKey } })
.then(async response => response);

const storeOrgAgentData: IStoreOrgAgentDetails = {
did: getDidMethod[0]?.did,
verkey: getDidMethod[0]?.didDocument?.verificationMethod[0]?.publicKeyBase58,
isDidPublic: true,
agentSpinUpStatus: 2,
walletName: payload.walletName,
agentsTypeId: AgentType.AFJ,
orgId: payload.orgId,
agentEndPoint: payload.agentEndPoint,
agentId: payload.agentId,
orgAgentTypeId: OrgAgentType.DEDICATED
};
const agentDidWriteUrl = `${payload.agentEndPoint}${CommonConstants.URL_AGENT_WRITE_DID}`;
const { seed } = payload;
const { apiKey } = payload;
const writeDid = 'write-did';
const agentDid = await this._retryAgentSpinup(agentDidWriteUrl, apiKey, writeDid, seed);
if (agentDid) {

const getDidMethodUrl = `${payload.agentEndPoint}${CommonConstants.URL_AGENT_GET_DIDS}`;
const getDidDic = 'get-did-doc';
const getDidMethod = await this._retryAgentSpinup(getDidMethodUrl, apiKey, getDidDic);


const storeOrgAgentData: IStoreOrgAgentDetails = {
did: getDidMethod[0]?.did,
verkey: getDidMethod[0]?.didDocument?.verificationMethod[0]?.publicKeyBase58,
isDidPublic: true,
agentSpinUpStatus: 2,
walletName: payload.walletName,
agentsTypeId: AgentType.AFJ,
orgId: payload.orgId,
agentEndPoint: payload.agentEndPoint,
agentId: payload.agentId,
orgAgentTypeId: OrgAgentType.DEDICATED
};


const storeAgentDid = await this.agentServiceRepository.storeOrgAgentDetails(storeOrgAgentData);
return storeAgentDid;
const storeAgentDid = await this.agentServiceRepository.storeOrgAgentDetails(storeOrgAgentData);
return storeAgentDid;

} else {
throw new InternalServerErrorException('DID is not registered on the ledger');
}
} else {
throw new InternalServerErrorException('DID is not registered on the ledger');
}


} catch (error) {
Expand All @@ -370,6 +371,30 @@ export class AgentServiceService {
}
}

async _retryAgentSpinup(agentUrl: string, apiKey: string, agentApiState: string, seed?: string): Promise<object> {
return retry(
async () => {

if ('write-did' === agentApiState) {

const agentDid = await this.commonService
.httpPost(agentUrl, { seed }, { headers: { 'x-api-key': apiKey } })
.then(async response => response);
return agentDid;
} else if ('get-did-doc' === agentApiState) {

const getDidMethod = await this.commonService
.httpGet(agentUrl, { headers: { 'x-api-key': apiKey } })
.then(async response => response);
return getDidMethod;
}
},
{
retries: 5
}
);
}

async _createLegacyConnectionInvitation(orgId: number, user: IUserRequestInterface, label: string): Promise<{
response;
}> {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
"@nestjs/websockets": "^10.1.3",
"@prisma/client": "^5.1.1",
"@sendgrid/mail": "^7.7.0",
"@types/async-retry": "^1.4.5",
"@types/crypto-js": "^4.1.1",
"@types/pdfkit": "^0.12.6",
"async-retry": "^1.3.3",
"auth0-js": "^9.22.1",
"bcrypt": "^5.1.0",
"blob-stream": "^0.1.3",
Expand Down