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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I can't visualize is how are we going to disregard the open timer if the user closes the showcase? The work has already been passed to the thread. Also the walk timer needs to respect multiple key presses and update the expiry time, and cease termination and "restart" work.

Then we need to be able to ask the thread to do two things:

  1. Add (or re-add) a showcase: $Qwalktimer->enqueue( "add:$showcasenum" );
  2. Remove an exist showcase: $Qwalktimer->enqueue( "del:$showcasenum" );

And that means adding a test : if( exists $showcase{ $showcasenum }  ) { for an showcase number;

And keeping a record of where the current timer for that showcase is: %showcase.

Now, when 'add'ing a showcase, we remove any existing timer before adding a new one. And if the cmd eq 'del', then we just remove the existing timer.

sub walktimer { ## The queue handle is passed in when the thread is started my $Q = shift; #Sleep for 180 seconds, upon completion execute SQL to change OK_O +PEN back to all zero for $showcasenum ## A hash to remember when things time out. my( %when, %showcase ); ## wake up once per second while( sleep 1 ) { ## while there are showcase numbers on the queue while( $Q->pending ) { ## grab them one at a time my( $cmd, $showcasenum ) = split ':', $Q->dequeue(); ## If a timer already exists for this showcase if( exists $showcase{ $showcasenum } ) { my $when = $showcase{ $showcasenum }; ## remove the timer @{ $when{ $when } } = grep{ $_ != $showcasenum } @{ $w +hen{ $when } }; ## and the pointer to it. delete $showcase{ $showcasenum }; ## And if this command is a delete request that's all next if $cmd eq 'del'; } ## Otherwise its a (re)add, so add the new timer ## Calculate when the timeout should occur, my $timeout = time() + $walktime; ## and add the showcase number to the hash to be dealt wit +h at that time push @{ $when{ $timeout } }, $showcasenumber; ## And remember its there $showcase{ $showcasenum } = $timeout; } ## find any times that have expired (earlier than now!) for my $time ( grep{ $_ < time() } keys %when ) { ## And for each showcase number scheduled at that time for my showcasenum ( @{ $when{ $time } } ) { ## Do your db stuff my $dbh = DBI->connect('dbi:mysql:alarm:databasehost', +'username','password') or die "Connection Error: $DBI::errstr\n"; my $sql = "update Showcases set OK_OPEN=0 WHERE SC_NUM + = ?"; my $sth = $dbh->prepare($sql) or die "Can't prepare $s +ql: $dbh->errstr\n"; $sth->execute($showcasenum) or die "SQL Error: $DBI::e +rrstr\n"; $dbh->disconnect; ## remove the pointer for this showcase delete $showcase{ showcasenum }; } ## and remove the expired time from the hash delete $when{ $time }; } } }

Does that make sense? Does it cover all the bases?

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re^7: ithreads memory leak by BrowserUk
in thread ithreads memory leak by DNAb

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?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (2)
    As of 2020-02-23 14:40 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (102 votes). Check out past polls.