Rex::Transaction(3pm)

πŸ’– Rex::Transaction πŸ’–

okay bestie, so like… this is Rex::Transaction, part of the User Contributed Perl Documentation. yes it's a Perl module manual page (a 3pm one, iykyk) and yes it's about transactions. stay with me. 🫢


✨ NAME ✨

Rex::Transaction β€” Transaction support. that's it, that's the name, periodt. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so with this module you can define transactions and rollback scenarios on failure. that's the whole vibe: you do a bunch of stuff, and if something goes wrong mid-way, Rex can undo it. main character energy fr fr. πŸ”₯


⭐ SYNOPSIS ⭐

here's the whole thing, exactly as it goes down:

use Rex::Transaction;

task 'do-something', 'server01', sub {
  transaction {
    on_rollback {
      rmdir '/tmp/mydata';
    };

    mkdir '/tmp/mydata';
    upload 'files/myapp.tar.gz', '/tmp/mydata';
    run 'tar xzf myapp.tar.gz -C /tmp/mydata';
    if ( $? != 0 ) { die('Error extracting myapp.tar.gz'); }
  };
};

like. look at that. clean. 😭✨


🌸 EXPORTED FUNCTIONS 🌸

these are the two lil guys this module hands you. both iconic, both necessary.

πŸ¦‹ transaction($codeRef)

Start a transaction for $codeRef. If $codeRef dies, Rex will run the on_rollback code to roll back the transaction. that's the deal, no cap. πŸ’œ

(if you wanna hop straight to that section on metacpan, it's on_rollback β€” https://metacpan.org/pod/Rex::Transaction#on_rollback.)

task 'deploy', group => 'frontend', sub {
  on_rollback {
    rmdir '...';
  };

  deploy 'myapp.tar.gz';
};

task 'restart_server', group => 'frontend', sub {
  service apache2 => 'restart';
};

task 'all', group => 'frontend', sub {
  transaction {
    do_task [qw/deploy restart_server/];
  };
};

see how deploy and restart_server both get swept up into one transaction? so if the restart flops, the deploy gets rolled back too. chef's kiss. πŸ‘‘

πŸ“ on_rollback($codeRef)

This will execute $codeRef if a step in the transaction fails. (here's the link if you need it: https://metacpan.org/pod/Rex::Transaction#transaction.) it's your little safety net, your plan B, your "okay we're fixing this" moment. πŸ™ˆβœ¨

ok real talk bestie β€” on_rollback only fires when a step in the transaction fails. so put your cleanup code in there and make it count. πŸ’…

πŸ’€ one (1) tiny detail before you go πŸ’€

nothing else here to warn you about β€” the page really is this short. go forth and transact. 🌈


and that's a wrap on the Rex::Transaction(3pm) page, bestie πŸ’– it came to you from the User Contributed Perl Documentation, running on perl v5.40.1, dated 2025-10-03. slay, bye, see you in the next manpage. πŸŽ€πŸ¦‹πŸ’…

πŸ“¦ where this page comes from