« LongSteps/Storage/Memory.pm
↑ Summary
LongSteps/Storage/AutoDBIx/Schema/Result/LongstepProcess.pm »
| 1 | package Schedule::LongSteps::Process; | ||||||||||
| 2 | 11 | $Schedule::LongSteps::Process::VERSION = '0.023'; | |||||||||
| 3 | 11 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use Log::Any qw/$log/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 11 | has 'longsteps' => ( is => 'ro', isa => 'Schedule::LongSteps' , required => 1); | |||||||||
| 7 | |||||||||||
| 8 | 11 | 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 | 12 | ▶ | sub state{ | ||||||||
|
Subroutine
state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 12 | my ($self) = @_; | |||||||||
| 25 | 12 | 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 | 29 | ▶ | sub new_step{ | ||||||||
|
Subroutine
new_step: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 29 | my ($self, $step_properties) = @_; | |||||||||
| 51 | 29 | 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 | 13 | ▶ | sub final_step{ | ||||||||
|
Subroutine
final_step: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 73 | 13 | my ($self, $step_properties) = @_; | |||||||||
| 74 | 13 | ▶ | defined( $step_properties ) or ( $step_properties = {} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | return { | ||||||||||
| 77 | 13 | %$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 | 2 | ▶ | sub wait_processes{ | ||||||||
|
Subroutine
wait_processes: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 103 | 2 | my ($self, $process_ids, $on_finish) = @_; | |||||||||
| 104 | 2 | ▶ | defined( $process_ids ) or ( $process_ids = [] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | ▶ | defined( $on_finish ) or ( $on_finish = sub{ $self->final_step(); } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||
| 106 | |||||||||||
| 107 | 2 | my @processes = map{ $self->longsteps()->find_process( $_ ) } @$process_ids; | |||||||||
| 108 | 2 | my @finished_processes = grep{ $_->status() eq 'terminated' } @processes; | |||||||||
| 109 | |||||||||||
| 110 | 2 | $log->debug(scalar(@finished_processes)." are finished"); | |||||||||
| 111 | |||||||||||
| 112 | 2 | ▶ | if( scalar( @processes ) == scalar( @finished_processes ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 1 | $log->debug("Calling on_finish"); | |||||||||
| 114 | 1 | return $on_finish->( @finished_processes ); | |||||||||
| 115 | } | ||||||||||
| 116 | # Run at next tick | ||||||||||
| 117 | 1 | $log->debug("Will wait a little bit more"); | |||||||||
| 118 | 1 | 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 | 1 | ▶ | sub revival_methods { | ||||||||
|
Subroutine
revival_methods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 129 | 1 | my ($self) = @_; | |||||||||
| 130 | 1 | my @revival_methods = map {$_->name}grep {$_->name =~ /^revive_/} $self->meta->get_all_methods; | |||||||||
| 131 | 1 | return \@revival_methods; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | 11 | __PACKAGE__->meta->make_immutable(); | |||||||||
| 136 | |||||||||||