forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorDaemonBulkJobMonitorController.php
161 lines (137 loc) · 4.71 KB
/
PhabricatorDaemonBulkJobMonitorController.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
final class PhabricatorDaemonBulkJobMonitorController
extends PhabricatorDaemonBulkJobController {
public function handleRequest(AphrontRequest $request) {
$viewer = $this->getViewer();
$job = id(new PhabricatorWorkerBulkJobQuery())
->setViewer($viewer)
->withIDs(array($request->getURIData('id')))
->executeOne();
if (!$job) {
return new Aphront404Response();
}
// If the user clicks "Continue" on a completed job, take them back to
// whatever application sent them here.
if ($request->getStr('done')) {
if ($request->isFormPost()) {
$done_uri = $job->getDoneURI();
return id(new AphrontRedirectResponse())->setURI($done_uri);
}
}
$title = pht('Bulk Job %d', $job->getID());
if ($job->getStatus() == PhabricatorWorkerBulkJob::STATUS_CONFIRM) {
$can_edit = PhabricatorPolicyFilter::hasCapability(
$viewer,
$job,
PhabricatorPolicyCapability::CAN_EDIT);
if ($can_edit) {
if ($request->isFormPost()) {
$type_status = PhabricatorWorkerBulkJobTransaction::TYPE_STATUS;
$xactions = array();
$xactions[] = id(new PhabricatorWorkerBulkJobTransaction())
->setTransactionType($type_status)
->setNewValue(PhabricatorWorkerBulkJob::STATUS_WAITING);
$editor = id(new PhabricatorWorkerBulkJobEditor())
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnMissingFields(true)
->applyTransactions($job, $xactions);
return id(new AphrontRedirectResponse())
->setURI($job->getMonitorURI());
} else {
return $this->newDialog()
->setTitle(pht('Confirm Bulk Job'))
->appendParagraph($job->getDescriptionForConfirm())
->appendParagraph(
pht('Start work on this bulk job?'))
->addCancelButton($job->getManageURI(), pht('Details'))
->addSubmitButton(pht('Start Work'));
}
} else {
return $this->newDialog()
->setTitle(pht('Waiting For Confirmation'))
->appendParagraph(
pht(
'This job is waiting for confirmation before work begins.'))
->addCancelButotn($job->getManageURI(), pht('Details'));
}
}
$dialog = $this->newDialog()
->setTitle(pht('%s: %s', $title, $job->getStatusName()))
->addCancelButton($job->getManageURI(), pht('Details'));
switch ($job->getStatus()) {
case PhabricatorWorkerBulkJob::STATUS_WAITING:
$dialog->appendParagraph(
pht('This job is waiting for tasks to be queued.'));
break;
case PhabricatorWorkerBulkJob::STATUS_RUNNING:
$dialog->appendParagraph(
pht('This job is running.'));
break;
case PhabricatorWorkerBulkJob::STATUS_COMPLETE:
$dialog->appendParagraph(
pht('This job is complete.'));
break;
}
$counts = $job->loadTaskStatusCounts();
if ($counts) {
$dialog->appendParagraph($this->renderProgress($counts));
}
switch ($job->getStatus()) {
case PhabricatorWorkerBulkJob::STATUS_COMPLETE:
$dialog->addHiddenInput('done', true);
$dialog->addSubmitButton(pht('Continue'));
break;
default:
Javelin::initBehavior('bulk-job-reload');
break;
}
return $dialog;
}
private function renderProgress(array $counts) {
$this->requireResource('bulk-job-css');
$states = array(
PhabricatorWorkerBulkTask::STATUS_DONE => array(
'class' => 'bulk-job-progress-slice-green',
),
PhabricatorWorkerBulkTask::STATUS_RUNNING => array(
'class' => 'bulk-job-progress-slice-blue',
),
PhabricatorWorkerBulkTask::STATUS_WAITING => array(
'class' => 'bulk-job-progress-slice-empty',
),
PhabricatorWorkerBulkTask::STATUS_FAIL => array(
'class' => 'bulk-job-progress-slice-red',
),
);
$total = array_sum($counts);
$offset = 0;
$bars = array();
foreach ($states as $state => $spec) {
$size = idx($counts, $state, 0);
if (!$size) {
continue;
}
$classes = array();
$classes[] = 'bulk-job-progress-slice';
$classes[] = $spec['class'];
$width = ($size / $total);
$bars[] = phutil_tag(
'div',
array(
'class' => implode(' ', $classes),
'style' =>
'left: '.sprintf('%.2f%%', 100 * $offset).'; '.
'width: '.sprintf('%.2f%%', 100 * $width).';',
),
'');
$offset += $width;
}
return phutil_tag(
'div',
array(
'class' => 'bulk-job-progress-bar',
),
$bars);
}
}