consul-template daemon dead when a template rendering failure happens

Created on 22 Jan 2017  路  1Comment  路  Source: hashicorp/consul-template

I am running consul-template as a daemon and registering/adding template dynamically.
When I add a wrong/"bad" template into the configure file or insert wrong consul data into consul, the consul-template daemon will die. How can I keep the daemon running forever to render the "OK" templates even one/more "bad" templates rendering failure happens?
Since the consul data is changed dynamically, it is hard to write a robust enough template to prevent rendering failure happens if some data sensitive function is used, like explode. It is possible to introduce some try/catch mechanism into consul-template?

Most helpful comment

Hi @mikezh15

This is the expected behavior; otherwise, there would be no indication to a parent process that the daemon is no longer rendering valid configuration. For example, how would you know you put bad data in Consul if CT didn't exit and cascade failure?

It's almost always possible to write a verbose template that handles all edge cases - you just need to identify them all first 馃槃 . Go's templating language evaluates empty arrays, empty strings, and nil as "false", so you can do things like:

{{ if service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ with service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ with $s := service "foo" }}
# I execute if there is a service named foo, and "$s" refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ range service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}

If you have a more specific template that you're trying to harden against errors, please open a new issue with that template and we'll do our best to help out. Alternatively you could ask the community on the Consul Mailing list. I would also recommend taking a quick read over the Golang text/template documentation.

Thanks and have a great day! 馃槃

>All comments

Hi @mikezh15

This is the expected behavior; otherwise, there would be no indication to a parent process that the daemon is no longer rendering valid configuration. For example, how would you know you put bad data in Consul if CT didn't exit and cascade failure?

It's almost always possible to write a verbose template that handles all edge cases - you just need to identify them all first 馃槃 . Go's templating language evaluates empty arrays, empty strings, and nil as "false", so you can do things like:

{{ if service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ with service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ with $s := service "foo" }}
# I execute if there is a service named foo, and "$s" refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}
{{ range service "foo" }}
# I execute if there is a service named foo, and "." refers to the service
{{ else }}
# I execute if there is no service named foo
{{ end }}

If you have a more specific template that you're trying to harden against errors, please open a new issue with that template and we'll do our best to help out. Alternatively you could ask the community on the Consul Mailing list. I would also recommend taking a quick read over the Golang text/template documentation.

Thanks and have a great day! 馃槃

Was this page helpful?
0 / 5 - 0 ratings