File Coverage

blib/lib/Dancer2/Plugin/ElasticSearch.pm
Criterion Covered Total %
statement 41 44 93.1
branch 3 6 50.0
condition 2 4 50.0
subroutine 12 12 100.0
pod n/a
total 58 66 87.8


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::ElasticSearch;
2             $Dancer2::Plugin::ElasticSearch::VERSION = '0.003';
3             # ABSTRACT: Dancer2 plugin for obtaining Search::Elasticsearch handles
4              
5 1     1   770592 use strict;
  1         2  
  1         29  
6 1     1   4 use warnings;
  1         3  
  1         25  
7 1     1   27 use 5.012;
  1         8  
8 1     1   4 use Carp;
  1         2  
  1         66  
9 1     1   4 use autodie;
  1         2  
  1         8  
10 1     1   4727 use utf8;
  1         2  
  1         7  
11              
12 1     1   17202 use Search::Elasticsearch;
  1         104794  
  1         48  
13 1     1   16 use Try::Tiny;
  1         3  
  1         108  
14 1     1   1094 use Dancer2::Plugin;
  1         2367  
  1         6  
15              
16             our $handles = {};
17              
18             register 'elastic' => sub {
19 1     1   91511 my ($dsl, $name) = @_;
20 1   50     10 $name //= 'default';
21              
22             # the classic fork/thread-safety mantra
23 1 50       7 my $pid_tid = $$ . ($INC{'threads.pm'} ? '_' . threads->tid : '');
24              
25 1         2 my $elastic;
26 1 50       5 if ($elastic = $handles->{$pid_tid}{$name}) {
27             # got one from the cache. done
28             } else {
29             # no handle in the cache, create one and stash it
30 1         14 my $plugin_config = plugin_setting();
31 1 50       101 unless (exists $plugin_config->{$name}) {
32 0         0 die "No config for ElasticSearch client '$name'";
33             }
34 1         3 my $config = $plugin_config->{$name};
35 1   50     5 my $params = $config->{params} // {};
36             try {
37 1     1   31 $elastic = Search::Elasticsearch->new(%{$params});
  1         11  
38             # S::E does not actually connect until it needs to, but
39             # we're already not creating the S::E object until we need
40             # one!
41 1         548387 $elastic->ping;
42             } catch {
43 1     1   5815 my $error = $_;
44 1         7 die "Could not connect to ElasticSearch: $error";
45 1         8 };
46 0           $handles->{$pid_tid}{$name} = $elastic;
47             }
48              
49 0           return $elastic;
50             };
51              
52             register_plugin;
53              
54             1;
55              
56              
57             =pod
58              
59             =head1 NAME
60              
61             Dancer2::Plugin::ElasticSearch - Dancer2 plugin for obtaining Search::Elasticsearch handles
62              
63             =head1 VERSION
64              
65             version 0.003
66              
67             =head1 SYNOPSIS
68              
69             use Dancer2::Plugin::ElasticSearch;
70             get '/count_all_docs' => sub {
71             return elastic->search(search_type => 'count',
72             body => { query => { match_all => {} } });
73             }
74              
75             =head1 DESCRIPTION
76              
77             This Dancer2 plugin handles connection configuration and
78             fork-and-thread safety for ElasticSearch connectors.
79              
80             =head1 KEYWORDS
81              
82             =head2 elastic
83              
84             elastic->ping;
85             elastic('other')->ping;
86              
87             Return a L subclass suitable for
88             running queries against an ElasticSearch instance. Each thread is
89             guaranteed to have its own client instances. If a connection already
90             exists for a given configuration name, it is returned instead of being
91             re-created.
92              
93             If a configuration name is not passed, "default" is assumed.
94              
95             =head1 CONFIGURATION
96              
97             plugins:
98             ElasticSearch:
99             default:
100             params:
101             nodes: localhost:9200
102             other:
103             params: etc
104              
105             The C hashref must contain a map of parameters that can be
106             passed directly to the L constructor. In the
107             above example, calling C (or C) will
108             result in
109              
110             Search::Elasticsearch->new(nodes => 'localhost:9200');
111              
112             =head1 INSTALLATION
113              
114             You can install this module as you would any Perl module.
115              
116             During installation, the unit tests will run queries against a local
117             ElasticSearch instance if there is one (read-only queries, of
118             course!). If you have no local ElasticSearch instance, but still wish
119             to run the unit tests, set the C variable to
120             "$host:$port". If no instance can be reached, the tests will be
121             safely skipped.
122              
123             =head1 SEE ALSO
124              
125             L, L
126              
127             =head1 AUTHOR
128              
129             Fabrice Gabolde
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2015 by Weborama.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut
139              
140              
141             __END__