| 1 | package MYDan::Deploy::Jobs; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use MYDan::Node; | ||
| 7 | |||
| 8 | our %CONF = ( redo => 0, retry => 0, timeout => 0, goon => '100%' ); | ||
| 9 | |||
| 10 | sub new | ||
| 11 | { | ||
| 12 | my ( $class, %self ) = splice @_; | ||
| 13 | |||
| 14 | map{ confess "no $_" unless defined $self{$_} }qw( name step conf ctrl code cache ); | ||
| 15 | |||
| 16 | bless \%self, ref $class || $class; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub run | ||
| 20 | { | ||
| 21 | my ( $this, @node ) = @_; | ||
| 22 | |||
| 23 | @node = map{ @$_ }@node if ref $node[0] eq 'ARRAY'; | ||
| 24 | my %node = map{ $_ => 1 }@node; | ||
| 25 | |||
| 26 | my ( $name, $step, $conf, $ctrl, $code, $cache ) | ||
| 27 | = @$this{qw( name step conf ctrl code cache )}; | ||
| 28 | |||
| 29 | map { $conf->{$_} = $CONF{$_} unless defined $conf->{$_} }keys %CONF; | ||
| 30 | my ( $redo, $retry, $title, $delay, $sleep, $repeat, $timeout, $grep, $fix, $goon ) | ||
| 31 | = @$conf{qw( redo retry title delay sleep repeat timeout grep fix goon )}; | ||
| 32 | |||
| 33 | $goon = ( $goon * scalar @node ) / 100 if $goon =~ s/%$//; | ||
| 34 | |||
| 35 | print "\n\n", "=" x 75,"\n"; | ||
| 36 | printf "title: $title | step: %s| node:%d\n", $step, scalar @node; | ||
| 37 | print "=" x 75,"\n"; | ||
| 38 | |||
| 39 | my ( $range, %succ, %tryfix ) = MYDan::Node->new(); | ||
| 40 | |||
| 41 | for my $i ( 0 .. $redo ) | ||
| 42 | { | ||
| 43 | print '#' x 75 ,"\n"; | ||
| 44 | printf "%s ...\n", $tryfix{$i} ? 'tryfix' : $i ? "redo #$i" : 'do'; | ||
| 45 | |||
| 46 | my ( $try, $error ) = $tryfix{$i} ? $tryfix{$i} -1 : $retry; | ||
| 47 | for my $j ( 0 .. $try ) | ||
| 48 | { | ||
| 49 | print '-' x 75 ,"\n"; | ||
| 50 | if( $tryfix{$i} ) | ||
| 51 | { | ||
| 52 | last unless $ctrl->stuck( $title, $step ); | ||
| 53 | printf "try fix %s\n", $j +1; | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | print "retry $j\n" if $j; | ||
| 58 | sleep 3 while $ctrl->stuck( $title, $step ); | ||
| 59 | } | ||
| 60 | |||
| 61 | %succ = () if $repeat; | ||
| 62 | my %excluded = map{ $_ => 1 }@{$ctrl->excluded()}; | ||
| 63 | @node = grep{ ! $succ{$_} }grep{ ! $excluded{$_} }@node; | ||
| 64 | @node = grep{ $cache->{succ}{$grep}{$_} }@node if $grep; | ||
| 65 | |||
| 66 | # last if ! @node && $tryfix{$i}; | ||
| 67 | |||
| 68 | |||
| 69 | printf "time: %s\nnode[%s]: %s\n", | ||
| 70 | POSIX::strftime( "%F_%T", localtime ), | ||
| 71 | scalar $range->load( \@node )->list(), | ||
| 72 | $range->load( \@node )->dump(); | ||
| 73 | |||
| 74 | sleep $delay if $delay && print "delay $delay sec ...\n"; | ||
| 75 | |||
| 76 | |||
| 77 | my %s; | ||
| 78 | eval{ | ||
| 79 | alarm $timeout; | ||
| 80 | %s = &$code | ||
| 81 | ( | ||
| 82 | name => $name, | ||
| 83 | step => $step, | ||
| 84 | title => $title, | ||
| 85 | param => $conf->{param}, | ||
| 86 | batch => \@node, | ||
| 87 | cache => $cache, | ||
| 88 | ); | ||
| 89 | alarm 0; | ||
| 90 | }; | ||
| 91 | alarm 0; | ||
| 92 | $error = $@ || ''; | ||
| 93 | |||
| 94 | map{ $succ{$_} = $s{$_} }grep{ $node{$_} }keys %s; | ||
| 95 | |||
| 96 | $error = sprintf "goon: $goon succ: %s err:$error", scalar keys %succ | ||
| 97 | if keys %succ < $goon; | ||
| 98 | |||
| 99 | |||
| 100 | sleep $sleep if $sleep && print "sleep $sleep sec ...\n"; | ||
| 101 | last unless $error; | ||
| 102 | print "[error]: $error\n"; | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | $ctrl->resume( $title, $step ) if $tryfix{$i} && !$error; | ||
| 107 | |||
| 108 | last unless $error; | ||
| 109 | $ctrl->pause( 'error', $title, $step, $error ) unless $tryfix{$i}; | ||
| 110 | |||
| 111 | redo if ! $tryfix{$i} && ( $tryfix{$i} = $fix ); | ||
| 112 | sleep 3 while $ctrl->stuck( $title, $step ); | ||
| 113 | } | ||
| 114 | |||
| 115 | return %succ; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; |