summaryrefslogtreecommitdiff
path: root/utils/notify_short/listener.pl
blob: a92867956bcdb0e4c642817e3b62972968984ffc (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($dsn, $db_user, $db_passwd,
  15. { AutoCommit => 1,
  16. PrintError => 0,
  17. RaiseError => 1, }
  18. );
  19. my $sth;
  20. $dbh->do("LISTEN parts_short");
  21. while (1){ # loop infinitely
  22. if ($dbh->func ('pg_notifies')){
  23. &on_notify;
  24. }
  25. sleep $cycle_delay;
  26. }
  27. sub on_notify {
  28. open (MAIL, '|-', "$sendmail");
  29. $sth = $dbh->prepare("
  30. SELECT partnumber, description, onhand, rop FROM parts
  31. WHERE onhand <= rop
  32. ");
  33. $sth->execute;
  34. print MAIL $template_top;
  35. while (($partnumber, $description, $avail, $rop) = $sth->fetchrow_array){
  36. write MAIL;
  37. }
  38. print MAIL $template_foot;
  39. close MAIL;
  40. }