| 1 | package Zing::Logic::Worker; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Logic'; | ||
| 15 | |||
| 16 | use Zing::Flow; | ||
| 17 | use Zing::Queue; | ||
| 18 | |||
| 19 | our $VERSION = '0.11'; # VERSION | ||
| 20 | |||
| 21 | # ATTRIBUTES | ||
| 22 | |||
| 23 | has 'on_handle' => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'CodeRef', | ||
| 26 | new => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | fun new_on_handle($self) { | ||
| 30 | $self->can('handle_handle_event') | ||
| 31 | } | ||
| 32 | |||
| 33 | has 'queues' => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'ArrayRef[Str]', | ||
| 36 | new => 1 | ||
| 37 | ); | ||
| 38 | |||
| 39 | fun new_queues($self) { | ||
| 40 | $self->process->queues | ||
| 41 | } | ||
| 42 | |||
| 43 | has 'relays' => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => 'HashRef[Queue]', | ||
| 46 | new => 1 | ||
| 47 | ); | ||
| 48 | |||
| 49 | fun new_relays($self) { | ||
| 50 | +{map {$_, Zing::Queue->new(name => $_)} @{$self->queues}} | ||
| 51 | } | ||
| 52 | |||
| 53 | # METHODS | ||
| 54 | |||
| 55 | method flow() { | ||
| 56 | my $step_0 = $self->next::method; | ||
| 57 | |||
| 58 | my ($step_f, $step_l); | ||
| 59 | |||
| 60 | for my $name (@{$self->queues}) { | ||
| 61 | my $label = $name =~ s/\W+/_/gr; | ||
| 62 | my $step_x = Zing::Flow->new( | ||
| 63 | name => "on_handle_${label}", | ||
| 64 | code => fun($step, $loop) { $self->trace('on_handle')->($self, $name) }, | ||
| 65 | ); | ||
| 66 | $step_l->next($step_x) if $step_l; | ||
| 67 | $step_l = $step_x; | ||
| 68 | $step_f = $step_l if !$step_f; | ||
| 69 | } | ||
| 70 | |||
| 71 | $step_0->append($step_f) if $step_f; | ||
| 72 | $step_0 | ||
| 73 | } | ||
| 74 | |||
| 75 | method handle_handle_event($name) { | ||
| 76 | my $process = $self->process; | ||
| 77 | |||
| 78 | return unless $process->can('handle'); | ||
| 79 | |||
| 80 | my $data = $self->relays->{$name}->recv or return; | ||
| 81 | |||
| 82 | $process->handle($name, $data); | ||
| 83 | |||
| 84 | return $data; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 | =encoding utf8 | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | Zing::Logic::Worker - Worker Logic | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | =head1 ABSTRACT | ||
| 98 | |||
| 99 | Worker Process Logic Chain | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | =head1 SYNOPSIS | ||
| 104 | |||
| 105 | package Process; | ||
| 106 | |||
| 107 | use parent 'Zing::Process'; | ||
| 108 | |||
| 109 | sub queues { | ||
| 110 | ['tasks'] | ||
| 111 | } | ||
| 112 | |||
| 113 | package main; | ||
| 114 | |||
| 115 | use Zing::Logic::Worker; | ||
| 116 | |||
| 117 | my $logic = Zing::Logic::Worker->new(process => Process->new); | ||
| 118 | |||
| 119 | # $logic->execute; | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | =head1 DESCRIPTION | ||
| 124 | |||
| 125 | This package provides the logic (or logic chain) to be executed by the worker | ||
| 126 | process event-loop. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | =head1 INHERITS | ||
| 131 | |||
| 132 | This package inherits behaviors from: | ||
| 133 | |||
| 134 | L<Zing::Logic> | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | =head1 LIBRARIES | ||
| 139 | |||
| 140 | This package uses type constraints from: | ||
| 141 | |||
| 142 | L<Zing::Types> | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | =head1 ATTRIBUTES | ||
| 147 | |||
| 148 | This package has the following attributes: | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | =head2 interupt | ||
| 153 | |||
| 154 | interupt(Interupt) | ||
| 155 | |||
| 156 | This attribute is read-only, accepts C<(Interupt)> values, and is optional. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | =head2 on_handle | ||
| 161 | |||
| 162 | on_handle(CodeRef) | ||
| 163 | |||
| 164 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | =head2 on_perform | ||
| 169 | |||
| 170 | on_perform(CodeRef) | ||
| 171 | |||
| 172 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | =head2 on_receive | ||
| 177 | |||
| 178 | on_receive(CodeRef) | ||
| 179 | |||
| 180 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | =head2 on_register | ||
| 185 | |||
| 186 | on_register(CodeRef) | ||
| 187 | |||
| 188 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | =head2 on_reset | ||
| 193 | |||
| 194 | on_reset(CodeRef) | ||
| 195 | |||
| 196 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | =head2 on_suicide | ||
| 201 | |||
| 202 | on_suicide(CodeRef) | ||
| 203 | |||
| 204 | This attribute is read-only, accepts C<(CodeRef)> values, and is optional. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | =head2 process | ||
| 209 | |||
| 210 | process(Process) | ||
| 211 | |||
| 212 | This attribute is read-only, accepts C<(Process)> values, and is required. | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | =head2 queues | ||
| 217 | |||
| 218 | queues(ArrayRef[Str]) | ||
| 219 | |||
| 220 | This attribute is read-only, accepts C<(ArrayRef[Str])> values, and is optional. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | =head2 relays | ||
| 225 | |||
| 226 | relays(HashRef[Queue]) | ||
| 227 | |||
| 228 | This attribute is read-only, accepts C<(HashRef[Queue])> values, and is optional. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | =head1 METHODS | ||
| 233 | |||
| 234 | This package implements the following methods: | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | =head2 flow | ||
| 239 | |||
| 240 | flow() : Flow | ||
| 241 | |||
| 242 | The flow method builds and returns the logic flow for the process event-loop. | ||
| 243 | |||
| 244 | =over 4 | ||
| 245 | |||
| 246 | =item flow example #1 | ||
| 247 | |||
| 248 | # given: synopsis | ||
| 249 | |||
| 250 | my $flow = $logic->flow; | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | =head2 signals | ||
| 257 | |||
| 258 | signals() : HashRef | ||
| 259 | |||
| 260 | The signals method builds and returns the process signal handlers. | ||
| 261 | |||
| 262 | =over 4 | ||
| 263 | |||
| 264 | =item signals example #1 | ||
| 265 | |||
| 266 | # given: synopsis | ||
| 267 | |||
| 268 | my $signals = $logic->signals; | ||
| 269 | |||
| 270 | =back | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | =head1 AUTHOR | ||
| 275 | |||
| 276 | Al Newkirk, C<awncorp@cpan.org> | ||
| 277 | |||
| 278 | =head1 LICENSE | ||
| 279 | |||
| 280 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 281 | |||
| 282 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 283 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 284 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 285 | |||
| 286 | =head1 PROJECT | ||
| 287 | |||
| 288 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 289 | |||
| 290 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 291 | |||
| 292 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 293 | |||
| 294 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 295 | |||
| 296 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 297 | |||
| 298 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 299 | |||
| 300 | =cut |