Browse Source

(2/2) advanced build. implement a daemon that will build static pages.

implement a queue and a lock. fix notice in bans. and it even works!

the daemon is basic right now, it could work in a mode that it will defer building certain
pages until a certain time.
pull/40/head
czaks 6 years ago
parent
commit
12e6aba5d4
  1. 2
      inc/bans.php
  2. 9
      inc/config.php
  3. 40
      inc/functions.php
  4. 2
      smart_build.php
  5. 0
      tmp/queue/generate/.gitkeep
  6. 31
      tools/worker.php

2
inc/bans.php

@ -166,7 +166,7 @@ class Bans {
if ($ban['post']) {
$post = json_decode($ban['post']);
$ban['message'] = $post->body;
$ban['message'] = isset($post->body) ? $post->body : 0;
}
unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);

9
inc/config.php

@ -103,7 +103,7 @@
/*
* ====================
* Cache settings
* Cache, lock and queue settings
* ====================
*/
@ -120,6 +120,7 @@
// $config['cache']['enabled'] = 'apc';
// $config['cache']['enabled'] = 'memcached';
// $config['cache']['enabled'] = 'redis';
// $config['cache']['enabled'] = 'fs';
// Timeout for cached objects such as posts and HTML.
$config['cache']['timeout'] = 60 * 60 * 48; // 48 hours
@ -142,6 +143,12 @@
// (this file will be explicitly loaded during cache hit, but not during cache miss).
$config['cache_config'] = false;
// Define a lock driver.
$config['lock']['enabled'] = 'fs';
// Define a queue driver.
$config['queue']['enabled'] = 'fs'; // xD
/*
* ====================
* Cookie settings

40
inc/functions.php

@ -19,6 +19,8 @@ require_once 'inc/database.php';
require_once 'inc/events.php';
require_once 'inc/api.php';
require_once 'inc/mod/auth.php';
require_once 'inc/lock.php';
require_once 'inc/queue.php';
require_once 'inc/polyfill.php';
@include_once 'inc/lib/parsedown/Parsedown.php'; // fail silently, this isn't a critical piece of code
@ -93,6 +95,8 @@ function loadConfig() {
'db',
'api',
'cache',
'lock',
'queue',
'cookies',
'error',
'dir',
@ -1749,7 +1753,6 @@ function buildJavascript() {
function checkDNSBL() {
global $config;
if (isIPv6())
return; // No IPv6 support yet.
@ -2806,17 +2809,17 @@ function generation_strategy($fun, $array=array()) { global $config;
$action = false;
foreach ($config['generation_strategies'] as $s) {
if ($strategy = $s($fun, $array)) {
if ($action = $s($fun, $array)) {
break;
}
}
switch ($strategy[0]) {
switch ($action[0]) {
case 'immediate':
return 'rebuild';
case 'defer':
// Ok, it gets interesting here :)
Queue::add(serialize(array('build', $fun, $array)));
get_queue('generate')->push(serialize(array('build', $fun, $array, $action)));
return 'ignore';
case 'build_on_load':
return 'delete';
@ -2832,5 +2835,32 @@ function strategy_smart_build($fun, $array) {
}
function strategy_sane($fun, $array) { global $config;
return false;
// Well, ideally a sane strategy would involve a more stringent checking,
// but let's at least have something to get the ball rolling :^)
if (php_sapi_name() == 'cli') return false;
else if (isset($_POST['mod']) || isset($_POST['json_response'])) return false;
else if ($fun == 'sb_thread' || ($fun == 'sb_board' && $array[1] == 1)) return array('immediate');
else return false;
}
// My first, test strategy.
function strategy_first($fun, $array) {
switch ($fun) {
case 'sb_thread':
return array('defer');
case 'sb_board':
if ($array[1] > 8) return array('build_on_load');
else return array('defer');
case 'sb_api':
return array('defer');
case 'sb_catalog':
return array('defer');
case 'sb_recent':
return array('build_on_load');
case 'sb_sitemap':
return array('build_on_load');
case 'sb_ukko':
return array('defer');
}
}

2
smart_build.php

@ -24,7 +24,7 @@ if (!$route) {
}
else {
list ($fun, $args) = $route;
$reached = call_user_func_array($route);
$reached = call_user_func_array($fun, $args);
}
function die_404() { global $config;

0
tmp/queue/generate/.gitkeep

31
tools/worker.php

@ -0,0 +1,31 @@
#!/usr/bin/php
<?php
/* worker.php - part of advanced build vichan feature */
require dirname(__FILE__) . '/inc/cli.php';
require_once 'inc/controller.php';
$config['smart_build'] = false; // Let's disable it, so we can build the page for real
$config['generation_strategies'] = array('strategy_immediate');
function after_open_board() { global $config;
$config['smart_build'] = false;
$config['generation_strategies'] = array('strategy_immediate');
};
echo "Hello world!\n";
$queue = get_queue('generate');
while (true) {
$q = $queue->pop(2);
foreach ($q as $v) {
list($__, $func, $ary, $action) = unserialize($v);
echo "Starting to generate $func ".implode(" ", $ary)."... ";
call_user_func_array($func, $ary);
echo "done!\n";
}
if (!$q) usleep(20000); // 0.02s
}
Loading…
Cancel
Save