summaryrefslogtreecommitdiff
path: root/utils/notify_short/listener.pl
diff options
context:
space:
mode:
Diffstat (limited to 'utils/notify_short/listener.pl')
-rw-r--r--utils/notify_short/listener.pl46
1 files changed, 46 insertions, 0 deletions
diff --git a/utils/notify_short/listener.pl b/utils/notify_short/listener.pl
new file mode 100644
index 00000000..37f31d38
--- /dev/null
+++ b/utils/notify_short/listener.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+# This is the SL-Short listener. It listens for the "parts_short" signal and
+# when the signal comes in, prepares a list of short parts to be sent to
+# at least one person.
+#
+# By Chris Travers, Metatron Technology Consulting
+# chris@metatrontech.com
+#
+# Released under the GNU GPL v2.0 or later. See included GPL.txt for more
+# information.
+
+require "config.pl";
+
+use DBI;
+my $dsn = "dbi:Pg:dbname=$database";
+my $dbh = DBI->connect($dsn, $db_user, $db_passwd,
+ { AutoCommit => 1,
+ PrintError => 0,
+ RaiseError => 1, }
+);
+
+my $sth;
+
+$dbh->do("LISTEN parts_short");
+while (1){ # loop infinitely
+ if ($dbh->func ('pg_notifies')){
+ &on_notify;
+ }
+ sleep $cycle_delay;
+}
+sub on_notify {
+ open (MAIL, "| $sendmail");
+ $sth = $dbh->prepare("
+ SELECT partnumber, description, onhand, rop FROM parts
+ WHERE onhand <= rop
+ ");
+ $sth->execute;
+ print MAIL $template_top;
+ while (($partnumber, $description, $avail, $rop) = $sth->fetchrow_array){
+ write MAIL;
+ }
+ print MAIL $template_foot;
+ close MAIL;
+}
+