xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / scripts / cron.php (07df3f2d16d5a6c7ed13c1f91041d0af958de83b) (5,371B) (mode 100644) [raw]
<?php
// This is called by cron
error_reporting(E_ALL);
ini_set("track_errors", "On");
set_time_limit(0);

require_once("/etc/rocketgit/config.php");

$INC = dirname(__FILE__) . "/../inc";
require_once($INC . "/init.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/struct.inc.php");
require_once($INC . "/repo.inc.php");
require_once($INC . "/keys.inc.php");
require_once($INC . "/fixes.inc.php");
require_once($INC . "/mr.inc.php");
require_once($INC . "/admin.inc.php");
require_once($INC . "/ver.php");

$now = time();

rg_log_set_file($rg_log_dir . "/cron.log");
rg_log_set_sid("000000"); // to spread the logs

if (rg_load() > 5)
	exit(0);

// locking
rg_lock_or_exit("cron.lock");

rg_log("Start (ver=$rocketgit_version)...");

if (gmdate("Hi") == "0305")
	rg_clean_logs('/var/log/rocketgit');

rg_sql_app("rg-cron");
$db = rg_sql_open($rg_sql);
if ($db === FALSE) {
	rg_log("Cannot connect to database (" . rg_sql_error() . ")!");
	// TODO: inform admin - already by e-mail?
	exit(1);
}

if (rg_struct_ok($db) === FALSE)
	exit(1);

$first_install = rg_state_get($db, 'first_install');
if ($first_install === FALSE)
	exit(1);
if (empty($first_install)) {
	rg_log("Admin did not created the user, so no need to run this code.");
	exit(0);
}

if (gmdate("Hi") == "0105") {
	while (1) {
		rg_log("Compute repository sizes if dirty...");
		// delete 'dirty' files
		$sql = "SELECT uid, repo_id, master, disk_used_mb FROM repos";
		$res = rg_sql_query($db, $sql);
		if ($res === FALSE) {
			// TODO: rg_internal_error? it must notify me in case of problems
			rg_log("Cannot run query (" . rg_sql_error() . ")!");
			break;
		}

		while (($row = rg_sql_fetch_array($res))) {
			rg_log("Processing repo " . $row['repo_id'] . "...");
			$repo_path = rg_repo_path_by_id($row['uid'], $row['repo_id']);

			$all_files = $row['master'] == 0 ? TRUE : FALSE;
			$disk_used = rg_repo_size($repo_path, $all_files);
			if ($disk_used === FALSE) {
				rg_log("Cannot compute the repo size: " . rg_repo_error());
				continue;
			}
			$disk_used_mb = sprintf("%u", $disk_used / 1024 / 1024);
			if ($disk_used_mb != $row['disk_used_mb']) {
				$sql = "UPDATE repos SET disk_used_mb = $disk_used_mb"
					. " WHERE repo_id = " . $row['repo_id'];
				$res2 = rg_sql_query($db, $sql);
				if ($res2 === FALSE) {
					rg_log("Cannot run query!");
					break;
				}
				rg_sql_free_result($res2);
			}
		}
		rg_sql_free_result($res);
		break;
	}

	while (1) {
		rg_log("Compute repository sizes per user...");
		$sql = "SELECT SUM(disk_used_mb) AS disk_used_mb, uid"
			. " FROM repos"
			. " GROUP BY uid";
		$res = rg_sql_query($db, $sql);
		if ($res === FALSE) {
			rg_log("Cannot run query (" . rg_sql_error() . ")!");
			break;
		}

		while (($row = rg_sql_fetch_array($res))) {
			$sql = "UPDATE users"
				. " SET disk_used_mb = " . $row['disk_used_mb']
				. " WHERE uid = " . $row['uid']
				. " AND disk_used_mb != " . $row['disk_used_mb'];
			$res2 = rg_sql_query($db, $sql);
			if ($res2 !== FALSE)
				rg_sql_free_result($res2);
		}
		rg_sql_free_result($res);
		break;
	}
}

if (gmdate("Hi") == "0300") {
	rg_log_enter("Clean old forget_pass entries...");
	$sql = "DELETE FROM forgot_pass WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	if ($res !== FALSE)
		rg_sql_free_result($res);
	rg_log_exit();
}

if (gmdate("i") == "30") {
	rg_log_enter("Clean old tokens...");
	$sql = "DELETE FROM tokens WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	if ($res !== FALSE)
		rg_sql_free_result($res);
	rg_log_exit();
}

if (gmdate("i") == "01") {
	rg_log_enter("Clean old sess entries...");
	$sql = "DELETE FROM sess WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	if ($res !== FALSE)
		rg_sql_free_result($res);
	rg_log_exit();
}

if (gmdate("Hi") == "0605") {
	rg_log_enter("Clean old login_tokens_ip entries...");
	$sql = "DELETE FROM login_tokens_ip WHERE expire < $now";
	$res = rg_sql_query($db, $sql);
	if ($res !== FALSE)
		rg_sql_free_result($res);
	rg_log_exit();
}

if (gmdate("dHi") == "010610") {
	rg_log_enter("Clean old empty slave tables...");

	$ts = time() - 3 * 31 * 24 * 3600;
	$limit = gmdate('Y_m', $ts);
	rg_log('limit=' . $limit);
	foreach ($rg_sql_struct_slaves as $table) {
		rg_log('Slave: ' . $table);
		$sql = 'SELECT relname FROM pg_class'
			. ' WHERE relname LIKE \'' . $table . '_%\''
			. ' AND relkind = \'r\''
			. ' AND relname < \'' . $table . '_' . $limit . '\''
			. ' ORDER BY relname';
		$res = rg_sql_query($db, $sql);
		if ($res === FALSE) {
			rg_log('Cannot load slave tables: ' . rg_sql_error());
			break;
		}
		while (($row = rg_sql_fetch_array($res))) {
			$sql = 'SELECT 1 FROM ' . $row['relname'] . ' LIMIT 1';
			$res2 = rg_sql_query($db, $sql);
			if ($res2 === FALSE) {
				rg_log('Cannot select from table ['
					. $row['relname'] . ']: '
					. rg_sql_error());
				break;
			}
			$rows = rg_sql_num_rows($res2);
			rg_sql_free_result($res2);

			if ($rows == 1)
				continue;

			$sql = 'DROP TABLE ' . $row['relname'];
			$res2 = rg_sql_query($db, $sql);
			rg_sql_free_result($res2);
		}
		rg_sql_free_result($res);
	}

	rg_log_exit();
}

rg_log_cron();

rg_sql_struct_slaves_update($db);

if (gmdate("Hi") == "0100")
	rg_admin_report1($db, $rg);

// TODO: move it as an event after the push
rg_mr_queue_process($db);

rg_log("Done!");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master