-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_master
executable file
·117 lines (103 loc) · 2.56 KB
/
copy_master
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
#! /usr/bin/env php
<?php
// script to manage copy tasks
//
// copy_master args
// --init: clear in_progress flags (do at boot time)
// --daemon: run as a daemon
// --sleep_interval: if daemon, how long to sleep between scans
// --retry_interval: how long to wait until retry failed copy (sec)
// default: start tasks as needed, then exit
require_once("hera_util.inc");
require_once("hl_db.inc");
$sleep_interval = 10;
$retry_interval = 60;
// clear the in_progress flag of uncompleted tasks
//
function init() {
task_update_all("in_progress=0", "completed=0");
echo "reset ".affected_rows()." tasks\n";
}
// spawn a process to handle a task
//
function start_task($task) {
task_update($task->id, "in_progress=1");
$pid = pcntl_fork();
if (!$pid) {
$cmd = "./copier --task_id $task->id";
system($cmd);
exit(0);
}
}
// enumerate tasks (up to the config limit) and handle them
//
function start_tasks() {
global $retry_interval, $config;
if ($config->max_transfers) {
$count = table_count("task", "in_progress<>0");
$limit = $config->max_transfers - $count;
if ($limit <= 0) {
echo "at max_transfer limit; not starting new copies\n";
return;
} else {
echo "limit: $limit new copies\n";
}
} else {
$limit = 9999;
}
$t = time() - $retry_interval;
$tasks = task_enum("completed=0 and in_progress=0 and last_error_time<$t limit $limit");
if (!$tasks) {
echo "no tasks to run\n";
return;
}
foreach ($tasks as $task) {
echo "starting task $task->id\n";
start_task($task);
}
}
// if run as daemon: repeatedly scan for tasks
//
function daemon() {
global $sleep_interval;
while (1) {
start_tasks();
// reap any child processes
//
while (1) {
$ret = pcntl_wait($status, WNOHANG);
if ($ret > 0) continue;
break;
}
sleep($sleep_interval);
}
}
$daemon = false;
$config = get_server_config();
if (!init_db($config)) {
die("can't open DB\n");
}
for ($i=1; $i<$argc; $i++) {
switch ($argv[$i]) {
case "--init":
init();
exit;
case "--daemon":
$daemon = true;
break;
case "--sleep_interval":
$sleep_interval = (int)($argv[++$argc]);
break;
case "--retry_interval":
$retry_interval = (int)($argv[++$argc]);
break;
default:
echo "bad arg: ".$argv[$i]."\n";
}
}
if ($daemon) {
daemon();
} else {
start_tasks();
}
?>