File Coverage

blib/lib/Etcd3/Watch.pm
Criterion Covered Total %
statement 33 41 80.4
branch n/a
condition n/a
subroutine 11 13 84.6
pod 2 2 100.0
total 46 56 82.1


line stmt bran cond sub pod time code
1 5     5   27 use utf8;
  5         11  
  5         29  
2             package Etcd3::Watch;
3              
4 5     5   159 use strict;
  5         10  
  5         84  
5 5     5   20 use warnings;
  5         11  
  5         108  
6              
7 5     5   24 use Moo;
  5         10  
  5         23  
8 5     5   1387 use Types::Standard qw(Str Int Bool HashRef ArrayRef);
  5         12  
  5         50  
9 5     5   5005 use MIME::Base64;
  5         11  
  5         249  
10 5     5   27 use Data::Dumper;
  5         14  
  5         195  
11 5     5   24 use JSON;
  5         10  
  5         33  
12              
13             with 'Etcd3::Role::Actions';
14              
15 5     5   636 use namespace::clean;
  5         11  
  5         30  
16              
17             =head1 NAME
18              
19             Etcd3::Range
20              
21             =cut
22              
23             our $VERSION = '0.007';
24              
25             =head1 DESCRIPTION
26              
27             Watch watches for events happening or that have happened. Both input and output are streams;
28             the input stream is for creating and canceling watchers and the output stream sends events.
29             One watch RPC can watch on multiple key ranges, streaming events for several watches at once.
30             The entire event history can be watched starting from the last compaction revision.
31              
32             =head1 ACCESSORS
33              
34             =head2 endpoint
35              
36             =cut
37              
38             has endpoint => (
39             is => 'ro',
40             isa => Str,
41             default => '/watch'
42             );
43              
44             =head2 key
45              
46             key is the first key for the range. If range_end is not given, the request only looks up key.
47             the key is encoded with base64. type bytes
48              
49             =cut
50              
51             has key => (
52             is => 'ro',
53             isa => Str,
54             required => 1,
55             coerce => sub { return encode_base64( $_[0], '' ) }
56             );
57              
58             =head2 range_end
59              
60             range_end is the end of the range [key, range_end) to watch. If range_end is not given, only
61             the key argument is watched. If range_end is equal to '\0', all keys greater than or equal to
62             the key argument are watched.
63              
64             =cut
65              
66             has range_end => (
67             is => 'ro',
68             isa => Str,
69             coerce => sub { return encode_base64( $_[0], '' ) }
70             );
71              
72             =head2 start_revision
73              
74             start_revision is an optional revision to watch from (inclusive). No start_revision is "now".
75             int64
76              
77             =cut
78              
79             has start_revision => (
80             is => 'ro',
81             isa => Int,
82             );
83              
84             =head2 progress_notify
85              
86             progress_notify is set so that the etcd server will periodically send a WatchResponse with no
87             events to the new watcher if there are no recent events. It is useful when clients wish to recover
88             a disconnected watcher starting from a recent known revision. The etcd server may decide how often
89             it will send notifications based on current load.
90              
91             =cut
92              
93             has progress_notify => (
94             is => 'ro',
95             isa => Bool,
96 5     5   2471 coerce => sub { no strict 'refs'; return $_[0] ? JSON::true : JSON::false }
  5         13  
  5         368  
97             );
98              
99             =head2 filters
100              
101             filter out put event. filter out delete event. filters filter the events at server side before it sends back to the watcher.
102             Options:
103             - NOPUT: filter out put event. (default)
104             - NODELETE: filter out delete event.
105              
106             =cut
107              
108             has filters => (
109             is => 'ro',
110             isa => Str,
111             );
112              
113             =head2 prev_key
114              
115             If prev_kv is set, created watcher gets the previous KV before the event happens. If the previous
116             KV is already compacted, nothing will be returned.
117              
118             =cut
119              
120             has prev_key => (
121             is => 'ro',
122             isa => Bool,
123 5     5   25 coerce => sub { no strict 'refs'; return $_[0] ? JSON::true : JSON::false }
  5         9  
  5         675  
124             );
125              
126             =head2 watch_id
127              
128             watch_id is the watcher id to cancel so that no more events are transmitted. This is only used for a
129             cancel request.
130             int64
131              
132             =cut
133              
134             has watch_id => (
135             is => 'ro',
136             isa => Int,
137             );
138              
139             =head1 PUBLIC METHODS
140              
141             =head2 create
142              
143             create watch
144              
145             =cut
146              
147             sub create {
148 0     0 1   my $self = shift;
149 0           $self->{json_args} = '{"create_request": '. $self->json_args . '}';
150 0           $self->request;
151 0           return $self;
152             }
153              
154             =head2 cancel
155              
156             cancel watch
157              
158             =cut
159              
160             sub cancel {
161 0     0 1   my $self = shift;
162 0           $self->{json_args} = '{"cancel_request": '. $self->json_args . '}';
163 0           $self->request;
164 0           return $self;
165             }
166              
167             1;