Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to skip the wrong json format data,but not exit #1165

Closed
TwitchChen opened this issue Jan 2, 2019 · 3 comments
Closed

how to skip the wrong json format data,but not exit #1165

TwitchChen opened this issue Jan 2, 2019 · 3 comments

Comments

@TwitchChen
Copy link

Please note that the Consul Template issue tracker is reserved
for bug reports and enhancements. For general usage questions,
please use the Consul mailing list:

https://groups.google.com/forum/#!forum/consul-tool

Consul Template version

consul-template v0.19.0 (33b34b3)

Configuration

consul{
    address = "127.0.0.1:8500"
}

log_level = "warn"
pid_file  = "/pacloud/cdm/base/2.0/logs/consul-template.pid"


template{
    source      = "/pacloud/cdm/base/2.0/conf/db-specific/nginx.tmpl"
    destination = "/pacloud/cdm/base/2.0/conf/db-specific/ng.conf.tmp"
    command     = "/pacloud/cdm/base/2.0/conf/db-specific/ng.sh"
    command_timeout = "60s"
}
# Copy-paste your Consul Template template here
{{ range tree "db/nginx" }}{{ $ng_service := .Key }}{{ with $d := .Value | parseJSON }}{{ $network_type := $d.network_type }}{{ $ng_site := $d.ng_site }}{{ range $listeners := $d.listeners }}{{ $instance_name := $listeners.Instance_name}}{{ $vip := $listeners.CIDR}}{{ $listener_port := $listeners.listener_port }}{{ $cpu := $listeners.limit_cpu }}{{ range $rss := $d.rss }}{{ if $rss.enable | parseBool }}{{ $ip := $rss.ip }}{{ $port := $rss.port }}{{ with $hosts := $listeners.hosts }}
{{ $ng_service }}:{{ $hosts.master  }}:{{ $hosts.slave }}:{{ $network_type }}:{{ $ng_site }}:{{ $instance_name }}:{{ $vip }}:{{ $listener_port }}:{{  $cpu }}:{{ $ip }}:{{ $port }}{{ end }}{{ end }}{{ end }}{{ end }}{{ end }}
{{ end }}

Command

# Place your Consul Template command here
the command    always exit with 0

Debug output

2018/12/19 09:32:58.234332 [ERR] (cli) /pacloud/cdm/base/2.0/conf/db-specific/nginx.tmpl: execute: template: :1:75: executing "" at : error calling parseJSON: invalid character 's' after object key:value pair

Expected behavior

When there is a wrong json format data, the consul-template should skip this data instead of exiting.
And the other json data can still be processed。
What should I do to avoid consul-template launches when this happens?

Actual behavior

the consul-template dead

@pierresouchay
Copy link

This is not possible AFAIK, consul-templaterb does support this if it is a frequent issue on your side, you might use this for your use-case (see get_value_json in https://github.com/criteo/consul-templaterb/blob/master/TemplateAPI.md#get-the-result-of-a-single-value) ... there are many examples on how doing this in https://github.com/criteo/consul-templaterb/tree/master/samples

@TwitchChen
Copy link
Author

Ok,i see,thanks.

@niko
Copy link

niko commented Sep 23, 2019

I just ran into the same issue, namely: consul-template is crashing if the template contains a parseJSON function and the json data is corrupt. I could totally live with consul-template crashing on startup on corrupt json. But IMHO an already running instance should not crash, if one of the keys in consul is changed from correct to corrupt json. I think instead it should log the error and not rerender anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants