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 serviceHost = environment[2];
let providerClient = new FluencePeer(); let providerClient = new FluencePeer();
await providerClient.start({ connectTo: providerHost }); try {
console.log('📘 uploading .wasm to node %s', providerHost.multiaddr); await providerClient.start({ connectTo: providerHost });
let path = globSource('../service/artifacts/process_files.wasm'); console.log('📘 uploading .wasm to node %s', providerHost.multiaddr);
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient); let path = globSource('../service/artifacts/process_files.wasm');
console.log('📗 swarmAddr', swarmAddr); let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
console.log('📗 rpcAddr', rpcAddr); console.log('📗 swarmAddr', swarmAddr);
console.log('📗 rpcAddr', rpcAddr);
await Fluence.start({ connectTo: relay }); await Fluence.start({ connectTo: relay });
console.log( console.log(
'📗 created a fluence client %s with relay %s', '📗 created a fluence client %s with relay %s',
Fluence.getStatus().peerId, Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId, Fluence.getStatus().relayPeerId,
); );
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node // default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(serviceHost.peerId, 10); await set_timeout(serviceHost.peerId, 10);
console.log('\n\n📘 Will deploy ProcessFiles service'); console.log('\n\n📘 Will deploy ProcessFiles service');
var service_id = await deploy_service( var service_id = await deploy_service(
serviceHost.peerId, serviceHost.peerId,
file.cid.toString(), file.cid.toString(),
rpcAddr, rpcAddr,
(label, error) => { (label, error) => {
console.error('📕 deploy_service failed: ', label, error); console.error('📕 deploy_service failed: ', label, error);
}, },
{ ttl: 10000 }, { ttl: 10000 },
); );
service_id = fromOption(service_id); service_id = fromOption(service_id);
if (service_id === null) { 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 Fluence.stop();
await providerClient.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 { function fromOption<T>(opt: T | T[] | null): T | null {

View File

@ -89,40 +89,42 @@ export async function main() {
// console.log("hello"); // console.log("hello");
// setLogLevel('DEBUG'); // setLogLevel('DEBUG');
await Fluence.start({ connectTo: krasnodar[2] }); try {
console.log( await Fluence.start({ connectTo: krasnodar[2] });
'created a fluence client %s with relay %s', console.log(
Fluence.getStatus().peerId, 'created a fluence client %s with relay %s',
Fluence.getStatus().relayPeerId, 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) { for (let item of echo_result) {
result += item.echo + ','; 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: // Uncomment to enable debug logs:
// setLogLevel('DEBUG'); // 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 // call the get_price function -- sequential processing
await Fluence.start({ connectTo: krasnodar[5] }); const network_result = await get_price(
console.log( 'ethereum',
'Created a fluence client with peer id %s and relay id %s', 'usd',
Fluence.getStatus().peerId, getter_topo[1].node,
Fluence.getStatus().relayPeerId, getter_topo[1].service_id,
); mean_topo[1].service_id,
);
console.log('seq result: ', network_result);
// call the get_price function -- sequential processing // call the get_price_par function -- parallel processing
const network_result = await get_price( // func get_price_par(coin: string, currency: string, getter_topo: []NodeServicePair, mean_topo: NodeServicePair) -> Result:
'ethereum', const network_result_par = await get_price_par('ethereum', 'usd', getter_topo, mean_topo[0]);
'usd', console.log('par result: ', network_result_par);
getter_topo[1].node, } finally {
getter_topo[1].service_id, await Fluence.stop();
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();
} }