| 1 | package Service::Engine::Data::Elasticsearch; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | use Service::Engine; | ||
| 9 | use Search::Elasticsearch; | ||
| 10 | |||
| 11 | our $Config; | ||
| 12 | our $Log; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | |||
| 16 | my ($class,$options) = @_; | ||
| 17 | |||
| 18 | # set some defaults | ||
| 19 | my $attributes = {'handle'=>''}; | ||
| 20 | |||
| 21 | # load options | ||
| 22 | if (ref($options) eq 'HASH') { | ||
| 23 | foreach my $option (keys %{$options}) { | ||
| 24 | $attributes->{$option} = $options->{$option}; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | # pull in some Service::Engine globals | ||
| 29 | $Config = $Service::Engine::Config; | ||
| 30 | $Log = $Service::Engine::Log; | ||
| 31 | |||
| 32 | $attributes->{'cxn_pool'} ||= 'Static'; | ||
| 33 | $attributes->{'trace_to'} ||= undef; | ||
| 34 | |||
| 35 | if (!$attributes->{'nodes'}) { | ||
| 36 | $Log->log({msg=>"Elasticsearch requires nodes",level=>1}); | ||
| 37 | } else { | ||
| 38 | my $handle = eval{Search::Elasticsearch->new('nodes'=>$attributes->{'nodes'}, 'cxn_pool'=>$attributes->{'cxn_pool'}, 'trace_to' => $attributes->{'trace_to'} );}; | ||
| 39 | |||
| 40 | if ($@) { | ||
| 41 | $Log->log({msg=>"error connecting to Elasticsearch: $@",level=>1}); | ||
| 42 | } else { | ||
| 43 | $attributes->{'handle'} = $handle; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my $self = bless $attributes, $class; | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | sub handle { | ||
| 54 | my ($self) = @_; | ||
| 55 | return $self->{'handle'}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub reconnect { | ||
| 59 | my ($self) = @_; | ||
| 60 | my $handle = eval{Search::Elasticsearch->new('nodes'=>$self->{'nodes'}, 'cxn_pool'=>$self->{'cxn_pool'}, 'trace_to' => $self->{'trace_to'} );}; | ||
| 61 | if ($@) { | ||
| 62 | $Log->log({msg=>"error re-connecting to Elasticsearch: $@",level=>1}); | ||
| 63 | } else { | ||
| 64 | $self->{'handle'} = $handle; | ||
| 65 | } | ||
| 66 | return $self->{'handle'}; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; |