in reply to Is "die" the best way to be atomic?

If by 'atomic' you mean 'do things all in one go, or not at all', then no, it isnt. To me it sounds like you want some sort of all-or-nothing transaction, in which either all the commands are complete, or none at all.

I'm not sure quite what those commands are doing, but what happens if you update the hostgroup, but fail to update the host, do you have an inconsistent system then? In which case you need to code some sort of 'undo' sub, which you can pass a value which indicates how far the script got before it encountered an error, and proceeds to undo the previous steps to return to the state the system was in before you started. And *then* it can die, or exit, or whatever..

Eg:

chdir("$staging_directory") or undo(1); system("svn", "update", "base") or undo(2); ... sub undo { my $state = shift; if($state >= .. ) # highest one first .. if($state >= 2) { system("svn" .. ) # undo svn/update/base command } if($state >= 1) { chdir("-"); # undo chdir command } die "Got to level X: $!\n"; }
.. or something.. but maybe I misunderstood what you wanted. C.

Replies are listed 'Best First'.
Re: Re: Is "die" the best way to be atomic?
by BuddhaNature (Beadle) on Apr 27, 2004 at 16:28 UTC
    Firstly, I am glad someone saw threough my poorly worded question what I meant - I _am_ looking for your definition of atomic - "do it, or don't do it - don't go halfway..." Well the two main places of concern are the permissions script and the rsync - if something goes wrong with either it could be a BadThing(tm).

    I like your idea of the undo() function, but am not sure it would be feasible in either case, meaning I am not sure if there would be a way to actually undo what had occurred. One way I might be able to cover myself on the rsync is to first run the rsync with the --dry-run option, and as long as all goes well run it without. Is there any kind of "dry run" in perl? Does eval or try work in that way - in that it tries to do it, but doesn't actually do it unless it is sucessful?

    -Shane

      Not really, no. At least, there appear to be a few modules for doing transactional perl code, eg WhatIf and Transaction, but these are not likely to be able to rollback system calls. So you'll need to know how to do that yourself.

      Seeing as the permissions call appears to be to a script you wrote yourself, (if I read the post correctly), then surely you know what that does internally, and how to roll it back? You might even make it take some sort of parameter, which makes it rollback itself.

      As to the rsync, there are also some Rsync modules, but whether any support rollback/transactions I couldnt see. (I would have thought any decent backup mechanism would be atomic somehow)

      C.