cesanta / mongoose-wizard

2 stars 1 forks source link

Handle delayed reboot #17

Closed cpq closed 5 months ago

cpq commented 5 months ago

Timer callbacks If I want a glue handler to set a timed callback (eg. to handle a restart command with a delay to allow the requests to be finished) I can't use mg_timer_add() as this also requires the mg_mgr.

@dvosully is this the only use case that requires delay, or are there any others?

dvosully commented 5 months ago

I also use delay after setting some configurations, where I want to write a record of a parameter set (eg. calibration information) which includes multiple (approx 30) separate variables. I set a delay after one of the calibration values gets set, and after the delay I write the calibration adjustment record (trusting that setting the other values has completed in this time). If I do it once for every value that gets set I will write many records, and if I do it in only one variable callback without a delay callback the order is unknown, and some values may not have been set yet.

cpq commented 5 months ago

I guess the right way is just to document the API for delayed execution

@dvosully what do you think would be a good API for that? How about this ?

void glue_delayed_call(void (*func)(void *), void *param, unsigned long milliseconds);

// Example
glue_call_delayed_call((void (*)(void *)) reboot, NULL, 500);
dvosully commented 5 months ago

That would cover all my use cases, I think that makes sense.

cpq commented 5 months ago

The other alternative is to expose the struct mg_mgr g_mgr variable from the mongoose_impl.c file, and use mg_timer_add()

mg_timer_add(&g_mgr, 500, 0,  my_func, NULL);  // Run my_func after 500 ms delay
dvosully commented 5 months ago

I don't have a strong opinion there. Either would cover my use cases. Moving g_mgr to global scope would also allow the user to add their own handlers and custom protocols (in my case a POST upload) without needing to modify mongoose_impl.c which would be convenient. It feels like a small change which puts a fair bit of power back in the hands of the programmer (if they choose to use it) while also not overwhelming the novice, which feels nicely balanced to me... Either way I trust your judgement.

cpq commented 5 months ago

@dvosully exported g_mgr, added docs snippet https://mongoose.ws/documentation/#execute-functions-after-a-delay

Please test & feedback

dvosully commented 5 months ago

Thanks Sergey, tested and working for me.