/scripts/cron.php (c705aa78ce2a4cf507e4afe961f8a56dd9566cee) (5886 bytes) (mode 100644) (type blob)
<?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 . "/stats.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
// 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);
}
$rg['hostname'] = rg_state_get($db, 'hostname');
$rg['http_allow'] = rg_state_get($db, 'http_allow');
$rg['https_allow'] = rg_state_get($db, 'https_allow');
rg_base_url_build($rg['hostname'], $rg['http_allow'], $rg['https_allow']);
$rg['base_url'] = rg_base_url();
rg_log('DEBUG: base_url=' . rg_base_url());
$rg['debug'] = rg_state_get($db, 'debug');
rg_stats_insert($db);
if (gmdate("Hi") == "0105") {
while (1) {
if (rg_load() > 100)
break;
rg_log("Compute repository sizes if dirty...");
// delete 'dirty' files
$sql = 'SELECT uid, repo_id, master, disk_used_mb FROM repos'
. ' WHERE deleted = 0';
$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();
}
while (gmdate("dHi") == "010610") {
if (rg_load() > 100)
break;
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);
if ($res2 !== FALSE)
rg_sql_free_result($res2);
}
rg_sql_free_result($res);
break;
}
rg_log_exit();
}
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_cron();
rg_log("Done!");
?>
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
9 |
f3c7a7c5da68804a1bdf391127ba34aed33c3cca |
.exclude |
100644 |
blob |
102 |
eaeb7d777062c60a55cdd4b5734902cdf6e1790c |
.gitignore |
100644 |
blob |
375 |
1f425bcd2049c526744d449511094fc045ceac74 |
AUTHORS |
100644 |
blob |
1132 |
dd65951315f3de6d52d52a82fca59889d1d95187 |
Certs.txt |
100644 |
blob |
1274 |
09165f115226fccce99add24cdf77becdd1ec1f6 |
History.txt |
100644 |
blob |
34520 |
dba13ed2ddf783ee8118c6a581dbf75305f816a3 |
LICENSE |
100644 |
blob |
3464 |
ec8d83caa215ab1aca88b7ac273557262009c82a |
Makefile.in |
100644 |
blob |
5867 |
0c5899445818b82269b17da3fff4c39a89f760bb |
README |
100644 |
blob |
146199 |
66abf9b425c5b7e86a25d198793750877cafff56 |
TODO |
100644 |
blob |
1294 |
f22911eb777f0695fcf81ad686eac133eb11fcc4 |
TODO-plans |
100644 |
blob |
203 |
a2863c67c3da44126b61a15a6f09738c25e0fbe0 |
TODO.perf |
100644 |
blob |
967 |
56bbaa7c937381fb10a2907b6bbe056ef8cc824a |
TODO.vm |
040000 |
tree |
- |
21928e906ad2907a55c2e81c2a8b0502b586b8a0 |
artwork |
100644 |
blob |
5550 |
bde1bc0fad6f0ebb858d324a2d58510bb339a6de |
compare.csv |
100755 |
blob |
30 |
92c4bc48245c00408cd7e1fd89bc1a03058f4ce4 |
configure |
040000 |
tree |
- |
69114e8648f8e0e7173c76e30ca6bbfcece7df31 |
debian |
040000 |
tree |
- |
a9d8117dcc14048c006970b5debd2f51cf52fdfd |
docker |
040000 |
tree |
- |
f67d3605efbd6422a8acdd953578991139266391 |
docs |
100755 |
blob |
16720 |
52405deef0d3708e7553022e1e9db73faa28d05c |
duilder |
100644 |
blob |
536 |
7e834f8f0a52ada786dd978522cd0f310e2438f6 |
duilder.conf |
040000 |
tree |
- |
77290d80669b96d488189caad411699db4b0a6a0 |
hooks |
040000 |
tree |
- |
8d09657bf79729a4df1d2856b7afadcbd13b3de3 |
inc |
040000 |
tree |
- |
e255ce234c3993998edc12bc7e93fff555376eda |
misc |
100644 |
blob |
4616 |
df249b84bedcc590a2a9b73cdc17ad1a8e008ca6 |
rocketgit.spec.in |
040000 |
tree |
- |
ecdfe0855baba8218eeabb995e579bc9446abee2 |
root |
040000 |
tree |
- |
2ac5565bc70ea971f2ea36d94953c7e00e3f4b8e |
samples |
040000 |
tree |
- |
c292fa2029303fcfeb6b3c328e8a776cf16e5475 |
scripts |
040000 |
tree |
- |
ecb1da91f5ae28f3f33eca9e5d076c3f9be92f49 |
selinux |
100755 |
blob |
256 |
462ccd108c431f54e380cdac2329129875a318b5 |
spell_check.sh |
040000 |
tree |
- |
3aee54193d1f2fb794cb1133433e4645b864f5a0 |
techdocs |
040000 |
tree |
- |
56db4df97c8496c2a7f99c56d64db58657f0040c |
tests |
040000 |
tree |
- |
3a262971aa172ade74d5a4930e04393918ca1911 |
tools |
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://rocketgit.com/user/catalinux/rocketgit
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/rocketgit
Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/rocketgit
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main