Skip to content

Commit

Permalink
Merge pull request WWBN#342 from manu0401/kill2
Browse files Browse the repository at this point in the history
Add killWorkerOnDelete option to kill ffmpeg process on dequeue
  • Loading branch information
DanielnetoDotCom authored Feb 16, 2021
2 parents 869acd8 + f86f46a commit d7e39f5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
1 change: 1 addition & 0 deletions install/checkConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
\$global['concurrent'] = 1;
\$global['hideUserGroups'] = false;
\$global['progressiveUpload'] = false;
\$global['killWorkerOnDelete'] = false;
\$mysqlHost = '{$_POST['databaseHost']}';
\$mysqlUser = '{$_POST['databaseUser']}';
Expand Down
1 change: 1 addition & 0 deletions install/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CREATE TABLE IF NOT EXISTS `encoder_queue` (
`status_obs` VARCHAR(255) NULL,
`return_vars` VARCHAR(255) NULL,
`worker_pid` INT NULL,
`worker_ppid` INT NULL,
`priority` INT(1) NULL,
`title` VARCHAR(255) NULL,
`videoDownloadedLink` VARCHAR(255) NULL,
Expand Down
84 changes: 79 additions & 5 deletions objects/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Encoder extends ObjectYPT {

protected $id, $fileURI, $filename, $status, $status_obs, $return_vars, $worker_pid, $priority, $created, $modified, $formats_id, $title, $videoDownloadedLink, $downloadedFileName, $streamers_id, $override_status;
protected $id, $fileURI, $filename, $status, $status_obs, $return_vars, $worker_ppid, $worker_pid, $priority, $created, $modified, $formats_id, $title, $videoDownloadedLink, $downloadedFileName, $streamers_id, $override_status;

static function getSearchFieldsNames() {
return array('filename');
Expand Down Expand Up @@ -95,6 +95,10 @@ function getReturn_vars() {
return $this->return_vars;
}

function getWorker_ppid() {
return intval($this->worker_ppid);
}

function getWorker_pid() {
return intval($this->worker_pid);
}
Expand Down Expand Up @@ -130,14 +134,16 @@ function setStatus($status) {
case "done":
case "error":
case "queue":
$this->setWorker_ppid(NULL);
$this->setWorker_pid(NULL);
break;
case "downloading":
case "encoding":
case "packing":
case "transferring":
default:
$this->setWorker_pid(getmypid());
$this->setWorker_ppid(getmypid());
$this->setWorker_pid(NULL);
break;
}
}
Expand All @@ -150,6 +156,10 @@ function setReturn_vars($return_vars) {
$this->return_vars = $return_vars;
}

function setWorker_ppid($worker_ppid) {
$this->worker_ppid = $worker_ppid;
}

function setWorker_pid($worker_pid) {
$this->worker_pid = $worker_pid;
}
Expand Down Expand Up @@ -558,11 +568,75 @@ static function getFromFileURI($fileURI) {
}

function isWorkerRunning() {
$ppid = $this->getWorker_ppid();
if (!is_numeric($ppid) || $ppid == 0)
return false;

exec("kill -0 ".$ppid, $output, $ppid_retval);
if ($ppid_retval != 0)
return false;

$pid = $this->getWorker_pid();
if (!is_numeric($pid) || $pid == 0)
if (!is_numeric($pid))
return false;
exec("kill -0 ".$pid, $output, $retval);
return ($retval == 0) ? true : false;

/*
* We have a parent ($ppid != 0) but no child ($pid == 0)
* when are between two formats.
*/
if ($pid != 0) {
exec("kill -0 ".$pid, $output, $pid_retval);
if ($pid_retval != 0)
return false;
}
return true;
}

function exec($cmd, &$output = array(), &$return_val = 0) {
if (function_exists("pcntl_fork")) {
if (($status = $this->getStatus()) != "encoding") {
error_log("id(".$this->getId().") status(".$status.") abort");
$return_val = 1;
return;
}
switch ($pid = pcntl_fork()) {
case -1:
$msg = "fork failed";
error_log("id(".$this->getId().") ".$msg);
$this->setStatus("error");
$this->setStatus_obs($msg);
$this->save();
break;
default:
$this->setWorker_pid($pid);
$this->save();
pcntl_wait($status);
if (pcntl_wifexited($status)) {
$return_val = pcntl_wexitstatus($status);
} else {
$return_val = 1;
}
if (pcntl_wifsignaled($status))
error_log("id=(".$this->getId()."), process ".$pid." got signal ".pcntl_wtermsig($status));
$this->setWorker_pid(NULL);
$this->save();
break;
case 0:
$argv = array("-c", $cmd);
$envp = array(
"PATH=".getenv("PATH"),
"LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH")
);
pcntl_exec("/bin/sh", $argv, $envp);
error_log("id=(".$this->getId()."), ".$cmd." failed: ".pnctl_strerror(pnctl_get_last_error()));
exit(1);
break;
}
} else {
exec($cmd, $output, $return_val);
}

return;
}

static function run($try = 0) {
Expand Down
17 changes: 9 additions & 8 deletions objects/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,18 +591,19 @@ static private function exec($format_id, $pathFileName, $destinationFile, $encod
} else {
$obj->code = $code;
error_log("AVideo-Encoder Format::exec Start Encoder [{$code}] ");
/* Make sure current process will terminate after us */
if (function_exists("apache_child_terminate"))
apache_child_terminate();

exec($code . " 1> {$global['systemRootPath']}videos/{$encoder_queue_id}_tmpFile_progress.txt 2>&1", $output, $return_val);
$encoder = new Encoder($encoder_queue_id);
$encoder->exec($code . " 1> {$global['systemRootPath']}videos/{$encoder_queue_id}_tmpFile_progress.txt 2>&1", $output, $return_val);
if ($return_val !== 0) {
//error_log("AVideo-Encoder Format::exec " . $code . " --- " . json_encode($output) . " --- ($format_id, $pathFileName, $destinationFile, $encoder_queue_id) ");
$obj->msg = print_r($output, true);
$encoder = new Encoder($encoder_queue_id);
$encoder->setStatus("error");
$encoder->setStatus_obs(json_encode($output));
$encoder->save();
if (empty($encoder->getId())) {/* dequeued */
error_log("id=(".$encoder_queue_id.") dequeued");
} else {
$encoder->setStatus("error");
$encoder->setStatus_obs(json_encode($output));
$encoder->save();
}
} else {
$obj->error = false;
}
Expand Down
2 changes: 2 additions & 0 deletions update/updateDb.v3.5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ENGINE = InnoDB;

ALTER TABLE `encoder_queue` ADD COLUMN `override_status` VARCHAR(45) NULL;

ALTER TABLE `encoder_queue` ADD COLUMN `worker_ppid` INT NULL;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Expand Down
13 changes: 13 additions & 0 deletions view/deleteQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
}
if (!empty($e->getId())) {
$obj->error = false;
$worker_pid = $e->getWorker_pid();
$worker_ppid = $e->getWorker_ppid();
$e->setStatus("error");
$e->setStatus_obs("deleted from queue");
if (!empty($global['killWorkerOnDelete'])) {
if (is_numeric($worker_pid) && $worker_pid > 0) {
exec("kill ".$worker_pid); // ignore result
}
if (is_numeric($worker_ppid) && $worker_ppid > 0) {
exec("kill ".$worker_ppid); // ignore result
}
}

$obj->msg = json_encode($e->delete());
} else {
$obj->msg = "Queue Item not found";
Expand Down

0 comments on commit d7e39f5

Please sign in to comment.