Passed
Push — server_release/1/1.4 ( 2fdc9f...fa038f )
by
unknown
10:44
created

handle_submit()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 38
nc 14
nop 3
dl 0
loc 55
rs 8.6897
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
DEPRECATED
4
5
// Handler for TreeThreader remote job submission.
6
//
7
// Assumptions:
8
// - there is a file "tree_threader_templates_files" in the project root
9
//   containing (one per line) the names of files containing
10
//   gzipped collections of template files
11
// - These files are in the download hierarchy.
12
13
require_once("../inc/boinc_db.inc");
0 ignored issues
show
Coding Style introduced by
PHP syntax error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE)
Loading history...
Bug introduced by
A parse error occurred: Syntax error, unexpected T_REQUIRE_ONCE on line 13 at column 0
Loading history...
14
require_once("../inc/submit_db.inc");
15
require_once("../inc/xml.inc");
16
require_once("../inc/dir_hier.inc");
17
require_once("../inc/result.inc");
18
require_once("../inc/submit_util.inc");
19
20
error_reporting(E_ALL);
21
ini_set('display_errors', true);
22
ini_set('display_startup_errors', true);
23
24
$app_name = "treeThreader";
25
$log = fopen("/tmp/tt_job.log","a+");
26
27
function error($s) {
28
    echo "<error>\n<message>$s</message>\n</error>\n";
29
    exit;
30
}
31
32
function handle_submit($r, $user, $app) {
33
    global $app_name,$log;
34
35
    $timestamp = date("Y-m-d H:i",time());
36
	// read the list of template filenames
37
	//
38
	$files = file("../../tree_threader_template_files");
39
	if ($files === false) { 
40
        fwrite($log,"$timestamp\ttemplate file tree_threader_template_files\n");
41
        error("no templates file");
42
        
43
    }
44
	$njobs = sizeof($files);
45
	$now = time();
46
    $batch_id = BoincBatch::insert(
47
		"(user_id, create_time, njobs, name, app_id, state) values ($user->id, $now, $njobs, 'tree_threader batch', $app->id, ".BATCH_STATE_IN_PROGRESS.")"
48
    );
49
    if (!$batch_id) {
50
        $log_msg = "$timestamp\tfailed to create batch for user $user->id\n";
51
        fwrite($log, $log_msg);
52
        die("couldn't create batch\n");
53
    } else {
54
        $log_msg = "$timestamp\tcreated batch $batch_id for user $user->id\n";
55
        fwrite($log, $log_msg);
56
    }
57
58
    // move the sequence file to the download hier
59
    //
60
    $config = simplexml_load_string(file_get_contents("../../config.xml"));
61
    $fanout = (int)$config->config->uldl_dir_fanout;
62
    $download_dir = trim((string)$config->config->download_dir);
63
64
    $seq_fname = "treeThreader_sequence_$batch_id.tar.gz";
65
    $seq_path = dir_hier_path($seq_fname, $download_dir, $fanout);
66
    $tmp_name = $_FILES['seq_file']['tmp_name'];
67
    $ret = rename($tmp_name, $seq_path);
68
    if ($ret === false) {
69
        error("couldn't rename sequence file");
70
    }
71
72
    $i = 1;
73
	foreach ($files as $file) {
74
        $file = trim($file);
75
        $wu_name = "ICT_".$batch_id."_$i";
76
77
		$cmd = "cd ../..; ./bin/create_work --appname $app_name --batch $batch_id --wu_name $wu_name --wu_template templates/ICT_in --result_template templates/ICT_out $seq_fname $file";
78
        fwrite($log, "$timestamp\t$cmd\n");
79
        system($cmd, $ret);
80
		if ($ret != 0) {
81
            fwrite($log, "can not creat job $wu_name\n");
82
			error("can't create job");
83
        }
84
        $i++;
85
	}
86
	echo "<tt_reply>\n<batch_id>$batch_id</batch_id>\n</tt_reply>\n";
87
}
88
89
// Enumerate all the successfully completed WUs for this batch.
90
// Each output file is a .zip that unzips into a directory ali/.
91
// Combine their output files into a zip file in /tmp,
92
// make a symbolic link to this from /download,
93
// and return the resulting URL
94
//
95
function handle_get_output($r, $batch) {
96
    global $log;
97
    $timestamp = date("Y-m-d H:i",time());
98
	$wus = BoincWorkUnit::enum("batch=$batch->id");
99
	$outdir = "/tmp/treeThreader_result_".$batch->id;
100
    @mkdir($outdir);
101
	foreach ($wus as $wu) {
102
		if (!$wu->canonical_resultid) continue;
103
		$result = BoincResult::lookup_id($wu->canonical_resultid);
104
		if (!$result) continue;
105
		$paths = get_outfile_paths($result);
106
		if (sizeof($paths) < 1) continue;
107
108
		// there's only one output file
109
		//
110
		$path = $paths[0];
111
112
		// unzip it into a directory in /tmp
113
		//
114
		$dir = "/tmp/$wu->name";
115
        @mkdir($dir);
116
		$cmd = "cd $dir; unzip -q $path";
117
		system($cmd, $ret);
118
		if ($ret != 0) {
119
			error("can't unzip output file");
120
		}
121
		$cmd = "cp $dir/Aln/* $outdir";
122
		system($cmd, $ret);
123
		if ($ret != 0) {
124
			error("can't copy output files");
125
		}
126
127
        system("rm -rf $dir");
128
	}
129
130
	$cmd = "zip -r -q $outdir $outdir";
131
	system($cmd, $ret);
132
	if ($ret != $ret) {
133
		error("can't zip output files");
134
	}
135
	$fname = "treeThreader_result_".$batch->id.".zip";
136
    $treeThreader_dir="treeThreaderResult";
137
    if(!is_dir("../../download/$treeThreader_dir"))mkdir("../../download/$treeThreader_dir");
138
	@symlink("/tmp/$fname", "../../download/$treeThreader_dir/$fname");
139
    system("rm -fr $outdir");
140
    $config = simplexml_load_string(file_get_contents("../../config.xml"));
141
    $download_url = trim((string)$config->config->download_url);
142
	echo "<tt_reply>\n<url>$download_url/$treeThreader_dir/$fname</url>\n</tt_reply>\n";
143
    $log_msg="$timestamp\tuser $batch->user_id downloads results for batch $batch->id : $download_url/$treeThreader_dir/$fname\n";
144
    fwrite($log, $log_msg);
145
}
146
147
xml_header();
148
149
if (1) {
150
    $r = simplexml_load_string($_POST['request']);
151
} else {
152
    $x = file_get_contents("xml_req");
153
    $r = simplexml_load_string($x);
154
}
155
156
if (!$r) {
157
    error("can't parse request message");
158
}
159
160
// authenticate the user
161
//
162
$auth = BoincDb::escape_string((string)$r->auth);
163
$user = BoincUser::lookup("authenticator='$auth'");
164
if (!$user) error("invalid authenticator");
165
$user_submit = BoincUserSubmit::lookup_userid($user->id);
166
if (!$user_submit) error("no submit access");
167
$app = BoincApp::lookup("name='$app_name'");
168
if (!$app) error("no tree_threader app");
169
170
if (!$user_submit->submit_all) {
171
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id");
172
    if (!$usa) {
173
        error("no submit access");
174
    }
175
}
176
177
switch ((string)$r->action) {
178
    case 'submit':
179
        handle_submit($r, $user, $app);
180
        break;
181
    case 'get_output':
182
		$batch_id = (int)$r->batch_id;
183
		$batch = BoincBatch::lookup_id($batch_id);
184
		if (!$batch) error("no such batch");
185
		if ($batch->user_id != $user->id) error("not owner of batch");
186
		handle_get_output($r, $batch);
187
		break;
188
    default: error("bad command");
0 ignored issues
show
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
189
}
190
191
?>
192