LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / DBUtil.pm
blob3ccabd3228bc45a39082959df846bfc9a5e88d83
1 package LJ::DBUtil;
2 use strict;
4 use lib "$ENV{LJHOME}/cgi-bin";
5 use LJ;
7 die "Don't use this in web context, it's only for admin scripts!"
8 if LJ::is_web_context();
10 sub get_inactive_db {
11 my $class = shift;
12 my $cid = shift or die "no cid passed\n";
13 my $verbose = shift;
15 print STDERR " - cluster $cid... " if $verbose;
17 my $role = LJ::get_inactive_role($cid);
18 return unless $role;
20 $LJ::DBIRole->clear_req_cache();
21 my $db = LJ::get_dbh($role);
22 if ($db) {
23 $db->{RaiseError} = 1;
25 return $db;
28 sub validate_clusters {
29 my $class = shift;
31 foreach my $cid (@LJ::CLUSTERS) {
32 unless (LJ::DBUtil->get_inactive_db($cid)) {
33 print STDERR " - found downed cluster: $cid (inactive side)\n";
34 print STDERR "Aborted. Please try again later.\n";
35 exit 0;
39 return 1;
42 ## get cluster DB handler, preferred order: inactive DB, active DB
43 sub connect_to_cluster {
44 my $class = shift;
45 my $clid = shift;
46 my $verbose = shift;
48 my $dbr = LJ::DBUtil->get_inactive_db($clid, $verbose);
49 unless ($dbr) {
50 warn "Using master database for cluster #$clid"
51 if $verbose;
52 $dbr = LJ::get_cluster_reader($clid);
55 die "Can't get DB connection for cluster #$clid"
56 unless $dbr;
57 $dbr->{RaiseError} = 1;
59 warn "Connected to cluster #$clid" if $verbose;
61 return $dbr;