2012-01-24 01:36:32 +01:00
|
|
|
<?php
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorWorkerTaskUpdateController
|
2012-01-24 01:36:32 +01:00
|
|
|
extends PhabricatorDaemonController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
private $action;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
$this->action = $data['action'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2012-10-31 23:22:16 +01:00
|
|
|
$task = id(new PhabricatorWorkerActiveTask())->load($this->id);
|
2012-10-31 23:22:32 +01:00
|
|
|
if (!$task) {
|
|
|
|
$task = id(new PhabricatorWorkerArchiveTask())->load($this->id);
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:36:32 +01:00
|
|
|
if (!$task) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:22:32 +01:00
|
|
|
$result_success = PhabricatorWorkerArchiveTask::RESULT_SUCCESS;
|
|
|
|
$can_retry = ($task->isArchived()) &&
|
|
|
|
($task->getResult() != $result_success);
|
|
|
|
|
|
|
|
$can_cancel = !$task->isArchived();
|
|
|
|
$can_release = (!$task->isArchived()) &&
|
|
|
|
($task->getLeaseOwner());
|
|
|
|
|
|
|
|
$next_uri = $this->getApplicationURI('/task/'.$task->getID().'/');
|
|
|
|
|
2012-01-24 01:36:32 +01:00
|
|
|
if ($request->isFormPost()) {
|
|
|
|
switch ($this->action) {
|
2012-10-31 23:22:32 +01:00
|
|
|
case 'retry':
|
|
|
|
if ($can_retry) {
|
|
|
|
$task->unarchiveTask();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'cancel':
|
|
|
|
if ($can_cancel) {
|
|
|
|
// Forcibly break the lease if one exists, so we can archive the
|
|
|
|
// task.
|
|
|
|
$task->setLeaseOwner(null);
|
|
|
|
$task->setLeaseExpires(time());
|
|
|
|
|
|
|
|
$task->archiveTask(
|
|
|
|
PhabricatorWorkerArchiveTask::RESULT_CANCELLED,
|
|
|
|
0);
|
|
|
|
}
|
2012-01-24 01:36:32 +01:00
|
|
|
break;
|
|
|
|
case 'release':
|
2012-10-31 23:22:32 +01:00
|
|
|
if ($can_release) {
|
|
|
|
$task->setLeaseOwner(null);
|
|
|
|
$task->setLeaseExpires(time());
|
|
|
|
$task->save();
|
|
|
|
}
|
2012-01-24 01:36:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-10-31 23:22:32 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI($next_uri);
|
2012-01-24 01:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($user);
|
|
|
|
|
|
|
|
switch ($this->action) {
|
2012-10-31 23:22:32 +01:00
|
|
|
case 'retry':
|
|
|
|
if ($can_retry) {
|
|
|
|
$dialog->setTitle('Really retry task?');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'The task will be put back in the queue and executed again.')));
|
2012-10-31 23:22:32 +01:00
|
|
|
$dialog->addSubmitButton('Retry Task');
|
|
|
|
} else {
|
|
|
|
$dialog->setTitle('Can Not Retry');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'Only archived, unsuccessful tasks can be retried.')));
|
2012-10-31 23:22:32 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'cancel':
|
|
|
|
if ($can_cancel) {
|
|
|
|
$dialog->setTitle('Really cancel task?');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'The work this task represents will never be performed if you '.
|
|
|
|
'cancel it. Are you sure you want to cancel it?')));
|
2012-10-31 23:22:32 +01:00
|
|
|
$dialog->addSubmitButton('Cancel Task');
|
|
|
|
} else {
|
|
|
|
$dialog->setTitle('Can Not Cancel');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'Only active tasks can be cancelled.')));
|
2012-10-31 23:22:32 +01:00
|
|
|
}
|
2012-01-24 01:36:32 +01:00
|
|
|
break;
|
|
|
|
case 'release':
|
2012-10-31 23:22:32 +01:00
|
|
|
if ($can_release) {
|
|
|
|
$dialog->setTitle('Really free task lease?');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'If the process which owns the task lease is still doing work '.
|
2012-10-31 23:22:32 +01:00
|
|
|
'on it, the work may be performed twice. Are you sure you '.
|
2013-02-13 23:50:15 +01:00
|
|
|
'want to free the lease?')));
|
2012-10-31 23:22:32 +01:00
|
|
|
$dialog->addSubmitButton('Free Lease');
|
|
|
|
} else {
|
|
|
|
$dialog->setTitle('Can Not Free Lease');
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'Only active, leased tasks may have their leases freed.')));
|
2012-10-31 23:22:32 +01:00
|
|
|
}
|
2012-01-24 01:36:32 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:22:32 +01:00
|
|
|
$dialog->addCancelButton($next_uri);
|
2012-01-24 01:36:32 +01:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|