3 # Taken from an example script to use DBD::Pg to listen for async notifications
4 # by andrew at supernews.net, but don't bother emailing me about it;
5 # try #PostgreSQL on irc.freenode.net but read all the relevent
12 my $database_password;
14 my $forge_get_config = "/opt/gforge/utils/forge_get_config";
15 my $jobs_dir = "/opt/gforge/cronjobs";
17 chomp($database_name = `$forge_get_config database_name`);
18 chomp($database_user = `$forge_get_config database_user`);
19 chomp($database_password = `$forge_get_config database_password`);
21 my $dbh = DBI->connect("dbi:Pg:dbname=$database_name", $database_user, $database_password,
22 { RaiseError => 1, AutoCommit => 1 });
24 $dbh->do("LISTEN create_scm_repos;");
25 $dbh->do("LISTEN scm_update;");
27 $sth=$dbh->prepare("INSERT INTO cron_history (rundate,job,output) values (?, ?, ?)");
31 my $notifies = $dbh->func('pg_notifies');
34 # No notifications received. So sleep waiting for data on the backend connection.
35 my $fd = $dbh->func('getfd');
38 my $n = select($rfds, undef, undef, 30);
40 $notifies = $dbh->func('pg_notifies');
44 # the result from pg_notifies is a ref to a two-element array,
45 # with the notification name and the sender's backend PID.
46 my ($n,$p) = @$notifies;
47 $output = "Running: $jobs_dir/$n.php $p\n";
48 $output .= `$jobs_dir/$n.php 2>&1`;
50 $sth->execute(time(),903,$output);
52 $notifies = $dbh->func('pg_notifies');