Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

pileofrogs,

Because of your concern about a 'flock' on the lock from a runaway process, you can use 2 files, one for the locking and one for the information your caching. I'm going to show untested code to give you an idea of how to go about it, but only you can decide what to do in a true deadlock situation.

my $lock_file = "./LockFile"; ## Put this in a common place GetLock: if ( ! -e $lock_file ) ## No Process is working { open ( my $lock, ">", $lock_file ) or die "Can't open $! "; if ( flock ( $lock, LOCK_EX | LOCK_NB ) ) { print $lock "$$\n"; close $lock; ## Now, you need to make sure your the process with the lock open ( $lock, "<", $lock_file ) or die "Can't open $! "; if ( flock ( $lock, LOCK_SH ) ) { my $pid = < $lock >; chomp ( $pid ); close $lock; if ( $pid eq "$$" ) { last; } ## Good--you have the lo +ck else { redo GetLock; } } else { redo GetLock; } } else { ## Another process has the exclusive lock close $lock; usleep 1_000; ## wait .001 seconds redo GetLock; } } elsif { if ( -s $lock_file ) ## Another Process is already w +orking { open ( my $lock, "<", $lock_file ) or die "Can't open $! " +; my $who = < $lock >; chomp ( $who ); close $lock; ## Now you can stat the file and if it exists longer than ## some number of seconds/minutes, you have the pid to k +ill it. } else { redo GetLock; } } ## Put your testing and cache here.

The idea is to verify you have the lock before starting the cache process. Once you are sure you have the lock, you can test the cache file to determine if it is fresh or stale. When you finish your work you 'unlink' the lock file.

Things to think about:

  • Do you need a counter on how many times to 'redo GetLock'
  • Do you need to issue an warning email/text message for deadlock.
  • Instead of using 'die' to have a common routine to issue warning message to the system admin.
  • Maybe BrowserUk's solution is better!

Good Luck!

"Well done is better than well said." - Benjamin Franklin


In reply to Re: Concurrent Cache Pattern by flexvault
in thread Concurrent Cache Pattern by pileofrogs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [hippo]: Make it an option?
    [Corion]: hippo: Yes, but I'm not sure that it's even worth the effort of implementing it at all...
    [Corion]: You'll only ever need that option if you have a long-running query whose results are not cached by your DB already, and in those cases I presume that the programmer will want to maintain the temporary tables themselves - I wouldn't know when to drop ...
    [Corion]: ... the temporary tables, and also don't have a good idea on how to create unique table names for them
    [hippo]: OIC. In that case leave it out but invite feature requests and see if any of the users suggest it. :-)
    [Corion]: Talking about this makes me realize that it's likely only a half useful idea. But it still would be convenient to have as an option...
    [Corion]: hippo: Hmmm - yeah, I could document it and wait for code implementing that option to show up ;-D

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (11)
    As of 2017-02-23 15:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?






      Results (347 votes). Check out past polls.