| 1 | package Zing::Zang::Worker; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Worker'; | ||
| 15 | |||
| 16 | our $VERSION = '0.11'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has 'on_handle' => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Maybe[CodeRef]', | ||
| 23 | opt => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'on_perform' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Maybe[CodeRef]', | ||
| 29 | opt => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | has 'on_receive' => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Maybe[CodeRef]', | ||
| 35 | opt => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'queues' => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => 'ArrayRef[Str]', | ||
| 41 | req => 1, | ||
| 42 | ); | ||
| 43 | |||
| 44 | # METHODS | ||
| 45 | |||
| 46 | method handle(@args) { | ||
| 47 | return $self if !$self->on_handle; | ||
| 48 | |||
| 49 | $self->on_handle->($self, @args); | ||
| 50 | |||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | method receive(@args) { | ||
| 55 | return $self if !$self->on_receive; | ||
| 56 | |||
| 57 | $self->on_receive->($self, @args); | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | method perform(@args) { | ||
| 63 | return $self if !$self->on_perform; | ||
| 64 | |||
| 65 | $self->on_perform->($self, @args); | ||
| 66 | |||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | =encoding utf8 | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | Zing::Zang::Worker - Worker Process | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | =head1 ABSTRACT | ||
| 80 | |||
| 81 | Worker Process Implementation | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | =head1 SYNOPSIS | ||
| 86 | |||
| 87 | use Zing::Zang::Worker; | ||
| 88 | |||
| 89 | my $zang = Zing::Zang::Worker->new( | ||
| 90 | on_handle => sub { | ||
| 91 | my ($self, $name, $data) = @_; | ||
| 92 | |||
| 93 | $self->{handled} = [$name, $data]; | ||
| 94 | }, | ||
| 95 | queues => ['tasks'] | ||
| 96 | ); | ||
| 97 | |||
| 98 | # $zang->execute; | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | =head1 DESCRIPTION | ||
| 103 | |||
| 104 | This package provides a L<Zing::Worker> which uses callbacks and doesn't need | ||
| 105 | to be subclassd. It supports providing a process C<perform> method as | ||
| 106 | C<on_perform> and a C<receive> method as C<on_receive> which operate as | ||
| 107 | expected, and also a C<handle> method as C<on_handle> which is executed | ||
| 108 | whenever a message is received from one of the queue(s). | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | =head1 INHERITS | ||
| 113 | |||
| 114 | This package inherits behaviors from: | ||
| 115 | |||
| 116 | L<Zing::Worker> | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | =head1 LIBRARIES | ||
| 121 | |||
| 122 | This package uses type constraints from: | ||
| 123 | |||
| 124 | L<Zing::Types> | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | =head1 ATTRIBUTES | ||
| 129 | |||
| 130 | This package has the following attributes: | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | =head2 on_handle | ||
| 135 | |||
| 136 | on_handle(Maybe[CodeRef]) | ||
| 137 | |||
| 138 | This attribute is read-only, accepts C<(Maybe[CodeRef])> values, and is optional. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | =head2 on_perform | ||
| 143 | |||
| 144 | on_perform(Maybe[CodeRef]) | ||
| 145 | |||
| 146 | This attribute is read-only, accepts C<(Maybe[CodeRef])> values, and is optional. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | =head2 on_receive | ||
| 151 | |||
| 152 | on_receive(Maybe[CodeRef]) | ||
| 153 | |||
| 154 | This attribute is read-only, accepts C<(Maybe[CodeRef])> values, and is optional. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | =head2 queues | ||
| 159 | |||
| 160 | queues(ArrayRef[Str]) | ||
| 161 | |||
| 162 | This attribute is read-only, accepts C<(ArrayRef[Str])> values, and is required. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | =head1 AUTHOR | ||
| 167 | |||
| 168 | Al Newkirk, C<awncorp@cpan.org> | ||
| 169 | |||
| 170 | =head1 LICENSE | ||
| 171 | |||
| 172 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 173 | |||
| 174 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 175 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 176 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 177 | |||
| 178 | =head1 PROJECT | ||
| 179 | |||
| 180 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 181 | |||
| 182 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 183 | |||
| 184 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 185 | |||
| 186 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 187 | |||
| 188 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 189 | |||
| 190 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 191 | |||
| 192 | =cut |