| 1 | package Hopkins::Store; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Hopkins::Store - interface to backend storage | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Hopkins::Store encapsulates all of the busywork associated | ||
| 13 | with the DBIx::Class schema creation. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use POE; | ||
| 18 | use POE::Filter::Reference; | ||
| 19 | use POE::Wheel::Run; | ||
| 20 | use Class::Accessor::Fast; | ||
| 21 | |||
| 22 | use Cache::FileCache; | ||
| 23 | use Data::UUID; | ||
| 24 | use Tie::IxHash; | ||
| 25 | |||
| 26 | use Hopkins::Store::Backend; | ||
| 27 | |||
| 28 | use base 'Class::Accessor::Fast'; | ||
| 29 | |||
| 30 | __PACKAGE__->mk_accessors(qw(config cache events backend tries subscribers)); | ||
| 31 | |||
| 32 | use constant HOPKINS_STORE_EVENT_PROC_INTERVAL => 10; | ||
| 33 | |||
| 34 | my $ug = new Data::UUID; | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item init | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new | ||
| 45 | { | ||
| 46 | my $self = shift->SUPER::new(@_); | ||
| 47 | |||
| 48 | return undef if not $self->config->loaded; | ||
| 49 | |||
| 50 | $self->events(new Tie::IxHash); | ||
| 51 | $self->subscribers({}); | ||
| 52 | |||
| 53 | $self->cache(new Cache::FileCache { | ||
| 54 | cache_root => $self->config->fetch('state/root')->stringify, | ||
| 55 | namespace => 'store', | ||
| 56 | directory_umask => 0077 | ||
| 57 | }); | ||
| 58 | |||
| 59 | if (my $events = $self->cache->get('events')) { | ||
| 60 | last if not ref $events eq 'ARRAY'; | ||
| 61 | |||
| 62 | foreach my $href (@$events) { | ||
| 63 | next if not ref $href eq 'HASH'; | ||
| 64 | next if not defined $href->{id}; | ||
| 65 | next if not defined $href->{contents}; | ||
| 66 | |||
| 67 | $self->events->Push($href->{id} => $href->{contents}); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | POE::Session->create | ||
| 72 | ( | ||
| 73 | object_states => | ||
| 74 | [ | ||
| 75 | $self => | ||
| 76 | { | ||
| 77 | _start => 'start', | ||
| 78 | _stop => 'stop', | ||
| 79 | |||
| 80 | init => 'init', | ||
| 81 | notify => 'notify', | ||
| 82 | subscribe => 'subscribe', | ||
| 83 | unsubscribe => 'unsubscribe', | ||
| 84 | proc => 'proc', | ||
| 85 | shutdown => 'shutdown', | ||
| 86 | |||
| 87 | spawn => 'backend_spawn', | ||
| 88 | stdout => 'backend_notify', | ||
| 89 | stderr => 'backend_error', | ||
| 90 | done => 'backend_exited' | ||
| 91 | } | ||
| 92 | ] | ||
| 93 | ); | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | =head1 STATES | ||
| 101 | |||
| 102 | =over 4 | ||
| 103 | |||
| 104 | =item start | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub start | ||
| 109 | { | ||
| 110 | my $self = $_[OBJECT]; | ||
| 111 | my $kernel = $_[KERNEL]; | ||
| 112 | |||
| 113 | $kernel->alias_set('store'); | ||
| 114 | |||
| 115 | $kernel->post(store => 'init'); | ||
| 116 | $kernel->alarm(proc => time + HOPKINS_STORE_EVENT_PROC_INTERVAL); | ||
| 117 | } | ||
| 118 | |||
| 119 | =item stop | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub stop | ||
| 124 | { | ||
| 125 | } | ||
| 126 | |||
| 127 | =item init | ||
| 128 | |||
| 129 | initialize the store. if a backend is currently | ||
| 130 | running, it is destroyed and a new spawn event is | ||
| 131 | generated. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub init | ||
| 136 | { | ||
| 137 | my $self = $_[OBJECT]; | ||
| 138 | my $kernel = $_[KERNEL]; | ||
| 139 | |||
| 140 | $self->backend(undef); | ||
| 141 | $kernel->post(store => 'spawn'); | ||
| 142 | } | ||
| 143 | |||
| 144 | =item shutdown | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub shutdown | ||
| 149 | { | ||
| 150 | my $self = $_[OBJECT]; | ||
| 151 | my $kernel = $_[KERNEL]; | ||
| 152 | |||
| 153 | $self->backend(undef); | ||
| 154 | $kernel->alias_remove('store'); | ||
| 155 | $kernel->alarm('proc'); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub proc | ||
| 159 | { | ||
| 160 | my $self = $_[OBJECT]; | ||
| 161 | my $kernel = $_[KERNEL]; | ||
| 162 | |||
| 163 | $kernel->alarm(proc => time + HOPKINS_STORE_EVENT_PROC_INTERVAL); | ||
| 164 | |||
| 165 | if (not defined $self->backend) { | ||
| 166 | $kernel->post(store => 'spawn'); | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | foreach my $id ($self->events->Keys) { | ||
| 171 | my $event = $self->events->FETCH($id); | ||
| 172 | |||
| 173 | Hopkins->log_debug("sending event $id to backend"); | ||
| 174 | |||
| 175 | $self->backend->put({ event => { id => $id, contents => $event } }); | ||
| 176 | } | ||
| 177 | |||
| 178 | $kernel->alarm(proc => time + HOPKINS_STORE_EVENT_PROC_INTERVAL); | ||
| 179 | } | ||
| 180 | |||
| 181 | sub evtflush | ||
| 182 | { | ||
| 183 | my $self = $_[OBJECT]; | ||
| 184 | my $kernel = $_[KERNEL]; | ||
| 185 | my $heap = $_[HEAP]; | ||
| 186 | |||
| 187 | while ($self->events->count) { | ||
| 188 | my $aref = $self->events->shift; | ||
| 189 | my $res = $kernel->call(store => $aref->[0] => $aref->[1..$#{$aref}]); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | sub subscribe | ||
| 194 | { | ||
| 195 | my $self = $_[OBJECT]; | ||
| 196 | |||
| 197 | $self->subscribers->{$_} = 1 foreach @_[ARG0..$#_]; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub unsubscribe | ||
| 201 | { | ||
| 202 | my $self = $_[OBJECT]; | ||
| 203 | |||
| 204 | delete $self->subscribers->{$_} foreach @_[ARG0..$#_]; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub notify | ||
| 208 | { | ||
| 209 | my $self = $_[OBJECT]; | ||
| 210 | my $kernel = $_[KERNEL]; | ||
| 211 | |||
| 212 | my $id = $ug->create_str; | ||
| 213 | |||
| 214 | Hopkins->log_debug("received $_[ARG0] notification; assigned event ID $id"); | ||
| 215 | |||
| 216 | my @args = @_[ARG0..$#_]; | ||
| 217 | |||
| 218 | $self->events->Push($id => \@args); | ||
| 219 | $self->write_state; | ||
| 220 | |||
| 221 | # notify any other interested sessions | ||
| 222 | $kernel->post($_ => notify => @args) foreach keys %{ $self->subscribers }; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub write_state | ||
| 226 | { | ||
| 227 | my $self = shift; | ||
| 228 | |||
| 229 | my @events = map +{ id => $_, contents => $self->events->FETCH($_) }, $self->events->Keys; | ||
| 230 | |||
| 231 | $self->cache->set(events => \@events); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub backend_spawn | ||
| 235 | { | ||
| 236 | my $self = $_[OBJECT]; | ||
| 237 | my $kernel = $_[KERNEL]; | ||
| 238 | |||
| 239 | my %args = | ||
| 240 | ( | ||
| 241 | Program => sub { new Hopkins::Store::Backend { config => $self->config } }, | ||
| 242 | StdoutEvent => 'stdout', | ||
| 243 | StderrEvent => 'stderr', | ||
| 244 | StdioFilter => new POE::Filter::Reference 'Storable' | ||
| 245 | ); | ||
| 246 | |||
| 247 | $kernel->sig(CHLD => 'done'); | ||
| 248 | $self->backend(new POE::Wheel::Run %args); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub backend_notify | ||
| 252 | { | ||
| 253 | my $self = $_[OBJECT]; | ||
| 254 | my $kernel = $_[KERNEL]; | ||
| 255 | my $href = $_[ARG0]; | ||
| 256 | |||
| 257 | if (my $href = $href->{eventproc}) { | ||
| 258 | $self->events->Delete($href->{id}); | ||
| 259 | $self->write_state; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | sub backend_error | ||
| 264 | { | ||
| 265 | my $self = $_[OBJECT]; | ||
| 266 | my $kernel = $_[KERNEL]; | ||
| 267 | my $text = $_[ARG0]; | ||
| 268 | |||
| 269 | Hopkins->log_warn($text); | ||
| 270 | } | ||
| 271 | |||
| 272 | sub backend_exited | ||
| 273 | { | ||
| 274 | my $self = $_[OBJECT]; | ||
| 275 | my $kernel = $_[KERNEL]; | ||
| 276 | my $signal = $_[ARG0]; | ||
| 277 | my $pid = $_[ARG1]; | ||
| 278 | my $status = $_[ARG2]; | ||
| 279 | |||
| 280 | return if $pid != $self->backend->PID; | ||
| 281 | |||
| 282 | Hopkins->log_error('lost database backend'); | ||
| 283 | |||
| 284 | $self->backend(undef); | ||
| 285 | $kernel->sig('CHLD'); | ||
| 286 | } | ||
| 287 | |||
| 288 | =back | ||
| 289 | |||
| 290 | =head1 AUTHOR | ||
| 291 | |||
| 292 | Mike Eldridge <diz@cpan.org> | ||
| 293 | |||
| 294 | =head1 LICENSE | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | 1; | ||
| 299 |