File Coverage

blib/lib/Schedule/LongSteps/Process.pm
Criterion Covered Total %
statement 31 32 96.8
branch 5 8 62.5
condition n/a
subroutine 7 8 87.5
pod 5 5 100.0
total 48 53 90.5


line stmt bran cond sub pod time code
1             package Schedule::LongSteps::Process;
2             $Schedule::LongSteps::Process::VERSION = '0.023';
3 11     11   6619 use Moose;
  11         22  
  11         82  
4 11     11   72970 use Log::Any qw/$log/;
  11         61  
  11         118  
5              
6             has 'longsteps' => ( is => 'ro', isa => 'Schedule::LongSteps' , required => 1);
7              
8             has 'stored_process' => ( is => 'ro' );
9              
10             =head1 NAME
11              
12             Schedule::LongSteps::Process - A base class for all LongSteps processes.
13              
14             =cut
15              
16             =head2 state
17              
18             Returns the current state (a HashRef JSONable data structure)
19             of this process.
20              
21             =cut
22              
23             sub state{
24 12     12 1 1000 my ($self) = @_;
25 12         362 return $self->stored_process()->state();
26             }
27              
28             =head2 new_step
29              
30             Returns a new step from the given properties.
31              
32             Usage examples:
33              
34             # In a process class.
35             sub do_current_step{
36             my ($self) = @_;
37             return $this->new_step({ run_at => DateTime->... , what => 'do_nextstep' , state => { new => 'state' } });
38             }
39              
40             If you omit run_at, the process will not run again and stay in paused state until you go and set the run_at
41             time manually.
42              
43             If you omit 'what', the next step to run will be the same as the current step.
44              
45             If you omit 'state', the state just doesnt change.
46              
47             =cut
48              
49             sub new_step{
50 29     29 1 9655 my ($self, $step_properties) = @_;
51 29         450 return $step_properties;
52             }
53              
54             =head2 final_step
55              
56             Returns a final step that will never run
57             from the given properies.
58              
59             Usage:
60              
61             # In a Process class.
62             sub do_last_thing{
63             my ($self) = @_;
64             return $self->final_step({ state => { the => 'final' , state => 1 } });
65             }
66              
67             If you omit 'state', the current state of the process just doesnt change and
68             the process terminates with the current state.
69              
70             =cut
71              
72             sub final_step{
73 13     13 1 181 my ($self, $step_properties) = @_;
74 13 50       51 defined( $step_properties ) or ( $step_properties = {} );
75              
76             return {
77 13         469 %$step_properties,
78             run_at => undef,
79             status => 'terminated'
80             };
81             }
82              
83             =head2 wait_processes
84              
85             Wait for the given process IDs and returns whatever the given
86             closure returns.
87              
88             Usage:
89              
90             return $this->wait_process(
91             [ $pid1 , $pid2 ],
92             sub{
93             ...
94             return $this->new_step(...); # or whatever usual stuff
95             }
96              
97             If you skip the closure, this will just terminate $this process after the
98             given subprocesses have finished.
99              
100             =cut
101              
102             sub wait_processes{
103 2     2 1 6 my ($self, $process_ids, $on_finish) = @_;
104 2 50       6 defined( $process_ids ) or ( $process_ids = [] );
105 2 50   0   6 defined( $on_finish ) or ( $on_finish = sub{ $self->final_step(); } );
  0         0  
106              
107 2         5 my @processes = map{ $self->longsteps()->find_process( $_ ) } @$process_ids;
  4         110  
108 2         6 my @finished_processes = grep{ $_->status() eq 'terminated' } @processes;
  4         105  
109              
110 2         13 $log->debug(scalar(@finished_processes)." are finished");
111              
112 2 100       10 if( scalar( @processes ) == scalar( @finished_processes ) ){
113 1         4 $log->debug("Calling on_finish");
114 1         5 return $on_finish->( @finished_processes );
115             }
116             # Run at next tick
117 1         5 $log->debug("Will wait a little bit more");
118 1         6 return $self->new_step({ run_at => DateTime->now() });
119             }
120              
121              
122             =head2 revival_methods
123              
124             Returns an array-ref of revive methods associated with the process.
125              
126             =cut
127              
128             sub revival_methods {
129 1     1 1 598 my ($self) = @_;
130 1         5 my @revival_methods = map {$_->name}grep {$_->name =~ /^revive_/} $self->meta->get_all_methods;
  1         5  
  24         2815  
131 1         3 return \@revival_methods;
132             }
133              
134              
135             __PACKAGE__->meta->make_immutable();
136