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);
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);
$q->execute($channel, $ts, $name, undef);
}
}
- $dbh->commit;
}
sub get_spreadsheet_with_title {
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).
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) {
# 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) {
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)) {
}
if (scalar @ready > 0) {
eval {
- $dbh->{AutoCommit} = 1;
- run($dbh);
- $dbh->commit;
+ run($dbh, $ua);
};
if ($@) {
warn "Died with: $@";
}
}
}
+} elsif ($#ARGV >= 0 && $ARGV[0] eq '--benchmark') {
+ for my $i (0..9) {
+ run($dbh, $ua);
+ }
} else {
- run($dbh);
+ run($dbh, $ua);
}