Zing.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 33 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing;
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::Kernel';
15  
16 our $VERSION = '0.11'; # VERSION
17  
18 # ATTRIBUTES
19  
20 has 'scheme' => (
21 is => 'ro',
22 isa => 'Scheme',
23 req => 1,
24 );
25  
26 # METHODS
27  
28 method start() {
29 return $self->execute;
30 }
31  
32 1;
33  
34 =encoding utf8
35  
36 =head1 NAME
37  
38 Zing - Multi-Process Management System
39  
40 =cut
41  
42 =head1 ABSTRACT
43  
44 Actor Toolkit and Multi-Process Management System
45  
46 =cut
47  
48 =head1 SYNOPSIS
49  
50 use Zing;
51  
52 my $zing = Zing->new(scheme => ['MyApp', [], 1]);
53  
54 # $zing->execute;
55  
56 =cut
57  
58 =head1 DESCRIPTION
59  
60 This distribution includes an actor-model architecture toolkit and
61 multi-process management system which provides primatives for building
62 resilient, reactive, concurrent, distributed message-driven applications in
63 Perl 5. If you're unfamiliar with this architectural pattern, learn more about
64 L<"the actor model"|https://en.wikipedia.org/wiki/Actor_model>.
65  
66 =cut
67  
68 =head1 INHERITS
69  
70 This package inherits behaviors from:
71  
72 L<Zing::Kernel>
73  
74 =cut
75  
76 =head1 LIBRARIES
77  
78 This package uses type constraints from:
79  
80 L<Zing::Types>
81  
82 =cut
83  
84 =head1 ATTRIBUTES
85  
86 This package has the following attributes:
87  
88 =cut
89  
90 =head2 scheme
91  
92 scheme(Scheme)
93  
94 This attribute is read-only, accepts C<(Scheme)> values, and is required.
95  
96 =cut
97  
98 =head1 METHODS
99  
100 This package implements the following methods:
101  
102 =cut
103  
104 =head2 start
105  
106 start() : Kernel
107  
108 The start method prepares the L<Zing::Kernel> and executes its event-loop.
109  
110 =over 4
111  
112 =item start example #1
113  
114 # given: synopsis
115  
116 $zing->start;
117  
118 =back
119  
120 =cut
121  
122 =head1 COMMANDS
123  
124 Given the following process (actor):
125  
126 # in lib/MyApp.pm
127  
128 package MyApp;
129  
130 use parent 'Zing::Single';
131  
132 sub perform {
133 # do something (once)
134 }
135  
136 1;
137  
138 With an application cartridge specifying 4 forks:
139  
140 # in app/myapp
141  
142 ['MyApp', [], 4]
143  
144 The L<zing> command-line application lets you manage Zing applications from the
145 command-line using these commands:
146  
147 =cut
148  
149 =head2 start
150  
151 $ zing start app/myapp
152  
153 The C<start> command loads an application I<cartridge> which returns a
154 L<"scheme"|Zing::Types/scheme> and runs it as a daemon.
155  
156 =cut
157  
158 =head2 stop
159  
160 $ zing stop app/myapp
161  
162 The C<stop> command finds a running application by its PID (process ID) and
163 terminates the process.
164  
165 =cut
166  
167 =head2 logs
168  
169 $ zing logs --level fatal
170  
171 The C<logs> command taps the centralized log source and outputs new events to
172 STDOUT (standard output).
173  
174 =cut
175  
176 =head1 PRIMATIVES
177  
178 This distribution provides a collection of actor-model primitives which can be
179 used to create sophisticated and powerful distributed systems, organized within
180 intricate process topologies. The following is a directory of those primitives
181 listed by classification:
182  
183 =head2 messaging
184  
185 These classes facilitate message-passing and communications:
186  
187 =over 4
188  
189 =item *
190  
191 L<Zing::Channel>: Shared Communication
192  
193 =item *
194  
195 L<Zing::Data>: Process Data
196  
197 =item *
198  
199 L<Zing::Domain>: Aggregate Root
200  
201 =item *
202  
203 L<Zing::Dropbox>: Transient Store
204  
205 =item *
206  
207 L<Zing::KeyVal>: Key/Value Store
208  
209 =item *
210  
211 L<Zing::Mailbox>: Process Mailbox
212  
213 =item *
214  
215 L<Zing::PubSub>: Pub/Sub Store
216  
217 =item *
218  
219 L<Zing::Queue>: Message Queue
220  
221 =item *
222  
223 L<Zing::Registry>: Process Registry
224  
225 =item *
226  
227 L<Zing::Repo>: Generic Store
228  
229 =item *
230  
231 L<Zing::Store>: Storage Abstraction
232  
233 =back
234  
235 =head2 processes
236  
237 These base classes implement the underlying process (actor) logic:
238  
239 =over 4
240  
241 =item *
242  
243 L<Zing>: Process Wrapper
244  
245 =item *
246  
247 L<Zing::Kernel>: Kernel Process
248  
249 =item *
250  
251 L<Zing::Launcher>: Scheme Launcher
252  
253 =item *
254  
255 L<Zing::Process>: Processing Unit
256  
257 =item *
258  
259 L<Zing::Ring>: Process Ring
260  
261 =item *
262  
263 L<Zing::Scheduler>: Scheme Launcher
264  
265 =item *
266  
267 L<Zing::Simple>: Simple Process
268  
269 =item *
270  
271 L<Zing::Single>: Single Process
272  
273 =item *
274  
275 L<Zing::Spawner>: Scheme Spawner
276  
277 =item *
278  
279 L<Zing::Timer>: Timer Process
280  
281 =item *
282  
283 L<Zing::Watcher>: Watcher Process
284  
285 =item *
286  
287 L<Zing::Worker>: Worker Process
288  
289 =back
290  
291 =head2 ready-made
292  
293 These classes are ready-made process implementations using callbacks:
294  
295 =over 4
296  
297 =item *
298  
299 L<Zing::Zang>: Process Implementation
300  
301 =item *
302  
303 L<Zing::Zang::Launcher>: Process Launcher
304  
305 =item *
306  
307 L<Zing::Zang::Simple>: Process Performer
308  
309 =item *
310  
311 L<Zing::Zang::Single>: Single-Task Process
312  
313 =item *
314  
315 L<Zing::Zang::Spawner>: Process Spawner
316  
317 =item *
318  
319 L<Zing::Zang::Timer>: Timer Process
320  
321 =item *
322  
323 L<Zing::Zang::Watcher>: Process Watcher
324  
325 =item *
326  
327 L<Zing::Zang::Worker>: Worker Process
328  
329 =back
330  
331 =cut
332  
333 =head1 FEATURES
334  
335 All features are implemented using classes and objects. The following is a list
336 of features currently enabled by this toolkit:
337  
338 =cut
339  
340 =head2 actor-model
341  
342 use Zing::Process;
343  
344 my $p1 = Zing::Process->new(name => 'p1');
345 my $p2 = Zing::Process->new(name => 'p2');
346  
347 $p1->mailbox->send($p2->mailbox->term, { action => 'greeting' });
348  
349 This distribution provides a toolkit for creating processes (actors) which can
350 be run in isolation and which communicate with other processes through
351 message-passing.
352  
353 =cut
354  
355 =head2 asynchronous
356  
357 # in process (1)
358 use Zing::Domain;
359 use Zing::Process;
360  
361 my $d1 = Zing::Domain->new(name => 'peers');
362 my $p1 = Zing::Process->new(name => 'p1');
363  
364 $d1->push('mailboxes', $p1->mailbox->term);
365 $p1->execute;
366  
367 # in process (2)
368 use Zing::Domain;
369 use Zing::Process;
370  
371 my $d2 = Zing::Domain->new(name => 'peers');
372 my $p2 = Zing::Process->new(name => 'p2');
373  
374 my $mailboxes = $d2->get('mailboxes');
375  
376 for my $address (@$mailboxes) {
377 # send each registered process a message
378 $p2->send($address, { discovery => $p2->mailbox->term });
379 }
380  
381 $p2->execute;
382  
383 This distribution provides a multi-process management system which allows
384 processes to be deployed and managed separately having the ability to
385 communicate across threads of execution.
386  
387 =cut
388  
389 =head2 atomicity
390  
391 # in process (1)
392 use Zing::KeyVal;
393  
394 my $i = 0;
395 my $kv = Zing::KeyVal->new(name => 'stash');
396  
397 while ($i < 1_000) {
398 $kv->send('random', { value => 1 });
399  
400 # my $data = $kv->recv('random');
401 }
402  
403 # in process (2)
404 use Zing::KeyVal;
405  
406 my $i = 0;
407 my $kv = Zing::KeyVal->new(name => 'stash');
408  
409 while ($i < 1_000) {
410 $kv->send('random', { value => 2 });
411  
412 # my $data = $kv->recv('random');
413 }
414  
415 This distribution provides data storage abstractions which perform atomic reads
416 and write by leveraging L<Redis|https://redis.io> as the default data storage
417 backend.
418  
419 =cut
420  
421 =head2 chainable
422  
423 use Zing::Process;
424 use Zing::Ring;
425  
426 my $p1 = Zing::Process->new(name => 'p1');
427 my $p2 = Zing::Process->new(name => 'p2');
428  
429 my $ring = Zing::Ring->new(processes => [$p1, $p2]);
430  
431 $ring->execute;
432  
433 This distribution provides a mechanism for chaining (i.e. joining) two or more
434 processes together and executing them in a turn-based manner. This ability
435 allows you to design complex hierarchical process topologies.
436  
437 =cut
438  
439 =head2 channels
440  
441 # in process (1)
442 use Zing::Channel;
443  
444 my $chan = Zing::Channel->new(name => 'chat');
445  
446 while (1) {
447 if (my $data = $chan->recv) {
448 # broadcast received
449 warn $data->{text};
450 }
451 }
452  
453 # in process (2)
454 use Zing::Channel;
455  
456 my $chan = Zing::Channel->new(name => 'chat');
457  
458 while (1) {
459 if (my $data = $chan->recv) {
460 # broadcast received
461 warn $data->{text};
462 }
463 }
464  
465 This distribution provides the means for braodcasting and communicating to
466 multiple processes simulaneously through channels which are similar to FIFO
467 queues.
468  
469 =cut
470  
471 =head2 clusterable
472  
473 # in process (1) on cluster (1)
474 use Zing::Queue;
475  
476 my $queue = Zing::Queue->new(name => 'tasks', target => 'global');
477  
478 # pull from global queue
479 $queue->recv;
480  
481 # in process (1) on cluster (2)
482 use Zing::Queue;
483  
484 my $queue = Zing::Queue->new(name => 'tasks', target => 'global');
485  
486 # pull from global queue
487 $queue->recv;
488  
489 This distribution provides support cross-cluster communication and thus
490 operations. Using federated Redis as the data storage backend means you can
491 scale your deployments without changing your implementations.
492  
493 =cut
494  
495 =head2 configuration
496  
497 # configure the namespace (defaults to "main")
498 ZING_NS=app
499  
500 # enable process debugging tracing
501 ZING_DEBUG=1
502  
503 # configure where the command-line tool finds catridges
504 ZING_HOME=/tmp
505  
506 # configure the hostname used in process registration
507 ZING_HOST=0.0.0.0
508 ZING_HOST=68.80.90.100
509  
510 # configure the system datastore (defaults to 'Zing::Redis')
511 ZING_STORE='Zing::Redis'
512  
513 # configure Redis driver without touching your source code
514 ZING_REDIS='server=127.0.0.1:6379'
515 ZING_REDIS='every=1_000_000,reconnect=60'
516 ZING_REDIS='sentinels=127.0.0.1:12345|127.0.0.1:23456,sentinels_cnx_timeout=0.1'
517 ZING_REDIS='server=192.168.0.1:6379,debug=0'
518  
519 # configure where the command-line tool finds catridges and PID files
520 ZING_APPDIR=./
521 ZING_PIDDIR=/tmp
522  
523 This distribution provides environment variables that let you customize how
524 Zing operates and behaves without the need to modify source code. These
525 attributes are not required and fallback to sane defaults.
526  
527 =cut
528  
529 =head2 distributed
530  
531 # in process (1..n) on cluster (1)
532 use Zing;
533  
534 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
535  
536 $zing->execute;
537  
538 # in process (1..n) on cluster (2)
539 use Zing;
540  
541 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
542  
543 $zing->execute;
544  
545 # in process (1..n) on cluster (3)
546 use Zing;
547  
548 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
549  
550 $zing->execute;
551  
552 This distribution provides a collection of actor-model primitives which can be
553 used to create sophisticated and powerful distributed systems, organized within
554 intricate process topologies.
555  
556 =cut
557  
558 =head2 event-driven
559  
560 # in process (1)
561 package MyApp::FileUpoad;
562  
563 use parent 'Zing::Process';
564  
565 sub receive {
566 my ($self, $from, $data) = @_;
567  
568 # react to file-upload events
569  
570 return;
571 }
572  
573 my $p1 = MyApp::FileUpoad->new;
574  
575 $p1->execute;
576  
577 # in process (2)
578 package MyApp::TextTranslate;
579  
580 use parent 'Zing::Process';
581  
582 sub receive {
583 my ($self, $from, $data) = @_;
584  
585 # react to text-translate events
586  
587 return;
588 }
589  
590 my $p2 = MyApp::TextTranslate->new;
591  
592 $p2->execute;
593  
594 This distribution provides all the prerequisites needed to develop scalable
595 reactive event-driven applications distributed across one or several servers.
596  
597 =cut
598  
599 =head2 fifo-queues
600  
601 # in process (1)
602 use Zing::Queue;
603  
604 my $queue = Zing::Queue->new(name => 'tasks');
605  
606 # pull from queue
607 $queue->send({ command => { ... } });
608 $queue->send({ command => { ... } });
609 $queue->send({ command => { ... } });
610 $queue->send({ command => { ... } });
611  
612 # in process (2)
613 use Zing::Queue;
614  
615 my $queue = Zing::Queue->new(name => 'tasks');
616  
617 # pull from FIFO queue
618 $queue->recv;
619  
620 This distribution provides high-performance FIFO message queues which enhance
621 messaging across processes when the order of operations and events is critical
622 and where duplicates can't be tolerated.
623  
624 =cut
625  
626 =head2 hot-reloadable
627  
628 use Zing;
629 use Zing::Daemon;
630  
631 my $daemon = Zing::Daemon->new(
632 name => 'myapp-sleep',
633 app => Zing->new(scheme => ['MyApp::Sleep', [], 4])
634 );
635  
636 $daemon->execute; # pid 12345
637  
638 # $ kill -USR2 12345
639  
640 This distribution provides zero-downtime through hot-reloading which is where
641 the process watchers (supervisors) keep the app running and gracefully reload
642 child processes at runtime.
643  
644 =cut
645  
646 =head2 log-shipping
647  
648 use Zing::Zang;
649  
650 my $zang = Zing::Zang->new(on_perform => sub {
651 my ($self) = @_;
652  
653 $self->log->fatal('something went wrong');
654  
655 return;
656 });
657  
658 $zang->execute;
659  
660 # tap the logs using the command-line tool
661  
662 # $ zing logs
663  
664 This distribution provides the ability to ship the logs of individual processes
665 to a specific centralized channel which can be tapped asynchronously,
666 especially by the command-line tool.
667  
668 =cut
669  
670 =head2 mailboxes
671  
672 use Zing::Process;
673  
674 my $p1 = Zing::Process->new(name => 'p1');
675 my $p2 = Zing::Process->new(name => 'p2');
676  
677 $p1->mailbox->send($p2->mailbox->term, { say => 'ehlo' });
678  
679 my $message = $p2->mailbox->recv;
680  
681 $p2->mailbox->reply($message, { say => 'helo' });
682  
683 This distribution provides every process with its own unique mailbox which can
684 receive messages from other processes as a means of cooperating through message
685 passing.
686  
687 =cut
688  
689 =head2 management
690  
691 use Zing::Kernel;
692  
693 my $kernel = Zing::Kernel->new(scheme => ['MyApp::Logger', [], 2]);
694  
695 $kernel->execute;
696  
697 This distribution provides a kernel process that can be used to manage the
698 deployment of child processes and is used to wrap cartridges by the
699 command-line tool when daemonizing process.
700  
701 =cut
702  
703 =head2 multitasking
704  
705 use Zing::Zang;
706  
707 my $zang = Zing::Zang->new(
708 on_perform => sub {
709 # do something
710 },
711 on_receive => sub {
712 # handle something
713 },
714 );
715  
716 $zang->execute;
717  
718 This distribution provides all processes with an event-loop that allows them to
719 perform multiple operations in sequence and supports operating in a
720 non-blocking manner.
721  
722 =cut
723  
724 =head2 non-blocking
725  
726 use Zing::Zang;
727  
728 my $zang = Zing::Zang->new(
729 on_perform => sub {
730 my ($self) = @_;
731  
732 $self->defer({ command => {...} }) and return;
733  
734 return;
735 },
736 on_receive => sub {
737 my ($self, $from, $data) = @_;
738  
739 return unless $self->term eq $from; # from myself
740  
741 # do something
742  
743 return;
744 }
745 );
746  
747 $zang->exercise;
748  
749 This distribution provides features that allow processes to operate in a
750 non-blocking manner, yielding and deferring operations, and chunking workloads.
751  
752 =cut
753  
754 =head2 parallelism
755  
756 use Zing::Process;
757  
758 my $p1 = Zing::Process->new;
759  
760 my $f1 = $p1->spawn(['MyApp', [id => 12345] 1]);
761 my $f2 = $p1->spawn(['MyApp', [id => 12346] 1]);
762  
763 This distribution provides multiple ways of executing operations in parallel,
764 including spawning processes via forking with the guarantee of not creating
765 zombie processes.
766  
767 =cut
768  
769 =head2 supervisors
770  
771 use Zing::Zang::Watcher;
772  
773 my $zang = Zing::Zang::Watcher->new(
774 scheme => ['MyApp', [] 8]
775 );
776  
777 $zang->exercise;
778  
779 This distribution provides watcher processes which to supervise child processes
780 but also are capable of multitasking and performing other operations while
781 monitoring supervised processes.
782  
783 =head2 virtual-actors
784  
785 use Zing::Zang::Spawner;
786  
787 my $zang = Zing::Zang::Spawner->new(
788 queues => ['schemes']
789 );
790  
791 $zang->exercise;
792  
793 This distribution provides the ability to use virtual actors, which are
794 processes (actors) created on-demand as a result of some system event. This
795 feature is enabled by the L<Zing::Launcher> and L<Zing::Spawner> superclasses.
796  
797 =cut
798  
799 =head1 SEE ALSO
800  
801 L<The Actor Model|https://en.wikipedia.org/wiki/Actor_model>
802  
803 L<Concurrent Computation|https://www.amazon.com/Actors-Concurrent-Computation-Distributed-Systems/dp/026251141X>
804  
805 L<Concurrency in Go/Erlang|https://www.youtube.com/watch?v=2yiKUIDFc2I>
806  
807 L<The Akka Project|https://github.com/akka/akka>
808  
809 L<The Actorkit Project|https://github.com/influx6/actorkit>
810  
811 L<The Orleans Project|http://dotnet.github.io/orleans>
812  
813 L<The Pyakka Project|https://github.com/jodal/pykka>
814  
815 L<The Reactive Manifesto|http://www.reactivemanifesto.org>
816  
817 =head1 DISCLOSURES
818  
819 The following is a list of all the known ways Zing is not like a traditional
820 actor-model system:
821  
822 =over 4
823  
824 =item *
825  
826 In Zing, actors act independently and aren't beholden to a system manager.
827  
828 =item *
829  
830 In Zing, actors are always active (each runs its own infinite event-loop).
831  
832 =item *
833  
834 In Zing, actors can communicate unrestricted (no approved communicators).
835  
836 =item *
837  
838 In Zing, actors can block using C<poll> but do not block by default.
839  
840 =item *
841  
842 In Zing, the default datastore/backend is L<Redis|https://redis.io> which means
843 the system is (by default) subject to the guarantees and limitations of that
844 system. Data is serialized as L<JSON|https://json.org> and stored in
845 plain-text.
846  
847 =back
848  
849 =cut
850  
851 =head1 AUTHOR
852  
853 Al Newkirk, C<awncorp@cpan.org>
854  
855 =head1 LICENSE
856  
857 Copyright (C) 2011-2019, Al Newkirk, et al.
858  
859 This is free software; you can redistribute it and/or modify it under the terms
860 of the The Apache License, Version 2.0, as elucidated in the L<"license
861 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
862  
863 =head1 PROJECT
864  
865 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
866  
867 L<Project|https://github.com/iamalnewkirk/zing>
868  
869 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
870  
871 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
872  
873 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
874  
875 L<Issues|https://github.com/iamalnewkirk/zing/issues>
876  
877 =cut