#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Luxembourg
    Posts
    89
    Rep Power
    15
    Hi

    I use flock DB,2 while running an archiving routine, but other routines - also using flock DB,2 - elsewhere in the application are writing to the file before the archiving is completed so I get lost / corrupted data.

    Do I have to include use:flock or something to make flock work or is it automatically available to my perl scripts?

    Or what else could be happening?

    Some details:-

    "reservations.txt" file has some 2500 records. The script extracts "expired" records and writes them to history.txt, while extracting and writing "current" records to "resback.txt" file. It then renames "resback.txt" to "reservations.txt".

    Script
    ------
    I inserted some "sleeps" to permit actions time to complete. The $newname.txt is a backup file as I need to recover lost data so often.

    open(DB, "data/reservations.txt") or die "Error opening reservations file: $!n";
    flock DB,2;

    open(ARCHIVE,">>data/history.txt") or die "Error opening archive file: $!n";
    flock ARCHIVE,2;

    open(TEMP,">data/resback.txt") or die "Error opening temp file: $!n";
    flock TEMP,2;

    $newname = time();
    open(BACKUP,">data/$newname.txt") or die "Error opening backup file: $!n";
    flock BACKUP,2;

    while(<DB> ){
    ($delkey,$gamedate,$court,$start,$player,$partner,$res,$slotgame)= split(/ /, $_);
    print BACKUP $_ ;

    if(($gamedate < $thisdate)&#0124; &#0124;(($gamedate < $tomoro)&&($start < $thour))){
    print ARCHIVE $_ ;
    }else{
    print TEMP $_ ;
    }
    }
    sleep 10;

    flock ARCHIVE,8;
    close(ARCHIVE);

    if(-e "data/resback.txt"){
    unlink("data/reservations.txt") &#0124; &#0124; die "Could not write from backup! $!";
    sleep 10;
    rename "data/resback.txt", "data/reservations.txt")&#0124; &#0124; die "Could not rename temp file! $!";
    sleep 10;
    }else{
    $message = "Error ! resback does not exist";
    require "error_notify.cgi";
    }
    flock DB,8;
    close(DB);

    flock BACKUP,8;
    close(BACKUP);
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    Your script doesn't need to use "flock" at all. While running your script, no other scripts will write to history.txt, resback.txt and $newname.txt.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Luxembourg
    Posts
    89
    Rep Power
    15
    I didn't express myself clearly.
    It's the reservations.txt file which is being written to by other routines
  6. #4
  7. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>which is being written to by other routines

    which is?

    Also,
    rename "data/resback.txt", "data/reservations.txt")| | die "Could not rename temp file! $!";

    why you placed this before the flock DB,8;?
    "8" is "unlock" and you were trying to overwrite the existing reservations.txt by resback.txt?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Luxembourg
    Posts
    89
    Rep Power
    15
    Hi freebsd

    >you were trying to overwrite the existing >reservations.txt by resback.txt?

    Correct.
    So I move the flock DB,8 to after the overwrite?
    What about the flock ARCHIVE,8 ? Is this redundant as the file name has changed?

    But how do I truly lock reservations.txt to prevent other routines ("reserve", "cancel reservation", "modify an existing reservation") from modifying the file until the archiving and renaming is completed?

    Thanks for any help, as my client is more than upset at the data loss (!)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Luxembourg
    Posts
    89
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
    Your script doesn't need to use "flock" at all. While running your script, no other scripts will write to history.txt, resback.txt and $newname.txt.[/quote]

    Well, the archive run is triggered by a player making a menu selection - to clear all played games (as they can only make a limited number of bookings, so his played games must be removed to show a current allocation). Hence a second player entering the system can trigger the archive run while it is running for the first player. I thought flocking would prevent any simultaneous runs.
  12. #7
  13. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>So I move the flock DB,8 to after the overwrite?

    Yes. In fact, you just need to flock DB, no others.

    >>But how do I truly lock reservations.txt to prevent other routines

    Simply flocking DB, that is it. All others are redundant. Also remove all the "sleep".

    >>I thought flocking would prevent any simultaneous runs

    Check here -> http://www.devshed.com/Talk/Forums/F...ML/000349.html

    Also,
    1 - Shared
    2 - Exclusive
    4 - Don't block when locking
    8 - Unlock

IMN logo majestic logo threadwatch logo seochat tools logo