]> git.sesse.net Git - skvidarsync/blobdiff - bin/sync.pl
Fix picking up the date from new messages.
[skvidarsync] / bin / sync.pl
index 7b82cace50b6cd781361a8b2b6a94502ff6121a9..2d315c9d2a646f7448d82b03db5f65b946f02ff4 100644 (file)
@@ -52,9 +52,28 @@ sub log_timing {
        printf "%s: %.0f ms.\n", $msg, 1e3 * $elapsed;
 }
 
+# Unicode::Collate is seemingly slow, so add a cache for each name part
+# (which, of course, only works for equality). Helps especially in
+# --daemon mode, where even the first request gets a warm cache.
+my %sort_key_cache = ();
+my $sort_key_sp = $uca->getSortKey(' ');
+
 sub sort_key {
        my $m = shift;
-       return $uca->getSortKey($m);
+       my $sk;
+       for my $part (split /\s+/, $m) {
+               my $psk = \$sort_key_cache{$part};
+               if (!defined($$psk)) {
+                       $$psk = $uca->getSortKey($part);
+               }
+               if (defined($sk)) {
+                       $sk .= $sort_key_sp;
+                       $sk .= $$psk;
+               } else {
+                       $sk = $$psk;
+               }
+       }
+       return $sk;
 }
 
 sub get_oauth_bearer_token {
@@ -132,7 +151,7 @@ sub matches_name {
        if (scalar @$ap >= 2 && scalar @bp >= 2 && $ap->[0] eq $bp[0]) {
                # First name matches, try to match some surname
                my $found = 0;
-               for my $ai (1..(scalar @$ap)) {
+               for my $ai (1..(scalar @$ap - 1)) {
                        for my $bi (1..$#bp) {
                                $found = 1 if ($ap->[$ai] eq $bp[$bi]);
                        }
@@ -560,12 +579,19 @@ sub run {
        log_timing($start, "/spreadsheets/");
 
        my $sheets_json = JSON::XS::decode_json($response->decoded_content);
+       if (!exists($sheets_json->{'sheets'})) {
+               die "Missing sheets (error response?): " . $response->decoded_content;
+       }
        my $main_sheet_json = $sheets_json->{'sheets'}[0];
        my $mapping_sheet_json = $sheets_json->{'sheets'}[1];
 
        # Update the list of groups we've seen people in.
+       $start = [Time::HiRes::gettimeofday];
        my %assignments = get_group_assignments($main_sheet_json);
+       log_timing($start, "Parsing group assignments");
+       $start = [Time::HiRes::gettimeofday];
        update_assignment_db($dbh, $config::invitation_channel, $invitation_ts, \%assignments);
+       log_timing($start, "Updating assignments in database");
 
        $start = [Time::HiRes::gettimeofday];
        my %seen_names = find_where_each_name_is($main_sheet_json);
@@ -652,7 +678,7 @@ sub run {
                                        }
                                }
                        }
-                       log_timing($start, "Fuzzy-searching for Slack name “$slack_name”");
+                       log_timing($start, "Fuzzy-searching for Slack name $slack_name");
                        if ($#candidates == -1) {
                                skv_log("$slack_name ($userid) er påmeldt på Slack, men fant ikke et regneark-navn for dem.");
                                possibly_nag_user($dbh, $ua, $userid, $invitation_ts, undef, \%slack_userid_to_slack_name);
@@ -805,6 +831,7 @@ sub run {
 
        my $elapsed = Time::HiRes::tv_interval($total_start);
        printf "Tok %.0f ms.\n", 1e3 * $elapsed;
+       print "\n";
 }
 
 # Initialize the handles we need for communication.
@@ -815,13 +842,13 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
        run($dbh, $ua);
 
        while (1) {
-               while (!defined($dbh)) {
+               while (!defined($dbh) || !$dbh->ping) {
                        print STDERR "Database connection lost, reconnecting...\n";
                        sleep 1;
                        $dbh = db_connect();
                }
                my $s = IO::Select->new($dbh->{pg_socket});
-               my @ready = $s->can_read(10.0);
+               my @ready = $s->can_read(150.0);  # slack.com HTTP timeout is ~3 minutes, sheets.googleapis.com is ~4 minutes.
                my @exceptions = $s->has_exception(0.0);
 
                if (scalar @exceptions > 0) {
@@ -837,6 +864,20 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
                                warn "Died with: $@";
                                $dbh = undef;
                        }
+               } else {
+                       # Keep the connections alive and the token in the database fresh.
+                       # (The two URLs we use don't really exist. Note that the first time,
+                       # we might be making the initial connection to slack.com, since it's
+                       # not a given that run() needed to talk to them.)
+                       get_oauth_bearer_token($dbh, $ua);
+                       $dbh->commit;
+                       #my $start = [Time::HiRes::gettimeofday];
+                       $ua->get('https://sheets.googleapis.com/ping');
+                       #log_timing($start, 'sheets.googleapis.com (keepalive)');
+                       #$start = [Time::HiRes::gettimeofday];
+                       $ua->get('https://slack.com/api/ping');
+                       #log_timing($start, 'slack.com (keepalive)');
+                       #print STDERR "\n";
                }
        }
 } elsif ($#ARGV >= 0 && $ARGV[0] eq '--benchmark') {