try-finally everywhere

This commit is contained in:
Pavel Murygin 2022-03-17 18:08:12 +03:00
parent 1e698b85ca
commit ee9f98478e
3 changed files with 113 additions and 108 deletions

View File

@ -29,65 +29,67 @@ export async function main(environment: Node[]) {
let serviceHost = environment[2];
let providerClient = new FluencePeer();
await providerClient.start({ connectTo: providerHost });
console.log('📘 uploading .wasm to node %s', providerHost.multiaddr);
let path = globSource('../service/artifacts/process_files.wasm');
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
console.log('📗 swarmAddr', swarmAddr);
console.log('📗 rpcAddr', rpcAddr);
try {
await providerClient.start({ connectTo: providerHost });
console.log('📘 uploading .wasm to node %s', providerHost.multiaddr);
let path = globSource('../service/artifacts/process_files.wasm');
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
console.log('📗 swarmAddr', swarmAddr);
console.log('📗 rpcAddr', rpcAddr);
await Fluence.start({ connectTo: relay });
console.log(
'📗 created a fluence client %s with relay %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
await Fluence.start({ connectTo: relay });
console.log(
'📗 created a fluence client %s with relay %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(serviceHost.peerId, 10);
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(serviceHost.peerId, 10);
console.log('\n\n📘 Will deploy ProcessFiles service');
var service_id = await deploy_service(
serviceHost.peerId,
file.cid.toString(),
rpcAddr,
(label, error) => {
console.error('📕 deploy_service failed: ', label, error);
},
{ ttl: 10000 },
);
service_id = fromOption(service_id);
if (service_id === null) {
console.log('\n\n📘 Will deploy ProcessFiles service');
var service_id = await deploy_service(
serviceHost.peerId,
file.cid.toString(),
rpcAddr,
(label, error) => {
console.error('📕 deploy_service failed: ', label, error);
},
{ ttl: 10000 },
);
service_id = fromOption(service_id);
if (service_id === null) {
await Fluence.stop();
await providerClient.stop();
return;
}
console.log('📗 ProcessFiles service is now deployed and available as', service_id);
console.log('\n\n📘 Will upload file & calculate its size');
let { file: newFile } = await provideFile(urlSource('https://i.imgur.com/NZgK6DB.png'), providerClient);
var putResult = await put_file_size(
serviceHost.peerId,
newFile.cid.toString(),
rpcAddr,
service_id,
(fileSize) => console.log('📗 Calculated file size:', fileSize),
(label, error) => {
console.error('📕 put_file_size failed: ', label, error);
},
{ ttl: 10000 },
);
putResult = fromOption(putResult);
if (putResult !== null) {
console.log('📗 File size is saved to IPFS:', putResult);
}
let result = await remove_service(serviceHost.peerId, service_id);
console.log('📗 ProcessFiles service removed', result);
} finally {
await Fluence.stop();
await providerClient.stop();
return;
}
console.log('📗 ProcessFiles service is now deployed and available as', service_id);
console.log('\n\n📘 Will upload file & calculate its size');
let { file: newFile } = await provideFile(urlSource('https://i.imgur.com/NZgK6DB.png'), providerClient);
var putResult = await put_file_size(
serviceHost.peerId,
newFile.cid.toString(),
rpcAddr,
service_id,
(fileSize) => console.log('📗 Calculated file size:', fileSize),
(label, error) => {
console.error('📕 put_file_size failed: ', label, error);
},
{ ttl: 10000 },
);
putResult = fromOption(putResult);
if (putResult !== null) {
console.log('📗 File size is saved to IPFS:', putResult);
}
let result = await remove_service(serviceHost.peerId, service_id);
console.log('📗 ProcessFiles service removed', result);
await Fluence.stop();
await providerClient.stop();
}
function fromOption<T>(opt: T | T[] | null): T | null {

View File

@ -89,40 +89,42 @@ export async function main() {
// console.log("hello");
// setLogLevel('DEBUG');
await Fluence.start({ connectTo: krasnodar[2] });
console.log(
'created a fluence client %s with relay %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
try {
await Fluence.start({ connectTo: krasnodar[2] });
console.log(
'created a fluence client %s with relay %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
let echo_result = await echo(names, echo_topos[0].node, echo_topos[0].service_id);
let echo_result = await echo(names, echo_topos[0].node, echo_topos[0].service_id);
let result = '';
let result = '';
for (let item of echo_result) {
result += item.echo + ',';
for (let item of echo_result) {
result += item.echo + ',';
}
console.log('echo result : ', result);
let greeting_result = await greeting(names[0], true, greeting_topos[0].node, greeting_topos[0].service_id);
console.log('greeting result : ', greeting_result);
// echo_greeting_par(greet: bool, echo_service: EchoServiceInput, greeting_services: []NodeServicePair) -> []string:
let seq_result = await echo_greeting_seq(
names,
true,
echo_topos[0].node,
echo_topos[0].service_id,
greeting_topos[0].service_id,
);
console.log('seq result : ', seq_result);
let par_result = await echo_greeting_par(true, echo_service, greeting_services);
console.log('par result : ', par_result);
par_result = await echo_greeting_par_improved(echo_service, greeting_services);
console.log('par improved signature result : ', par_result);
} finally {
await Fluence.stop();
}
console.log('echo result : ', result);
let greeting_result = await greeting(names[0], true, greeting_topos[0].node, greeting_topos[0].service_id);
console.log('greeting result : ', greeting_result);
// echo_greeting_par(greet: bool, echo_service: EchoServiceInput, greeting_services: []NodeServicePair) -> []string:
let seq_result = await echo_greeting_seq(
names,
true,
echo_topos[0].node,
echo_topos[0].service_id,
greeting_topos[0].service_id,
);
console.log('seq result : ', seq_result);
let par_result = await echo_greeting_par(true, echo_service, greeting_services);
console.log('par result : ', par_result);
par_result = await echo_greeting_par_improved(echo_service, greeting_services);
console.log('par improved signature result : ', par_result);
await Fluence.stop();
}

View File

@ -54,29 +54,30 @@ export async function main() {
// Uncomment to enable debug logs:
// setLogLevel('DEBUG');
try {
// create the Fluence client for the Krasnodar testnet
await Fluence.start({ connectTo: krasnodar[5] });
console.log(
'Created a fluence client with peer id %s and relay id %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
// create the Fluence client for the Krasnodar testnet
await Fluence.start({ connectTo: krasnodar[5] });
console.log(
'Created a fluence client with peer id %s and relay id %s',
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId,
);
// call the get_price function -- sequential processing
const network_result = await get_price(
'ethereum',
'usd',
getter_topo[1].node,
getter_topo[1].service_id,
mean_topo[1].service_id,
);
console.log('seq result: ', network_result);
// call the get_price function -- sequential processing
const network_result = await get_price(
'ethereum',
'usd',
getter_topo[1].node,
getter_topo[1].service_id,
mean_topo[1].service_id,
);
console.log('seq result: ', network_result);
// call the get_price_par function -- parallel processing
// func get_price_par(coin: string, currency: string, getter_topo: []NodeServicePair, mean_topo: NodeServicePair) -> Result:
const network_result_par = await get_price_par('ethereum', 'usd', getter_topo, mean_topo[0]);
console.log('par result: ', network_result_par);
await Fluence.stop();
// call the get_price_par function -- parallel processing
// func get_price_par(coin: string, currency: string, getter_topo: []NodeServicePair, mean_topo: NodeServicePair) -> Result:
const network_result_par = await get_price_par('ethereum', 'usd', getter_topo, mean_topo[0]);
console.log('par result: ', network_result_par);
} finally {
await Fluence.stop();
}
}