Skip to content

Commit

Permalink
added some error handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
bergm committed Jun 11, 2024
1 parent 2c97f25 commit c96064e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/run/capnp-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,17 @@ kj::Promise<DataAccessor> monica::dataAccessorFromTimeSeries(mas::schema::climat
Tools::Date(sd.getDay(), sd.getMonth(), sd.getYear()),
Tools::Date(ed.getDay(), ed.getMonth(), ed.getYear()),
headerResponse.getHeader(), dataTResponse.getData());
}, [](auto &&e) {
KJ_LOG(ERROR, "Error while trying to get transposed time series data.", e);
return DataAccessor();
});
}, [](auto &&e) {
KJ_LOG(ERROR, "Error while trying to get header data.", e);
return DataAccessor();
});
}, [](auto &&e) {
KJ_LOG(ERROR, "Error while trying to get range data.", e);
return DataAccessor();
});
}

Expand Down Expand Up @@ -175,6 +184,9 @@ kj::Promise<J11Array> monica::fromCapnpSoilProfile(mas::schema::soil::Profile::C
}
ls.emplace_back(l);
}
return ls;
return kj::mv(ls);
}, [](auto &&e) {
KJ_LOG(ERROR, "Error while trying to get soil profile data.", e);
return J11Array();
});
}
31 changes: 24 additions & 7 deletions src/run/monica-capnp-fbp-component-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class FBPMain {
return true;
}

kj::MainBuilder::Validity setMonicaSr(kj::StringPtr name) {
monicaSr = kj::str(name);
return true;
}


kj::MainBuilder::Validity startComponent() {
bool startedServerInDebugMode = false;

Expand All @@ -81,26 +87,32 @@ class FBPMain {
//std::cout << "outSr: " << outSr.cStr() << std::endl;
auto inp = conMan.tryConnectB(inSr.cStr()).castAs<Channel::ChanReader>();
auto outp = conMan.tryConnectB(outSr.cStr()).castAs<Channel::ChanWriter>();
//auto runMonicaClient = conMan.tryConnectB("capnp://insecure@10.10.24.218:9999/monica_sr").castAs<MonicaEnvInstance>();

auto runMonica = kj::heap<RunMonica>(startedServerInDebugMode);
MonicaEnvInstance::Client runMonicaClient = kj::mv(runMonica);

MonicaEnvInstance::Client runMonicaClient(nullptr);
if (monicaSr.size() > 0) {
runMonicaClient = conMan.tryConnectB(monicaSr.cStr()).castAs<MonicaEnvInstance>();
} else {
runMonicaClient = kj::heap<RunMonica>(startedServerInDebugMode);
}
try {
while (true) {
auto msg = inp.readRequest().send().wait(ioContext.waitScope);
KJ_LOG(INFO, "received msg");
// check for end of data from in port
if (msg.isDone()) {
//cout << "monica-capnp-fbp-component-main: received done message" << endl;
KJ_LOG(INFO, "received done -> exiting main loop");
break;
} else {
auto inIp = msg.getValue();
auto attr = mas::infrastructure::common::getIPAttr(inIp, fromAttr);
auto env = attr.orDefault(inIp.getContent()).getAs<Env>();
KJ_LOG(INFO, "received env -> running MONICA");
auto rreq = runMonicaClient.runRequest();
rreq.setEnv(env);
auto res = rreq.send().wait(ioContext.waitScope);
KJ_LOG(INFO, "received MONICA result");
if (res.hasResult() && res.getResult().hasValue()) {
KJ_LOG(INFO, "result is not empty");
auto resJsonStr = res.getResult().getValue();
auto wreq = outp.writeRequest();
auto outIp = wreq.initValue();
Expand All @@ -111,17 +123,19 @@ class FBPMain {
auto toAttrBuilder = mas::infrastructure::common::copyAndSetIPAttrs(inIp, outIp,
toAttr);//, capnp::toAny(resJsonStr));
KJ_IF_MAYBE(builder, toAttrBuilder) builder->setAs<capnp::Text>(resJsonStr);

KJ_LOG(INFO, "sending result on out port");
wreq.send().wait(ioContext.waitScope);
KJ_LOG(INFO, "sent result on out port");
}
}

}

KJ_LOG(INFO, "closing out port");
outp.closeRequest().send().wait(ioContext.waitScope);
//cout << "monica-capnp-fbp-component-main: closed result out port" << endl;
}
catch (const kj::Exception &e) {
KJ_LOG(INFO, "Exception: ", e.getDescription());
std::cerr << "Exception: " << e.getDescription().cStr() << endl;
//std::cout << "Exception: " << e.getDescription().cStr() << endl;
}
Expand All @@ -141,6 +155,8 @@ class FBPMain {
"<sturdy_ref>", "Sturdy ref to input channel.")
.addOptionWithArg({'o', "result_out_sr"}, KJ_BIND_METHOD(*this, setOutSr),
"<sturdy_ref>", "Sturdy ref to output channel.")
.addOptionWithArg({'m', "monica_sr"}, KJ_BIND_METHOD(*this, setMonicaSr),
"<sturdy_ref>", "Sturdy ref to MONICA instance.")
.callAfterParsing(KJ_BIND_METHOD(*this, startComponent))
.build();
}
Expand All @@ -152,6 +168,7 @@ class FBPMain {
kj::ProcessContext &context;
kj::String inSr;
kj::String outSr;
kj::String monicaSr;
kj::String fromAttr;
kj::String toAttr;
};
Expand Down
16 changes: 14 additions & 2 deletions src/run/run-monica-capnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,21 @@ kj::Promise<void> RunMonica::run(RunContext context)

if (envR.hasTimeSeries()) {
auto ts = envR.getTimeSeries();
proms.add(dataAccessorFromTimeSeries(ts).then([&da](const DataAccessor &da2) { da = da2; }));
proms.add(dataAccessorFromTimeSeries(ts).then([&da](const DataAccessor &da2) {
da = da2;
}, [](auto &&e) {
KJ_LOG(INFO, "Error while trying to get data accessor from time series: ", e);
}));
} else {
proms.add(kj::READY_NOW);
}

if (envR.hasSoilProfile()) {
auto layersProm = fromCapnpSoilProfile(envR.getSoilProfile());
proms.add(layersProm.then([this](auto &&layers) {
proms.add(layersProm.then([this](auto &&layers) mutable {
_soilLayers = layers;
}, [](auto &&e) {
KJ_LOG(INFO, "Error while trying to get soil layers: ", e);
}));
} else {
proms.add(kj::READY_NOW);
Expand All @@ -136,6 +142,12 @@ kj::Promise<void> RunMonica::run(RunContext context)
auto res = rs.initResult();
res.initStructure().setJson();
res.setValue(out.toString());
}, [context](auto &&e) mutable {
KJ_LOG(INFO, "Error while trying to gather soil and/or time series data: ", e);
auto rs = context.getResults();
auto res = rs.initResult();
res.initStructure().setNone();
res.setValue(kj::str("Error while trying to gather soil and/or time series data: ", e));
});
}

Expand Down

0 comments on commit c96064e

Please sign in to comment.