summaryrefslogtreecommitdiff
path: root/utils/notify_short/listener.pl
blob: 05b5afd0bd595c9249cb68d8f5e0d50967705124 (plain)
  1. #!/usr/bin/perl
  2. # This is the SL-Short listener. It listens for the "parts_short" signal and
  3. # when the signal comes in, prepares a list of short parts to be sent to
  4. # at least one person.
  5. #
  6. # By Chris Travers, Metatron Technology Consulting
  7. # chris@metatrontech.com
  8. #
  9. # Released under the GNU GPL v2.0 or later. See included GPL.txt for more
  10. # information.
  11. require "config.pl";
  12. use DBI;
  13. my $dsn = "dbi:Pg:dbname=$database";
  14. my $dbh = DBI->connect(
  15. $dsn, $db_user,
  16. $db_passwd,
  17. {
  18. AutoCommit => 1,
  19. PrintError => 0,
  20. RaiseError => 1,
  21. }
  22. );
  23. my $sth;
  24. $dbh->do("LISTEN parts_short");
  25. while (1) { # loop infinitely
  26. if ( $dbh->func('pg_notifies') ) {
  27. &on_notify;
  28. }
  29. sleep $cycle_delay;
  30. }
  31. sub on_notify {
  32. open( MAIL, "|-", $sendmail );
  33. $sth = $dbh->prepare( "
  34. SELECT partnumber, description, onhand, rop FROM parts
  35. WHERE onhand <= rop
  36. " );
  37. $sth->execute;
  38. print MAIL $template_top;
  39. while ( ( $partnumber, $description, $avail, $rop ) = $sth->fetchrow_array )
  40. {
  41. write MAIL;
  42. }
  43. print MAIL $template_foot;
  44. close MAIL;
  45. }