lib/Rex/Transaction.pm

filter: Statementstmt 82.6% 38 / 46 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 83.0% 49 / 59 CC 3 SCAR 5.4
CC
3
Cov
81%
transaction
3.3
on_rollback
2.0
BEGIN
1.0
CRAP 1.7
« lib/Rex/Interface/Cache/Base.pm ↑ Summary lib/Rex/User/Base.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Transaction - Transaction support
8  
9 =head1 DESCRIPTION
10  
11 With this module you can define transactions and rollback scenarios on failure.
12  
13 =head1 SYNOPSIS
14  
15 use Rex::Transaction;
16  
17 task 'do-something', 'server01', sub {
18 transaction {
19 on_rollback {
20 rmdir '/tmp/mydata';
21 };
22  
23 mkdir '/tmp/mydata';
24 upload 'files/myapp.tar.gz', '/tmp/mydata';
25 run 'tar xzf myapp.tar.gz -C /tmp/mydata';
26 if ( $? != 0 ) { die('Error extracting myapp.tar.gz'); }
27 };
28 };
29  
30 =head1 EXPORTED FUNCTIONS
31  
32 =cut
33  
34 package Rex::Transaction;
35  
36 22 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 22 our $VERSION = '1.16.1'; # VERSION
40  
41 22 require Exporter;
42  
43 22 ▶use vars qw(@EXPORT @ROLLBACKS);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 22 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 22 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 22 ▶use Rex::TaskList;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 22 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 22 @EXPORT = qw(transaction on_rollback);
51  
52 =head2 transaction($codeRef)
53  
54 Start a transaction for C<$codeRef>. If C<$codeRef> dies, Rex will run the L<on_rollback|https://metacpan.org/pod/Rex::Transaction#on_rollback> code to roll back the transaction.
55  
56 task 'deploy', group => 'frontend', sub {
57 on_rollback {
58 rmdir '...';
59 };
60  
61 deploy 'myapp.tar.gz';
62 };
63  
64 task 'restart_server', group => 'frontend', sub {
65 service apache2 => 'restart';
66 };
67  
68 task 'all', group => 'frontend', sub {
69 transaction {
70 do_task [qw/deploy restart_server/];
71 };
72 };
73  
74 =cut
75  
76 2 ▶sub transaction(&) {
Subroutine transaction: called CC=3 SCAR=11.9
Pod documented
77 2 my ($code) = @_;
78 2 my $ret = 1;
79  
80 2 Rex::Logger::debug("Cleaning ROLLBACKS array");
81 2 @ROLLBACKS = ();
82  
83 2 Rex::TaskList->create()->set_in_transaction(1);
84  
85 2 eval { &$code(); };
86  
87 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 0 2
88 2 my $err = $@;
89 2 Rex::Logger::info("Transaction failed. Rolling back.");
90  
91 2 $ret = 0;
92 2 for my $rollback_code ( reverse @ROLLBACKS ) {
93  
94 # push the connection of the task back
95 0 Rex::push_connection( $rollback_code->{"connection"} );
96  
97 # run the rollback code
98 0 &{ $rollback_code->{"code"} }($err);
99  
100 # and pop it away
101 0 Rex::pop_connection();
102 }
103  
104 2 Rex::TaskList->create()->set_in_transaction(0);
105  
106 2 die("Transaction failed. Rollback done.");
107 }
108  
109 0 Rex::TaskList->create()->set_in_transaction(0);
110  
111 0 return $ret;
112  
113 }
114  
115 =head2 on_rollback($codeRef)
116  
117 This will execute C<$codeRef> if a step in the L<transaction|https://metacpan.org/pod/Rex::Transaction#transaction> fails.
118  
119 =cut
120  
121 0 ▶sub on_rollback(&) {
Subroutine on_rollback: not called CC=1 SCAR=6.9
Pod documented
122 0 my ($code) = @_;
123  
124 0 push(
125 @ROLLBACKS,
126 {
127 code => $code,
128 connection => Rex::get_current_connection()
129 }
130 );
131 }
132  
133 22 1;
« lib/Rex/Interface/Cache/Base.pm ↑ Summary lib/Rex/User/Base.pm »