Exemple #1
0
        /// <summary>
        /// Register the elasticsearch index template if the provided options mandate it.
        /// </summary>
        public void RegisterTemplateIfNeeded()
        {
            if (!_registerTemplateOnStartup)
            {
                return;
            }

            try
            {
                if (!_options.OverwriteTemplate)
                {
                    var templateExistsResponse = _client.IndicesExistsTemplateForAll <DynamicResponse>(_templateName);
                    if (templateExistsResponse.HttpStatusCode == 200)
                    {
                        TemplateRegistrationSuccess = true;

                        return;
                    }
                }

                var result = _client.IndicesPutTemplateForAll <DynamicResponse>(_templateName, GetTempatePostData());

                if (!result.Success)
                {
                    ((IElasticsearchResponse)result).TryGetServerErrorReason(out var serverError);
                    SelfLog.WriteLine("Unable to create the template. {0}", serverError);

                    if (_options.RegisterTemplateFailure == RegisterTemplateRecovery.FailSink)
                    {
                        throw new Exception($"Unable to create the template named {_templateName}.", result.OriginalException);
                    }

                    TemplateRegistrationSuccess = false;
                }
                else
                {
                    TemplateRegistrationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                TemplateRegistrationSuccess = false;

                SelfLog.WriteLine("Failed to create the template. {0}", ex);

                if (_options.RegisterTemplateFailure == RegisterTemplateRecovery.FailSink)
                {
                    throw;
                }
            }
        }