summaryrefslogtreecommitdiff
blob: 1112b0a1df7f455c50113b8b67ad9b1e26db038d (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
class sql_task extends sql_row_obj {
	protected $table='tasks', $primary_key=array('build', 'order'), $columns=array(
		'build' => array (
			'type' => 'CHAR',
			'length' => 6,
			'not_null' => true,
			'default' => '',
			'refers_to' => 'builds.id'
		),
		'order' => array (
			'type' => 'TINYINT',
			'length' => 4,
			'not_null' => true,
			'default' => 0
		),
		'type' => array (
			'type' => 'ENUM',
			'length' => '\'exec\',\'internal\'',
			'not_null' => true
		),
		'description' => array (
			'type' => 'VARCHAR',
			'length' => 255,
			'not_null' => true,
			'default' => ''
		),
		'command' => array (
			'type' => 'TEXT'
		),
		'start' => array (
			'type' => 'INT',
			'length' => 10,
			'unsigned' => true
		),
		'finish' => array (
			'type' => 'INT',
			'length' => 10,
			'unsigned' => true
		),
		'exit' => array (
			'type' => 'TINYINT',
			'length' => 3
		)

	);
	function display() {
		$html='<div class="task"><div class="description">'.htmlentities($this->description).'</div><div class="info">[<a href="'.url("build/$this->build/$this->order").'">log</a>] '.($this->command?'<span class="command">'.htmlentities($this->command).'</span> ':'');
		if (isset($this->start)) {
			if (isset($this->finish)) {
				$html.='<span class="status ';
				if ($this->exit === '0') {
					$html.='successful">[successful';
				} else {
					$html.='failed">[';
					if (isset($this->exit)) {
						if ($this->exit > 0)
							$html.='exit status '.$this->exit;
						elseif ($this->exit == -128)
							$html.='got unknown signal';
						else
							$html.='got signal '.-$this->exit;
					} else
						$html.='failed to execute';
				}
				$html.=']</span> <span class="time">Finished in <span class="time">'.display_time($this->finish-$this->start).'</span></span>';
			} else {
				$html.='<span class="status running">[running]</span> <span class="time">Running for <span class="time">'.display_time(time()-$this->start).'</span></span>';
			}
		} else {
			$num=$S['pdo']->query('SELECT COUNT(*) FROM `tasks` WHERE `builds`="'.$this->build.'" AND `start` IS NULL AND `order` <= '.$this->order)->fetch(PDO::FETCH_ASSOC);
			$html.="<span class=\"status queued\">[queued $num/".$build->queued_tasks()."]</span>";
		}
		$html.='</div></div>';
		return $html;
	}
	function execute($path=null, $env=null) {
		if (!isset($this->command, $this->build, $this->order)) {
			throw_exception('$this->command, $this->build, or $this->order not set');
		} elseif (isset($this->start)) {
			throw_exception('task has already executed: start is '.$this->start);
		}
		$this->type='exec';
		debug('Executing '.$this->command.'...');
		$descriptorspec=array(
			0 => array('pipe', 'r'), // STDIN
			1 => array('pipe', 'w'), // STDOUT
			2 => array('pipe', 'w') // STDERR
		);
		$this->start=time();
		$this->write();
		$p=proc_open($this->command, $descriptorspec, $pipes, $path, $env);
		if ($p === false) {
			$this->finish=time();
			$this->write();
			throw_exception('Failed to execute command: '.$this->command);
		}
		foreach ($pipes as $pipe) {
			stream_set_blocking($pipe, 0);
		}
		while (true) {
			$null=null; // We have to set these all to variables because stream_select requires pass by reference
			$outs=array_slice($pipes, 1);
			$status=proc_get_status($p); // Get status before the wait so we're sure to get all the output
			$s=stream_select($outs, $null, $null, 1);
			if ($s) {
				$c=stream_get_contents($pipes[2]);
				if ($c) {
					// STDERR
					$entry=new sql_buildlog_entry($this->build, $this->order, buildlog_entry_get_order(), time(), 'stderr', $c);
					$entry->write();
				}
				$c=stream_get_contents($pipes[1]);
				if ($c) {
					// STDOUT
					$entry=new sql_buildlog_entry($this->build, $this->order, buildlog_entry_get_order(), time(), 'stdout', $c);
					$entry->write();
				}
			}
			if ($status['running'] === false) {
				if ($status['signaled']) {
					if ($status['termsig'])
						$this->exit=-$status['termsig'];
					elseif ($status['stopsig'])
						$this->exit=-$status['stopsig'];
					else
						// This should never happen
						$this->exit=-128;
				} else
					$this->exit=$status['exitcode'];
				break;
			}
		}
		$this->finish=time();
		foreach ($pipes as $pipe) {
			fclose($pipe);
		}
		if (!isset($this->exit)) {
			$this->exit=proc_close($p);
		}
		$this->write();
		return $this->exit;
	}
}
?>