commit 3ef5da75b8ced4010d6f7e977e714277dbcbfe1c
parent a9db6c3ebd64a26f593dfd3f766dd89c2182516f
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Sun, 9 Nov 2014 13:47:42 -0700
don't globally export the db handle
Diffstat:
6 files changed, 21 insertions(+), 24 deletions(-)
diff --git a/gen_index.pl b/gen_index.pl
@@ -9,6 +9,8 @@ use Template;
use shared;
+my $dbh = get_dbh();
+
my $config = {
INTERPOLATE => 1,
POST_CHOMP => 1,
diff --git a/gen_svg.pl b/gen_svg.pl
@@ -14,6 +14,7 @@ use shared;
#
# my $log = get_log("gen_svg");
+my $dbh = get_dbh();
my $svg_dir = "/var/www/htdocs/pricechart/svg";
mkdir $svg_dir;
diff --git a/price_scraper.pl b/price_scraper.pl
@@ -7,6 +7,7 @@ use shared;
my $ua = get_ua();
+my $dbh = get_dbh();
# pick the oldest product
my $cutoff = time - (30 * 24 * 60 * 60);
diff --git a/product_scraper.pl b/product_scraper.pl
@@ -11,6 +11,7 @@ use shared;
my $ua = get_ua();
+my $dbh = get_dbh();
srand;
$dbh->do("create table if not exists products(" .
diff --git a/search.pl b/search.pl
@@ -8,20 +8,15 @@ use Template;
use Proc::Daemon;
use shared;
-print "disconnecting dbh\n";
-$dbh->disconnect();
-my $pid_file = "/var/www/run/search.pid";
+my $pid_file = "/var/www/run/search.pid";
if (-e $pid_file) {
print "pid file $pid_file exists, search may already be running\n";
print "make sure that search is not running and remove\n";
exit
}
-print "daemonizing\n";
-
-
my $daemon = Proc::Daemon->new(
setuid => 67,
work_dir => "/var/www",
@@ -56,13 +51,7 @@ my $template = Template->new($config);
print "made new template config\n";
-my $dbh = DBI->connect(
- "dbi:SQLite:dbname=/var/www/db/pricechart.db",
- "",
- "",
- { RaiseError => 1 }
-) or die $DBI::errstr;
-
+my $dbh = get_dbh();
print "opened db\n";
my $sql = "select part_num, manufacturer, description from products " .
diff --git a/shared.pm b/shared.pm
@@ -10,7 +10,7 @@ use LWP::Simple;
use POSIX;
@ISA = ("Exporter");
-@EXPORT = qw(get_dom get_ua get_log vprint vprintf %args $cfg $dbh);
+@EXPORT = qw(get_dom get_ua get_log get_dbh vprint vprintf %args $cfg);
our %args;
@@ -51,16 +51,19 @@ my $parser = Config::Grammar->new({
our $cfg =$parser->parse($cfg_file) or die "error: $parser->{err}\n";
-my $db_dir = "/var/www/db";
-mkdir $db_dir;
-
-our $dbh = DBI->connect(
- "dbi:SQLite:dbname=$db_dir/pricechart.db",
- "",
- "",
- { RaiseError => 1 }
-) or die $DBI::errstr;
-
+sub get_dbh
+{
+ my $db_dir = "/var/www/db";
+ mkdir $db_dir;
+
+ my $dbh = DBI->connect(
+ "dbi:SQLite:dbname=$db_dir/pricechart.db",
+ "",
+ "",
+ { RaiseError => 1 }
+ ) or die $DBI::errstr;
+ return $dbh;
+}
sub get_dom
{