|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
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)| |(($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") | | die "Could not write from backup! $!"; sleep 10; rename "data/resback.txt", "data/reservations.txt")| | 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
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
I didn't express myself clearly.
It's the reservations.txt file which is being written to by other routines |
|
#4
|
|||
|
|||
|
>>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? |
|
#5
|
|||
|
|||
|
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 (!) |
|
#6
|
|||
|
|||
|
<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. |
|
#7
|
|||
|
|||
|
>>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/Forum6/HTML/000349.html Also, 1 - Shared 2 - Exclusive 4 - Don't block when locking 8 - Unlock |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > flocking not working? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|