-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.pl
executable file
·173 lines (142 loc) · 3.65 KB
/
fetch.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/perl
# Fetch daemon.
use strict;
use warnings;
use threads;
use threads::shared;
use Fcntl qw/LOCK_EX/;
use IO::Socket::INET;
use IO::Handle;
use CSplat::Util qw/run_service/;
use CSplat::Config;
use CSplat::Ttyrec qw/clear_cached_urls fetch_ttyrecs/;
use CSplat::Seek qw/tty_frame_offset/;
use CSplat::Xlog qw/xlog_hash xlog_str desc_game/;
use POSIX;
my $MAX_REQUEST_COUNT = 279;
my $live_thread_count :shared = 0;
my $lastsync;
local $| = 1;
main();
sub main {
run_autovacuum();
run_service('fetch', \&run_fetch);
}
sub run_autovacuum {
my $pid = fork;
return if $pid;
exec("perl -I. vacuum.pl");
exit 0;
}
sub run_fetch {
my $fetch_port = CSplat::Config::fetch_port();
my $server = IO::Socket::INET->new(LocalPort => $fetch_port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5)
or die "Couldn't open server socket on $fetch_port: $@\n";
# Since the shared thread variables seem to leak, set a max request count.
my $max_requests = $MAX_REQUEST_COUNT;
while ((my $client = $server->accept()) && $max_requests-- > 0) {
++$live_thread_count;
my $thread = threads->new(sub {
eval {
process_command($client);
};
--$live_thread_count if $live_thread_count > 0;
warn "$@" if $@;
});
$thread->detach;
}
if ($max_requests < 0) {
print "Fulfilled $MAX_REQUEST_COUNT requests, exiting\n";
}
$server->close();
my $wait_max = 180;
my $waited = 0;
my $wait_interval = 2;
while ($live_thread_count > 0 && $waited < $wait_max) {
print "Waiting for fetch threads to exit\n";
sleep $wait_interval;
$waited += $wait_interval;
}
if ($live_thread_count == 0) {
print "All threads completed, shutting down.\n";
} else {
print "Force-shut down after $waited s, ignoring $live_thread_count rogue threads\n";
}
}
sub process_command {
my $client = shift;
my $command = <$client>;
chomp $command;
my ($cmd) = $command =~ /^(\w+)/;
return unless $cmd;
if ($cmd eq 'G') {
my ($game) = $command =~ /^\w+ (.*)/;
my $g = xlog_hash($game);
my $have_cache = CSplat::Ttyrec::have_cached_listing_for_game($g);
my $res;
eval {
$res = fetch_game($client, $game)
};
warn "$@" if $@;
if ($@ && $have_cache) {
CSplat::Ttyrec::clear_cached_urls_for_game($g);
eval {
$res = fetch_game($client, $game);
};
warn "$@" if $@;
}
if ($@) {
print $client "FAIL $@\n";
}
$res
}
elsif ($cmd eq 'CLEAR') {
clear_cached_urls();
print $client "OK\n";
}
}
sub fetch_notifier {
my ($client, $g, @event) = @_;
my $text = join(" ", @event);
eval {
print $client "S $text\n";
};
warn $@ if $@;
}
sub fetch_game {
my ($client, $g) = @_;
print "Requested download: $g\n";
my $listener = sub {
fetch_notifier($client, $g, @_);
};
$g = xlog_hash($g);
my $start = $g->{start};
my $nocheck = $g->{nocheck};
delete $g->{nocheck};
delete @$g{qw/start nostart/} if $g->{nostart};
my $result = fetch_ttyrecs($listener, $g, $nocheck);
$g->{start} = $start;
if ($result) {
my $dejafait = $g->{id};
if ($dejafait) {
print "Not redownloading ttyrecs for ", desc_game($g), "\n";
}
else {
print "Downloaded ttyrecs for ", desc_game($g), " ($g->{ttyrecs})\n";
}
if ($@) {
warn $@;
CSplat::DB::delete_game($g);
print $client "FAIL $@\n";
}
else {
print $client "OK " . xlog_str($g, 1) . "\n";
}
} else {
print "Failed to download ", desc_game($g), "\n";
die "Failed to download game\n";
}
}