summaryrefslogtreecommitdiff
path: root/lib/Multi/Maintenance.pm
blob: e7c66a0481926cbdfc6497f6e7771a5a03b0fcbc (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

#
#  Multi::Maintenance  -  General maintenance functions
#

package Multi::Maintenance;

use strict;
use warnings;
use POE;
use PerlIO::gzip;
use VNDBUtil 'normalize_titles';


sub spawn {
  my $p = shift;
  POE::Session->create(
    package_states => [
      $p => [qw|
        _start shutdown set_daily daily set_monthly monthly log_stats
        vncache_inc tagcache traitcache vnpopularity vnrating cleangraphs cleansessions cleannotifications rmuncomfirmusers
        vncache_full usercache statscache logrotate
        vnsearch_check vnsearch_gettitles vnsearch_update
      |],
    ],
    heap => {
      daily => [qw|vncache_inc tagcache traitcache vnpopularity vnrating cleangraphs cleansessions cleannotifications rmuncomfirmusers|],
      monthly => [qw|vncache_full usercache statscache logrotate|],
      vnsearch_checkdelay => 3600,
      @_,
    },
  );
}


sub _start {
  $_[KERNEL]->alias_set('maintenance');
  $_[KERNEL]->sig(shutdown => 'shutdown');
  $_[KERNEL]->yield('set_daily');
  $_[KERNEL]->yield('set_monthly');
  $_[KERNEL]->yield('vnsearch_check');
  $_[KERNEL]->post(pg => listen => vnsearch => 'vnsearch_check');
}


sub shutdown {
  $_[KERNEL]->delay('daily');
  $_[KERNEL]->delay('monthly');
  $_[KERNEL]->delay('vnsearch_check');
  $_[KERNEL]->alias_remove('maintenance');
}


sub set_daily {
  # run daily each day at 0:00 GMT
  # (GMT because we're calculating on the UNIX timestamp, I can easily add an
  #  offset if necessary, but it doesn't really matter what time this cron
  #  runs, as long as it's run on a daily basis)
  $_[KERNEL]->alarm(daily => int((time+3)/86400+1)*86400);
}


sub daily {
  $_[KERNEL]->call(core => log => 'Running daily cron: %s', join ', ', @{$_[HEAP]{daily}});

  # dispatch events that need to be run on a daily basis
  $_[KERNEL]->call($_[SESSION], $_) for (@{$_[HEAP]{daily}});

  # re-activate timer
  $_[KERNEL]->call($_[SESSION], 'set_daily');
}


sub set_monthly {
  # Calculate the UNIX timestamp of 0:00 GMT of the first day of the next month.
  # We do this by simply incrementing the timestamp with one day and checking gmtime()
  # for a month change. This might not be very reliable, but should be enough for
  # our purposes.
  my $nextday = int((time+3)/86400+1)*86400;
  my $thismonth = (gmtime)[5]*100+(gmtime)[4]; # year*100 + month, for easy comparing
  $nextday += 86400 while (gmtime $nextday)[5]*100+(gmtime $nextday)[4] <= $thismonth;
  $_[KERNEL]->alarm(monthly => $nextday);
}


sub monthly {
  $_[KERNEL]->call(core => log => 'Running monthly cron: %s', join ', ', @{$_[HEAP]{monthly}});

  # dispatch events that need to be run on a monthly basis
  $_[KERNEL]->call($_[SESSION], $_) for (@{$_[HEAP]{monthly}});

  # re-activate timer
  $_[KERNEL]->call($_[SESSION], 'set_monthly');
}


sub log_stats { # num, res, action, time
  $_[KERNEL]->call(core => log => sprintf 'Finished %s in %.3fs (%d rows)', $_[ARG2], $_[ARG3], $_[ARG0]);
}


#
#  D A I L Y   J O B S
#


sub vncache_inc {
  # takes about 50ms to 1s to complete, depending on how many
  # releases have been released within the past 5 days
  $_[KERNEL]->post(pg => do => q|
    SELECT update_vncache(id)
      FROM (
        SELECT DISTINCT rv.vid
          FROM releases r
          JOIN releases_rev rr ON rr.id = r.latest
          JOIN releases_vn rv ON rv.rid = r.latest
         WHERE rr.released  > TO_CHAR(NOW() - '5 days'::interval, 'YYYYMMDD')::integer
           AND rr.released <= TO_CHAR(NOW(), 'YYYYMMDD')::integer
     ) AS r(id)
  |, undef, 'log_stats', 'vncache_inc');
}


sub tagcache {
  # takes about 5 seconds max, still OK
  $_[KERNEL]->post(pg => do => 'SELECT tag_vn_calc()', undef, 'log_stats', 'tagcache');
}


sub traitcache {
  # still takes less than a second
  $_[KERNEL]->post(pg => do => 'SELECT traits_chars_calc()', undef, 'log_stats', 'traitcache');
}


sub vnpopularity {
  # takes a bit more than 8 seconds, meh...
  $_[KERNEL]->post(pg => do => 'SELECT update_vnpopularity()', undef, 'log_stats', 'vnpopularity');
}


sub vnrating {
  # takes less than a second, but can be performed in ranges as well when necessary
  $_[KERNEL]->post(pg => do => q|
    UPDATE vn SET
      c_rating = (SELECT (
          ((SELECT COUNT(vote)::real/COUNT(DISTINCT vid)::real FROM votes)*(SELECT AVG(a)::real FROM (SELECT AVG(vote) FROM votes GROUP BY vid) AS v(a)) + SUM(vote)::real) /
          ((SELECT COUNT(vote)::real/COUNT(DISTINCT vid)::real FROM votes) + COUNT(uid)::real)
        ) FROM votes WHERE vid = id AND uid NOT IN(SELECT id FROM users WHERE ign_votes)
      ),
      c_votecount = COALESCE((SELECT count(*) FROM votes WHERE vid = id AND uid NOT IN(SELECT id FROM users WHERE ign_votes)), 0)
  |, undef, 'log_stats', 'vnrating');
}


sub cleangraphs {
  # should be pretty fast
  $_[KERNEL]->post(pg => do => q|
    DELETE FROM relgraphs vg
     WHERE NOT EXISTS(SELECT 1 FROM vn WHERE rgraph = vg.id)
       AND NOT EXISTS(SELECT 1 FROM producers WHERE rgraph = vg.id)
    |, undef, 'log_stats', 'cleangraphs');
}


sub cleansessions {
  $_[KERNEL]->post(pg => do =>
    q|DELETE FROM sessions WHERE lastused < NOW()-'1 month'::interval|,
    undef, 'log_stats', 'cleansessions');
}


sub cleannotifications {
  $_[KERNEL]->post(pg => do =>
    q|DELETE FROM notifications WHERE read < NOW()-'1 month'::interval|,
    undef, 'log_stats', 'cleannotifications');
}


sub rmuncomfirmusers {
  $_[KERNEL]->post(pg => do =>
    q|DELETE FROM users WHERE NOT email_confirmed AND registered < NOW()-'1 week'::interval|,
    undef, 'log_stats', 'rmunconfirmusers');
}



#
#  M O N T H L Y   J O B S
#


sub vncache_full {
  # this takes more than a minute to complete, and should only be necessary in the
  # event that the daily vncache_inc cron hasn't been running for 5 subsequent days.
  $_[KERNEL]->post(pg => do => 'SELECT update_vncache(id) FROM vn', undef, 'log_stats', 'vncache_full');
}


sub usercache {
  # Shouldn't really be necessary, except c_changes could be slightly off when hiding/unhiding DB items
  # Currently takes about 25 seconds to complete.
  $_[KERNEL]->post(pg => do => q|UPDATE users SET
    c_votes = COALESCE(
      (SELECT COUNT(vid)
      FROM votes
      WHERE uid = users.id
      GROUP BY uid
    ), 0),
    c_changes = COALESCE(
      (SELECT COUNT(id)
      FROM changes
      WHERE requester = users.id
      GROUP BY requester
    ), 0),
    c_tags = COALESCE(
      (SELECT COUNT(tag)
      FROM tags_vn
      WHERE uid = users.id
      GROUP BY uid
    ), 0)
  |, undef, 'log_stats', 'usercache');
}


sub statscache {
  # Shouldn't really be necessary, the triggers in PgSQL should keep these up-to-date nicely.
  # But it takes less than 100ms to complete, anyway
  $_[KERNEL]->post(pg => do => $_) for(
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM users)-1 WHERE section = 'users'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM vn        WHERE hidden = FALSE) WHERE section = 'vn'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM releases  WHERE hidden = FALSE) WHERE section = 'releases'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM producers WHERE hidden = FALSE) WHERE section = 'producers'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM chars     WHERE hidden = FALSE) WHERE section = 'chars'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM tags      WHERE state = 2)      WHERE section = 'tags'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM traits    WHERE state = 2)      WHERE section = 'traits'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM threads   WHERE hidden = FALSE) WHERE section = 'threads'|,
    q|UPDATE stats_cache SET count = (SELECT COUNT(*) FROM threads_posts WHERE hidden = FALSE
        AND EXISTS(SELECT 1 FROM threads WHERE threads.id = tid AND threads.hidden = FALSE)) WHERE section = 'threads_posts'|
  );
}


sub logrotate {
  my $dir = sprintf '%s/old', $VNDB::M{log_dir};
  mkdir $dir if !-d $dir;

  for (glob sprintf '%s/*', $VNDB::M{log_dir}) {
    next if /^\./ || /~$/ || !-f;
    my $f = /([^\/]+)$/ ? $1 : $_;
    my $n = sprintf '%s/%s.%04d-%02d-%02d.gz', $dir, $f, (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3];
    if(-f $n) {
      $_[KERNEL]->call(core => log => 'Logs already rotated earlier today!');
      return;
    }
    open my $I, '<', sprintf '%s/%s', $VNDB::M{log_dir}, $f;
    open my $O, '>:gzip', $n;
    print $O $_ while <$I>;
    close $O;
    close $I;
    open $I, '>', sprintf '%s/%s', $VNDB::M{log_dir}, $f;
    close $I;
  }
  $_[KERNEL]->call(core => log => 'Logs rotated.');
}


#
#  V N   S E A R C H   C A C H E
#


sub vnsearch_check {
  $_[KERNEL]->call(pg => query =>
    'SELECT id FROM vn WHERE c_search IS NULL LIMIT 1',
    undef, 'vnsearch_gettitles');
}


sub vnsearch_gettitles { # num, res
  return $_[KERNEL]->delay('vnsearch_check', $_[HEAP]{vnsearch_checkdelay}) if $_[ARG0] == 0;
  my $id = $_[ARG1][0]{id};

  # fetch the titles
  $_[KERNEL]->call(pg => query => q{
    SELECT vr.title, vr.original, vr.alias
      FROM vn v
      JOIN vn_rev vr ON vr.id = v.latest
     WHERE v.id = ?
    UNION
    SELECT rr.title, rr.original, NULL
      FROM releases r
      JOIN releases_rev rr ON rr.id = r.latest
      JOIN releases_vn rv ON rv.rid = r.latest
     WHERE rv.vid = ?
       AND NOT r.hidden
  }, [ $id, $id ], 'vnsearch_update', $id);
}


sub vnsearch_update { # num, res, vid, time
  my($res, $id, $time) = @_[ARG1..ARG3];
  my @t = map +($_->{title}, $_->{original}), @$res;
  # alias fields are a bit special
  for (@$res) {
    push @t, split /[\n,]/, $_->{alias} if $_->{alias};
  }
  my $t = normalize_titles(@t);
  $_[KERNEL]->call(core => log => 'Updated search cache for v%d', $id);
  $_[KERNEL]->call(pg => do =>
    q|UPDATE vn SET c_search = ? WHERE id = ?|,
    [ $t, $id ], 'vnsearch_check');
}


1;