File Coverage

blib/lib/Perinci/Tx/Util.pm
Criterion Covered Total %
statement 46 48 95.8
branch 14 16 87.5
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 68 72 94.4


line stmt bran cond sub pod time code
1             package Perinci::Tx::Util;
2              
3 1     1   52820 use 5.010001;
  1         4  
  1         42  
4 1     1   5 use strict;
  1         2  
  1         36  
5 1     1   5 use warnings;
  1         2  
  1         40  
6              
7 1     1   7 use Perinci::Sub::Util qw(err);
  1         2  
  1         49  
8 1     1   771 use UUID::Random;
  1         207  
  1         152  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             use_other_actions
14             );
15              
16             our $VERSION = '0.38'; # VERSION
17              
18             sub use_other_actions {
19 5     5 1 14117 my %args = @_;
20 5         16 my $actions = $args{actions};
21              
22 1     1   5 no strict 'refs';
  1         1  
  1         369  
23              
24 5         8 my ($has_unfixable, $has_fixable, $has_error);
25 0         0 my (@do, @undo);
26 0         0 my ($res, $a);
27 5         9 my $i = 0;
28 5         12 for (@$actions) {
29 13         16 $a = $_;
30 13         19 my $f = $a->[0];
31 13         69 my ($pkg) = $a->[0] =~ /(.+)::.+/;
32 13 50       33 $pkg or
33             return [400, "action #$i: please supply qualified function name"];
34              
35 13         14 $res = $f->(%{$a->[1]},
  13         71  
36             -tx_action=>'check_state',
37             -tx_v=>2,
38             # it's okay to use random here, when tm records undo data it
39             # will record by calling actions in do_actions directly, not
40             # using our undo data
41             -tx_action_id=>UUID::Random::generate(),
42             );
43 13 100       790 if ($res->[0] == 200) {
    100          
    100          
44 4         7 $has_fixable++;
45 4         6 push @do, $a;
46 4         8 my $uu = $res->[3]{undo_actions};
47 4         10 for my $u (@$uu) {
48 4 50       20 $u->[0] = "$pkg\::$u->[0]" unless $u->[0] =~ /::/;
49             }
50 4         10 unshift @undo, @$uu;
51             } elsif ($res->[0] == 304) {
52             # fixed
53             } elsif ($res->[0] == 412) {
54 1         2 $has_unfixable++;
55 1         3 last;
56             } else {
57 1         3 $has_error++;
58 1         4 last;
59             }
60 11         67 $i++;
61             }
62              
63 5 100       20 if ($has_error) {
    100          
    100          
64 1         9 err(500, "There is an error: action #$i: ", $res);
65             } elsif ($has_unfixable) {
66 1         9 err(412, "There is an unfixable state: action #$i: ", $res);
67             } elsif ($has_fixable) {
68 1         9 [200, "Some action needs to be done", undef, {
69             do_actions => \@do,
70             undo_actions => \@undo,
71             }];
72             } else {
73 2         50 [304, "No action needed"];
74             }
75             }
76              
77             1;
78             # ABSTRACT: Helper when writing transactional functions
79              
80             __END__