-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrebuildd.php
More file actions
130 lines (106 loc) · 3.46 KB
/
rebuildd.php
File metadata and controls
130 lines (106 loc) · 3.46 KB
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
<?
// rebuild daemon
// rebuilds HTML pages constantly, leaving only threads for the posting processes
// for each STATIC_REBUILD board, start one of these with the board as cwd
// setup (turn off die() and such)
$mode = "nothing";
define("IS_REBUILDD", true);
$mysql_never_die = true;
$mysql_suppress_err = false;
//$mysql_connect_opts = MYSQL_CLIENT_COMPRESS;
set_time_limit(0);
ini_set("memory_limit", "-1");
include("imgboard.php");
//if( ENABLE_CATALOG ) require_once 'catalog.php';
$count = 0;
$sec_in_us = 1000000;
$time_to_sleep_max = MAX_REBUILD_TIMER;
$time_to_sleep_min = MIN_REBUILD_TIMER;
$time_to_sleep = $time_to_sleep_min;
$update_counter = 0;
$update_total_secs = 0.0;
$lastno = 0;
$lastthreadno = 0;
$do_trim_db = false;
file_put_contents("/www/perhost/rebuildd-".BOARD_DIR.".pid", getmypid());
function rebuildd_gate()
{
global $lastno,$lastthreadno,$do_trim_db,$con,$gcon;
$ret = 0;
$ctype = get_resource_type($con); $gtype = get_resource_type($gcon);
echo "last $lastno, last thread $lastthreadno, cons $con/$ctype $gcon/$gtype, ";
mysql_check_connections();
list($newlastno) = mysql_fetch_row(mysql_board_call("select max(no) from `".SQLLOG."`"));
if ($newlastno > $lastno) {
//may not be indexed...
list($newthreadno) = mysql_fetch_row(mysql_board_call("select max(no) from `".SQLLOG."` where resto=0 and archived=0"));
$do_trim_db = $newthreadno > $lastthreadno;
$lastthreadno = $newthreadno;
$lastno = $newlastno;
$ret = 1;
}
echo "new thread $lastthreadno, new last $lastno, shouldRebuild $ret\n";
return $ret;
}
function rebuildd_update_index()
{
global $do_trim_db, $count;
log_cache(1);
if ($do_trim_db) {
trim_db();
trim_archive();
}
//updatelog();
rebuild_indexes_daemon();
rpc_task();
if( ENABLE_CATALOG && $count == CATALOG_DAEMON_REBUILD_INTERVAL ) {
generate_catalog();
$count = 0;
}
if( ENABLE_CATALOG ) $count++;
}
function rebuildd_occasional_cleanup()
{
global $rpc_mh; global $rpc_chs;
$nrpcs = count($rpc_chs);
echo "finishing $nrpcs RPCs\n";
rpc_finish_all();
}
while (1) {
$update_start_time = microtime(true);
if (rebuildd_gate()) {
rebuildd_update_index();
$update_end_time = microtime(true);
$update_counter++;
$work_time = $update_this_secs = $update_end_time - $update_start_time;
$update_total_secs += $update_this_secs;
$update_avg_secs = $update_total_secs / $update_counter;
$next_start_time = $update_start_time + $time_to_sleep_min;
$to_sleep = $next_start_time - $update_end_time;
if ($to_sleep <= 0) { // if we're late, skip to the next time
rebuildd_occasional_cleanup();
$to_sleep = $time_to_sleep_min;
$work_time = microtime(true) - $update_start_time;
}
$sleepusec = $to_sleep * $sec_in_us;
usleep($sleepusec);
// reset long-sleep to shortest time
$time_to_sleep = $time_to_sleep_min;
// avoid overflow
if ($update_counter == 65536) {
$update_counter = 0;
$update_total_secs = 0.0;
}
echo "$update_end_time update counter $update_counter slept $to_sleep update took $work_time\n";
} else {
// todo push-style rebuilds instead of polling for slower boards
rebuildd_occasional_cleanup();
$update_end_time = microtime(true);
$sleepusec = $time_to_sleep * $sec_in_us;
usleep($sleepusec);
$time_to_sleep = min($time_to_sleep*1.25, $time_to_sleep_max);
$update_this_secs = $update_end_time - $update_start_time;
echo "$update_end_time update counter $update_counter slept $to_sleep cleanup took $update_this_secs\n";
}
}
?>