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 / events.php (aeaa0a40a46bcc193111dbd812fec7084aa872f4) (4,045B) (mode 100644) [raw]
<?php
// This is called by cron, and is persistent.
// It takes care of any background job received.
// It will receive signals using a UNIX socket.
// TODO: This will obsolete q.php
error_reporting(E_ALL);
ini_set("track_errors", "On");
set_time_limit(0);

$now = time();
$_s = microtime(TRUE);

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 . "/events.inc.php");
require_once($INC . "/repo.inc.php");
require_once($INC . "/prof.inc.php");
require_once($INC . "/mr.inc.php");
require_once($INC . "/keys.inc.php");
require_once($INC . "/user.inc.php");
require_once($INC . "/bug.inc.php");
require_once($INC . "/fixes.inc.php");
require_once($INC . "/plan.inc.php");
require_once($INC . "/admin.inc.php");
require_once($INC . "/ver.php");

rg_prof_start("MAIN");

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

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

rg_sql_app("rg-events");
$db = rg_sql_open($rg_sql);
if ($db === FALSE) {
	rg_internal_error("Cannot connect to database!");
	exit(1);
}

if (rg_sql_struct_update_needed($db) !== 0)
	exit(0);

if (rg_fixes_needed($db) !== 0)
	exit(0);

// Remove the socket, else we will get error
if (file_exists($rg_event_socket))
	unlink($rg_event_socket);

// Prepare socket for signaling
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($socket === FALSE) {
	rg_internal_error("Cannot create events socket!");
	exit(1);
}

$r = socket_bind($socket, $rg_event_socket);
if ($r === FALSE) {
	rg_internal_error("Cannot bind socket!");
	exit(1);
}

$r = socket_listen($socket, 128);
if ($r === FALSE) {
	rg_internal_error("Cannot set queue length on socket!");
	exit(1);
}

// Allow apache to connect to socket
$r = chmod($rg_event_socket, 0666);
if ($r === FALSE) {
	rg_internal_error("Cannot set rights on event socket!");
	exit(1);
}

/* This will force accept "accept" to return too early
$r = socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 5, "usec" => 0));
if ($r === FALSE) {
	rg_internal_error("Cannot set SO_RCVTIMEO!");
	exit(1);
}
*/

$original_mtime = @filemtime(__FILE__);
$notify_list = array();
do {
	// Check our mtime so we can upgrade the software and this script
	// will restart.
	clearstatcache();
	$mtime = @filemtime(__FILE__);
	rg_log("mtime=$mtime, original_mtime=$original_mtime");
	if ($mtime != $original_mtime) {
		rg_log("File changed. Exiting...");
		break;
	}

	rg_log_buffer_clear();

	do {
		// check machine load - if too big we will delay
		$load = rg_load();
		if ($load > 10) {
			rg_log("\tLoad too big! Skip queue processing.");
			break;
		}

		$r = rg_event_process_queue($db, $notify_list);
		if ($r === FALSE)
			break;
	} while ($r > 0);
	if ($r === FALSE)
		break;

	// Wait for signal
	rg_log("Waiting for signal...");
	// TODO: use socket_select because, else, we are blocking!
	$client = socket_accept($socket);
	if ($client === FALSE) {
		rg_log("Connection seems broken!");
		continue;
	}

	// TODO: this will be used with select
	//socket_set_nonblock($client);

	$close = 1;
	$r = socket_recv($client, $buf, 1024, 0);
	if ($r === 0) { // remote close the connection
		rg_log("Remote closed the connection.");
	} else if ($r === FALSE) {
		$errno = socket_last_error($client);
		rg_log("Error in receive (" . socket_strerror($errno) . ").");
	} else {
		rg_log("Received $r byte(s): [$buf].");
		if (strncmp($buf, "NOTIFY ", 7) == 0) {
			$ev_id = trim(substr($buf, 7));
			$notify_list[$ev_id][] = array("fd" => $client, "itime" => time());
			$close = 0;

			// It is possible that we already executed the task
			rg_event_notify($notify_list, $ev_id, "");
		}
	}

	if ($close == 1)
		socket_close($client);

	// clean up notify_list
	rg_event_notify_clean($notify_list);
} while (1);

socket_close($socket);

rg_log("Exiting...");

rg_prof_end("MAIN");
rg_prof_log();
?>
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