[pbs-devel] [PATCH proxmox-backup 2/3] server/email_notifications: do not panic on template registration

Dominik Csapak d.csapak at proxmox.com
Thu Mar 18 10:57:46 CET 2021


On 3/17/21 20:38, Thomas Lamprecht wrote:
> On 16.03.21 12:56, Dominik Csapak wrote:
>> instead print an error and continue, the rendering functions will error
>> out if one of the templates could not be registered
>>
>> if we `.unwrap()` here, it can lead to problems if the templates are
>> not correct, i.e. we could panic while holding a lock, if something holds
>> a mutex while this is called for the first time
> 
> how can they error?
> And any error (with or without this patch) would lead to emails notification not
> working anymore, some may seem this as quite fatal error if they do not get notified
> on erroneous jobs anymore? We may not be able to do much here, that's why above
> question about what the error source can be.

they can error if they do not compile, e.g. they have syntax errors
while we should catch that during developement/reviewing,
if it does happen, it generates some weird behaviour
(for example panicing while holding a mutex)

with my patch, we still generate a warning, but aside from
sending notification mails (where we still would warn in the log)
the rest should work fine, there we can ofc also error out on
notification errors so that the tasks get an error
(but a well defined one instead of a panic)

also we may want to put the templates into files in the future
so that users can adapt it and we can more easily change
them (maybe localize them?)

> 
>>
>> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
>> ---
>>   src/server/email_notifications.rs | 35 +++++++++++++++++++------------
>>   1 file changed, 22 insertions(+), 13 deletions(-)
>>
>> diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs
>> index 229443f6..59cd016a 100644
>> --- a/src/server/email_notifications.rs
>> +++ b/src/server/email_notifications.rs
>> @@ -1,10 +1,11 @@
>>   use anyhow::Error;
>>   use serde_json::json;
>>   
>> -use handlebars::{Handlebars, Helper, Context, RenderError, RenderContext, Output, HelperResult};
>> +use handlebars::{Handlebars, Helper, Context, RenderError, RenderContext, Output, HelperResult, TemplateError};
>>   
>>   use proxmox::tools::email::sendmail;
>>   use proxmox::api::schema::parse_property_string;
>> +use proxmox::try_block;
>>   
>>   use crate::{
>>       config::datastore::DataStoreConfig,
>> @@ -181,25 +182,33 @@ lazy_static::lazy_static!{
>>   
>>       static ref HANDLEBARS: Handlebars<'static> = {
>>           let mut hb = Handlebars::new();
>> +        let result: Result<(), TemplateError> = try_block!({
>>   
>> -        hb.set_strict_mode(true);
>> +            hb.set_strict_mode(true);
>>   
>> -        hb.register_helper("human-bytes", Box::new(handlebars_humam_bytes_helper));
>> -        hb.register_helper("relative-percentage", Box::new(handlebars_relative_percentage_helper));
>> +            hb.register_helper("human-bytes", Box::new(handlebars_humam_bytes_helper));
>> +            hb.register_helper("relative-percentage", Box::new(handlebars_relative_percentage_helper));
>>   
>> -        hb.register_template_string("gc_ok_template", GC_OK_TEMPLATE).unwrap();
>> -        hb.register_template_string("gc_err_template", GC_ERR_TEMPLATE).unwrap();
>> +            hb.register_template_string("gc_ok_template", GC_OK_TEMPLATE)?;
>> +            hb.register_template_string("gc_err_template", GC_ERR_TEMPLATE)?;
>>   
>> -        hb.register_template_string("verify_ok_template", VERIFY_OK_TEMPLATE).unwrap();
>> -        hb.register_template_string("verify_err_template", VERIFY_ERR_TEMPLATE).unwrap();
>> +            hb.register_template_string("verify_ok_template", VERIFY_OK_TEMPLATE)?;
>> +            hb.register_template_string("verify_err_template", VERIFY_ERR_TEMPLATE)?;
>>   
>> -        hb.register_template_string("sync_ok_template", SYNC_OK_TEMPLATE).unwrap();
>> -        hb.register_template_string("sync_err_template", SYNC_ERR_TEMPLATE).unwrap();
>> +            hb.register_template_string("sync_ok_template", SYNC_OK_TEMPLATE)?;
>> +            hb.register_template_string("sync_err_template", SYNC_ERR_TEMPLATE)?;
>>   
>> -        hb.register_template_string("tape_backup_ok_template", TAPE_BACKUP_OK_TEMPLATE).unwrap();
>> -        hb.register_template_string("tape_backup_err_template", TAPE_BACKUP_ERR_TEMPLATE).unwrap();
>> +            hb.register_template_string("tape_backup_ok_template", TAPE_BACKUP_OK_TEMPLATE)?;
>> +            hb.register_template_string("tape_backup_err_template", TAPE_BACKUP_ERR_TEMPLATE)?;
>>   
>> -        hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE).unwrap();
>> +            hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE)?;
>> +
>> +            Ok(())
>> +        });
>> +
>> +        if let Err(err) = result {
>> +            eprintln!("error during template registration: {}", err);
>> +        }
>>   
>>           hb
>>       };
>>
> 






More information about the pbs-devel mailing list