slartyblartfast (slartyblartfast) wrote in changelog,
slartyblartfast
slartyblartfast
changelog

[livejournal] r16947: LJSUP-6332: E-mail system update

Committer: mchernyshev
LJSUP-6332: E-mail system update
U   trunk/bin/upgrading/update-db-general.pl
A   trunk/bin/worker/find-sendmail-problems
A   trunk/cgi-bin/LJ/User/Email.pm
Modified: trunk/bin/upgrading/update-db-general.pl
===================================================================
--- trunk/bin/upgrading/update-db-general.pl	2010-07-07 05:23:54 UTC (rev 16946)
+++ trunk/bin/upgrading/update-db-general.pl	2010-07-07 08:47:53 UTC (rev 16947)
@@ -4235,4 +4235,12 @@
     }
 });
 
+register_tablecreate("send_email_errors", <<'EOC');
+CREATE TABLE send_email_errors (
+  email VARCHAR(50) NOT NULL DEFAULT '',
+  time DATETIME DEFAULT NULL,
+  PRIMARY KEY (email)
+)
+EOC
+
 1; # return true

Added: trunk/bin/worker/find-sendmail-problems
===================================================================
--- trunk/bin/worker/find-sendmail-problems	                        (rev 0)
+++ trunk/bin/worker/find-sendmail-problems	2010-07-07 08:47:53 UTC (rev 16947)
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+use strict;
+
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljlib.pl';
+
+LJ::NewWorker::Manual::SendEmailErrorsFind->start();
+
+package LJ::NewWorker::Manual::SendEmailErrorsFind;
+use base 'LJ::NewWorker::Manual';
+
+use LJ::User::Email;
+
+sub work {
+    my %u_ids = LJ::User::Email->get_user_ids();
+    foreach my $email (keys %u_ids) {
+        my $complete = 1; # reset this in case of any error with one of user for this email
+        foreach my $uid (@{$u_ids{$email}}) {
+            my $u = LJ::want_user($uid);
+            if (LJ::isu($u)) {
+                if ($u->is_validated) {
+                    LJ::update_user($u, { status => 'N' });
+                    if ($u->is_validated) {
+                        $complete = 0;
+                    } else {
+                        print "user with id $uid and email <$email> validation has been revoked.\n" if LJ::NewWorker->verbose;
+                    }
+                } else { # user was not validated
+                    $complete = 0;
+                }
+            } else { # cannot find user for this uid.
+                $complete = 0;
+            }
+        }
+        LJ::User::Email->mark(undef, $email) if $complete;
+    }
+
+    return 0;
+}
+
+sub on_idle {
+    return if $LJ::IS_DEV_SERVER;
+    sleep 600;
+}
+
+1;


Property changes on: trunk/bin/worker/find-sendmail-problems
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/cgi-bin/LJ/User/Email.pm
===================================================================
--- trunk/cgi-bin/LJ/User/Email.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/User/Email.pm	2010-07-07 08:47:53 UTC (rev 16947)
@@ -0,0 +1,87 @@
+package LJ::User::Email;
+use strict;
+
+# status_code:
+#   undef   - OK: forget about problems with this address
+#   0       - cannot connect to MX-host or email domain.
+#   5xx     - smtp-status
+#
+sub mark {
+    my $self        = shift;
+    my $status_code = shift;
+    my $emails      = shift;    # One email if scalar or list of emails if array ref.
+
+    return if $LJ::DISABLED{'revoke_validation_on_errors'};
+
+    if ('ARRAY' eq ref $emails) {
+        foreach my $email (@$emails) {
+            $self->_log_one_email_status($status_code, $email);
+        }
+    } else {
+        $self->_log_one_email_status($status_code, $emails);
+    }
+}
+
+sub _log_one_email_status {
+    my $self        = shift;
+    my $status_code = shift;
+    my $email       = shift;
+
+    eval {  # Don't die if somthing wrong with database.
+        my $dbh = LJ::get_db_writer();
+        if (defined $status_code) {
+            $dbh->do("INSERT IGNORE INTO send_email_errors (email, time) VALUES (?, NOW())",
+            undef, $email);
+        } else { # undef: OK, remove row if any
+            $dbh->do("DELETE FROM send_email_errors WHERE email = ?", undef, $email);
+        }
+    };
+}
+
+sub get_marked {
+    my $self    = shift;
+    my %opts    = @_;
+
+    return () if $LJ::DISABLED{'revoke_validation_on_errors'};
+
+    my $limit   = $opts{limit}  || 1000;
+    my $timeout = $opts{timeout}|| 72;
+
+    my $dbh = LJ::get_db_reader();
+    return $dbh->selectcol_arrayref(qq{
+        SELECT email
+        FROM send_email_errors
+        WHERE (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(time))/3600 > $timeout
+        ORDER BY last_time DESC
+        LIMIT $limit
+    });
+}
+
+sub get_user_ids {
+    my $self    = shift;
+    my %opts    = @_;
+
+    return undef if $LJ::DISABLED{'revoke_validation_on_errors'};
+
+    my $dbh = LJ::get_db_reader();
+
+    my $emails = $self->get_marked(%opts);
+    my %user_ids = ();
+
+    foreach my $email (@$emails) {
+        my $userids = $dbh->selectcol_arrayref(qq{
+            SELECT userid
+            FROM email
+            WHERE email = ?
+        }, undef, $email);
+
+        foreach my $userid (@$userids) {
+            push @{$user_ids{$email}}, $userid;
+        }
+    }
+
+    return %user_ids;
+}
+
+1;
+

Tags: livejournal, pl, pm, slartyblartfast
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments