]> git.sesse.net Git - skvidarsync/blobdiff - bin/sync.pl
Make do with only one commit per sync.
[skvidarsync] / bin / sync.pl
index 7e0317eb9550f8e431102fc54b5b4ce08bf6b71d..8ca281d6c195591afc22e62b7c0043a3b7b675d0 100644 (file)
@@ -10,12 +10,18 @@ use POSIX;
 use Time::HiRes;
 use IO::Select;
 use Unicode::Collate;
+use IO::Socket::SSL;
 binmode STDOUT, ':utf8';
 binmode STDERR, ':utf8';
 use utf8;
 
 require '../include/config.pm';
 
+my $global_ctx = IO::Socket::SSL::SSL_Context->new(
+       SSL_session_cache_size => 100,  # Probably overkill.
+);
+IO::Socket::SSL::set_default_context($global_ctx);
+
 my @log = ();
 my $uca = Unicode::Collate->new(level => 1);
 
@@ -229,7 +235,6 @@ sub get_group_assignments {
 sub update_assignment_db {
        my ($dbh, $channel, $ts, $assignments) = @_;
 
-       local $dbh->{AutoCommit} = 0;
        my %db_assignments = ();
        my $q = $dbh->prepare('SELECT name,group_name FROM current_group_membership_history WHERE channel=? AND ts=?');
        $q->execute($channel, $ts);
@@ -250,7 +255,6 @@ sub update_assignment_db {
                        $q->execute($channel, $ts, $name, undef);
                }
        }
-       $dbh->commit;
 }
 
 sub get_spreadsheet_with_title {
@@ -477,19 +481,21 @@ sub db_connect {
        if (!defined($dbh)) {
                return undef;
        }
+       $dbh->{AutoCommit} = 0;
        $dbh->do('LISTEN skvupdate') or return undef;
        return $dbh;
 }
 
 sub run {
-       my $dbh = shift;
+       my ($dbh, $ua) = @_;
        my $total_start = [Time::HiRes::gettimeofday];
 
        @log = ();
        skv_log("Siste sync startet: " . POSIX::ctime(time));
 
-       # Initialize the handles we need for communication.
-       my $ua = LWP::UserAgent->new('SKVidarLang/1.0');
+       # For the logic on the “applied” table below.
+       $dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
+
        my $token = get_oauth_bearer_token($dbh, $ua);
 
        # Find the newest message, what it is linked to, and what was the one before it (for group diffing).
@@ -560,7 +566,9 @@ sub run {
        my %assignments = get_group_assignments($main_sheet_json);
        update_assignment_db($dbh, $config::invitation_channel, $invitation_ts, \%assignments);
 
+       $start = [Time::HiRes::gettimeofday];
        my %seen_names = find_where_each_name_is($main_sheet_json);
+       log_timing($start, "Making sort key reverse mapping");
 
        # Find duplicates.
        for my $name (sort keys %seen_names) {
@@ -714,8 +722,6 @@ sub run {
 
        # Find the list of names we already marked yellow.
        my %have_colors = ();
-       $dbh->{AutoCommit} = 0;
-       $dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
        $q = $dbh->prepare('SELECT name,color FROM applied WHERE channel=? AND ts=?');
        $q->execute($config::invitation_channel, $invitation_ts);
        while (my $ref = $q->fetchrow_hashref) {
@@ -799,10 +805,12 @@ sub run {
        printf "Tok %.0f ms.\n", 1e3 * $elapsed;
 }
 
+# Initialize the handles we need for communication.
 my $dbh = db_connect() or die;
+my $ua = LWP::UserAgent->new(agent => 'SKVidarLang/1.0', keep_alive => 50);
 if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
        # Start with a single, forced run.
-       run($dbh);
+       run($dbh, $ua);
 
        while (1) {
                while (!defined($dbh)) {
@@ -821,9 +829,7 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
                }
                if (scalar @ready > 0) {  
                        eval {
-                               $dbh->{AutoCommit} = 1;
-                               run($dbh);
-                               $dbh->commit;
+                               run($dbh, $ua);
                        };
                        if ($@) {
                                warn "Died with: $@";
@@ -833,10 +839,8 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
        }
 } elsif ($#ARGV >= 0 && $ARGV[0] eq '--benchmark') {
        for my $i (0..9) {
-               $dbh->{AutoCommit} = 1;
-               run($dbh);
-               $dbh->commit;
+               run($dbh, $ua);
        }
 } else {
-       run($dbh);
+       run($dbh, $ua);
 }