summaryrefslogtreecommitdiff
path: root/utils/notify_short/listener.pl
blob: 5662cdb546e4d73376f878ab7d41e38e98705766 (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. $dbh->{pg_enable_utf8} = 1;
  24. my $sth;
  25. $dbh->do("LISTEN parts_short");
  26. while (1) { # loop infinitely
  27. if ( $dbh->func('pg_notifies') ) {
  28. &on_notify;
  29. }
  30. sleep $cycle_delay;
  31. }
  32. sub on_notify {
  33. open( MAIL, "|-", $sendmail );
  34. $sth = $dbh->prepare( "
  35. SELECT partnumber, description, onhand, rop FROM parts
  36. WHERE onhand <= rop
  37. " );
  38. $sth->execute;
  39. print MAIL $template_top;
  40. while ( ( $partnumber, $description, $avail, $rop ) = $sth->fetchrow_array )
  41. {
  42. write MAIL;
  43. }
  44. print MAIL $template_foot;
  45. close MAIL;
  46. }