You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to execute something just before the instance is terminated? #205
I need to execute a process whenever a new instance of my application is created, and another whenever that instance dies. Since each instance has an envoy sidecar, I thought about adding the first logic to on_vm_start, but I couldn't find a way to add an on_delete or something that is executed before the instance is finalized.
I tried implementing it through the RootContext, but I couldn't find anything that worked. Example:
impl RootContext for MyService {
fn on_vm_start(&mut self, _: usize) -> bool {
info!("Starting the VM {}", self.id);
true
}
// not available
fn on_delete(&mut self, _: u32) {
info!("Terminating the VM {}", self.id);
}
}
I had tried with on_log, but it was never executed. I implemented on_done as you suggested and it seems to be working as expected.
However, during testing, I found something strange and I have a question. It seems that 2 plugins are started, or the same one was started 2 times. At the start of the plugin, I create a UUID and assign it to it. So in the log, it shows that 2 UUIDs are starting, but after that, only 1 continues to execute.
Have you ever encountered something like this?
I will leave the code I used for the example and the envoy file.
Plugin Wasm
use log::{error, info};
use uuid::Uuid;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
use std::time::Duration;
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
Box::new(Service{
id: Uuid::new_v4(),
queue_id: 0,
})
});
}}
struct Service {
id: Uuid,
queue_id: u32,
}
impl Context for Service {
fn on_http_call_response(&mut self, _token_id: u32, _num_headers: usize, _body_size: usize, _num_trailers: usize) {
if let Some(resp) = self.get_http_call_response_body(0, _body_size) {
info!("response {} in VM id {}", String::from_utf8(resp).unwrap(), self.id);
} else {
error!("invalid response from api");
return;
}
}
fn on_done(&mut self) -> bool {
info!("on_done in VM id {}", self.id);
true
}
}
impl RootContext for Service {
fn on_vm_start(&mut self, _: usize) -> bool {
info!("starting VM id {}", self.id);
let queue_id = self.register_shared_queue("queue_test");
self.queue_id = queue_id;
self.set_tick_period(Duration::new(15, 0));
true
}
fn on_queue_ready(&mut self, queue_id: u32) {
if let Ok(Some(body)) = self.dequeue_shared_queue(queue_id) {
if let Ok(r) = std::str::from_utf8(body.as_slice()) {
info!("body {} in VM id {}", r, self.id);
} else {
error!("error reading message body in VM id {}", self.id);
}
}
}
fn on_tick(&mut self) {
if let Err(err) = self.enqueue_shared_queue(self.queue_id, Some("teste".as_bytes())) {
error!("failed to send msg. Error: {:?}", err);
}
get_config(self);
match self.set_shared_data("test", Some("test".as_bytes()), None) {
Ok(_) => {}
Err(cause) => {
error!("unexpected status on set data shared: {:?}", cause)
},
}
}
fn on_log(&mut self) {
info!("on_log exec in VM id {}", self.id);
}
}
pub fn get_config(ctx: &dyn Context) {
let headers = vec![
(":method", "GET"),
(":path", "/config"),
(":scheme", "http"),
(":authority", "go-config"),
];
match ctx.dispatch_http_call("my-api", headers, None, vec![], Duration::new(10, 0)) {
Ok(_) => {},
Err(status) => {
error!("error dispatching call to service: {:?}", status)
}
}
}
Is there any way I can differentiate between these two flows in on_vm_start?
Unfortunately, currently it's not (I agree that's not great).
Note that both canary and "regular" plugin are started in the same WasmVM, so you could store some state in global variables within the plugin, but keep in mind that the same WasmVM is also reused across multiple instances of the same plugin with different plugin configurations, so you'd need to account for that.
Activity
PiotrSikora commentedon Aug 22, 2023
Did you try
on_logoron_done?aleksousa commentedon Aug 22, 2023
Good afternoon @PiotrSikora
I had tried with
on_log, but it was never executed. I implementedon_doneas you suggested and it seems to be working as expected.However, during testing, I found something strange and I have a question. It seems that 2 plugins are started, or the same one was started 2 times. At the start of the plugin, I create a UUID and assign it to it. So in the log, it shows that 2 UUIDs are starting, but after that, only 1 continues to execute.
Have you ever encountered something like this?
I will leave the code I used for the example and the envoy file.
Plugin Wasm
Envoy File
Logs
PiotrSikora commentedon Aug 23, 2023
on_vm_startis called by the "canary" instance that's instantiating the plugin to check if the configuration is valid, and it's discarded afterwards.aleksousa commentedon Aug 23, 2023
Is there any way I can differentiate between these two flows in
on_vm_start?balbifm commentedon Aug 23, 2023
Is the
on_configurecalled as well in that process?PiotrSikora commentedon Aug 23, 2023
Unfortunately, currently it's not (I agree that's not great).
Note that both canary and "regular" plugin are started in the same WasmVM, so you could store some state in global variables within the plugin, but keep in mind that the same WasmVM is also reused across multiple instances of the same plugin with different plugin configurations, so you'd need to account for that.
PiotrSikora commentedon Aug 23, 2023
Yes.