Rex::Transaction(3)

πŸ“¦ version 1.14.3, in nixos 26.05, nixpkgs unstable

✨ Rex::Transaction(3) β€” the Rex::Transaction era ✨

okay so real talk: this is Rex::Transaction, straight out of the User Contributed Perl Documentation section of the manual, and it's giving undo button for your whole deploy πŸ’… bestie, it's literally transaction support, iykyk.


🌸 NAME 🌸

Rex::Transaction β€” transaction support, no cap πŸŽ€

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the whole vibe: with this module you can define transactions and rollback scenarios for when stuff goes wrong on failure. that's it, that's the pitch. if your task face-plants, you get to clean up after it instead of leaving chaos everywhere 😭✨

πŸ’– SYNOPSIS πŸ’–

here's the whole thing in action, copy it exactly bestie:

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 it. mkdir, upload, run, and if the $? isn't 0 it dies β€” and dying is exactly what kicks the rollback into gear πŸ”₯


πŸ‘‘ EXPORTED FUNCTIONS πŸ‘‘

πŸŽ€ transaction($codeRef) πŸŽ€

this one starts a transaction for $codeRef 🫢. and here's the important bit: if $codeRef dies, Rex will run the on_rollback code to roll the whole transaction back. that's the deal. no cap.

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 are just sitting there living their lives, and then transaction wraps do_task [qw/deploy restart_server/] so the whole group either slays together or rolls back together? main character energy 🌈

🧚 on_rollback($codeRef) 🧚

this will execute $codeRef if a step in the transaction fails πŸ’€. it's the cleanup crew, the safety net, the "okay we tried, let's put the furniture back" moment. iconic.


⭐ lowkey important: the rollback only kicks in on failure β€” that's literally what it's for. define your on_rollback inside the transaction block and let it handle the mess πŸ™ˆ

and that's the whole page bestie, go forth and transact responsibly πŸ’œ

this cutie pie of a manpage is brought to you by perl v5.42.3, dated 2023-08-05, and documented as Rex::Transaction(3) β€” take care, mwah πŸ“πŸ’–

πŸ“¦ where this page comes from