madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r19793: LJSUP-9307: Worker: Delayed entries

Committer: sbelyaev
LJSUP-9307: Worker: Delayed entries
A   branches/delayed_entries/bin/worker/delayed-entries-poster
A   branches/delayed_entries/cgi-bin/LJ/DelayedEntry/
A   branches/delayed_entries/cgi-bin/LJ/DelayedEntry/Scheduler.pm
Added: branches/delayed_entries/bin/worker/delayed-entries-poster
===================================================================
--- branches/delayed_entries/bin/worker/delayed-entries-poster	                        (rev 0)
+++ branches/delayed_entries/bin/worker/delayed-entries-poster	2011-08-19 10:16:18 UTC (rev 19793)
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use lib "$ENV{LJHOME}/cgi-bin";
+LJ::NewWorker::Manual::DelayedEntriesPoster->start();
+
+package LJ::NewWorker::Manual::DelayedEntriesPoster ;
+
+
+use strict;
+use warnings;
+use lib "$ENV{LJHOME}/cgi-bin";
+use base 'LJ::NewWorker::Manual';
+
+require 'ljlib.pl';
+require 'ljdb.pl';
+
+use LJ::DelayedEntry::Scheduler;
+
+sub work {
+    LJ::foreach_cluster( sub { LJ::DelayedEntry::Scheduler::on_pulse(@_); } );
+    return 1 if __PACKAGE__->should_quit;
+    return 0;
+}
+
+sub on_idle {
+    sleep LJ::DelayedEntry::Scheduler::pulse_time();
+;
+}
+


Property changes on: branches/delayed_entries/bin/worker/delayed-entries-poster
___________________________________________________________________
Added: svn:executable
   + *

Added: branches/delayed_entries/cgi-bin/LJ/DelayedEntry/Scheduler.pm
===================================================================
--- branches/delayed_entries/cgi-bin/LJ/DelayedEntry/Scheduler.pm	                        (rev 0)
+++ branches/delayed_entries/cgi-bin/LJ/DelayedEntry/Scheduler.pm	2011-08-19 10:16:18 UTC (rev 19793)
@@ -0,0 +1,82 @@
+package LJ::DelayedEntry::Scheduler;
+use LJ::DelayedEntry;
+use LJ::Text;
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $PULSE_TIME = 1 * 10;
+
+sub pulse_time {
+    return $PULSE_TIME;
+}
+
+sub __load_delayed_entries {
+    my ($dbh) = @_;
+    my @entries;
+    
+    my $list = $dbh->selectall_arrayref("SELECT journalid, delayedid, posterid " .
+                                     "FROM delayedlog2 ".
+                                     "WHERE posttime <= NOW()");
+
+    foreach my $tuple (@$list) {
+        push @entries, LJ::DelayedEntry->load_data($dbh,
+                                                   { journalid  => $tuple->[0],
+                                                     delayed_id => $tuple->[1],
+                                                     posterid   => $tuple->[2]} );
+       
+    }
+    return \@entries;
+}
+
+
+sub __send_error {
+    my ($poster, $subject, $error) = @_;
+    my $email = $poster->email_raw;
+    
+    LJ::send_mail({
+        'to' => $email,
+        'from' => $LJ::ADMIN_EMAIL,
+        'fromname' => $LJ::SITENAME,
+        'charset' => 'utf-8',
+        'subject' => LJ::Lang::get_text($poster->prop('browselang'),
+                                        'email.delayed_error.subject'),
+        'body' =>  LJ::Lang::get_text($poster->prop('browselang'),
+                                        'email.delayed_error.body',
+        {subject => $subject, reason=>$error}),
+    });    
+}
+
+
+sub on_pulse {
+    my ($clusterid, $dbh) = @_;
+    __assert($dbh);
+
+    my $entries = __load_delayed_entries($dbh);
+    foreach my $entry(@$entries) {
+        my $post_status = $entry->convert($clusterid);
+        
+        # do we need to send error
+        if ( $post_status->{error_message} ) {
+            __send_error($entry->poster, 
+                         $entry->data->{subject},
+                         $post_status->{error_message});
+        }
+        
+        if ( $post_status->{delete_entry} ) {
+            $entry->delete();
+        }
+    }
+    
+}
+
+
+sub __assert() {
+    my ($statement) = @_;
+    unless ($statement) {
+        die "assertion failed!";
+    }
+}
+
+1;

Tags: livejournal, madeon, pm
  • 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 

  • 0 comments