|
|
|
|
|
by chadillac
4541 days ago
|
|
I'm not proud of this legacy code, but... it exists because it was a real world issue that would cripple a conversion server in production envs when fed certain files with timing/syncing errors as part of an automated upload and conversion process. When it would crash it would consume 100% of the cores and eat up enough RAM to force swap. This cron has been keeping ffmpeg in check for over 4 years (not 6, whoopsie) in a production environment at this point... it processes thousands of videos a day using a custom queuing and reviewing system. 1 <?php
2 /*
3 ** Cron responsible for detecting failed/hung ffmpeg
4 ** instances and killing them.
5 */
6 require_once '/lib/class/dbmysqli.php';
7
8 $output = shell_exec('ps -aeo pid,etime,args | grep ffmpeg | grep -v grep');
9
10
11
12
13 preg_match_all("/^[ ]{0,}([0-9]*)[ ]{0,}(.*?) .*?([0-9]*?)\-[0-9]*?\.flv /m", $output, $preg_out, PREG_SET_ORDER);
14
15 if (!empty($preg_out)) {
16
17 $db = new DBmysqli('dbmaster');
18
19 foreach ($preg_out as $process) {
20 $pid = intval($process[1]);
21 $etime = $process[2];
22 $queue_id = intval($process[3]);
23
24 $etime = intval(str_replace(':','',$etime));
25
26 // elapsed time >= 60:00 (1 hour)
27 if ($etime >= 6000) {
28 $fail_sql = "DELETE FROM media_upload_queue WHERE queue_id = $queue_id";
29 shell_exec("kill -9 ".$pid); // kill hung ffmpeg process
30 $db->query($fail_sql); // remove file from DB
31 // debate if life is worth living...
32 }
33 }
34 }
35 ?>
Yes I know there are better ways to do this now at the OS level, but it was a quick hack over a half decade ago and continues to work... ain't broke, don't fix it kinda deal. |
|
"This cron has been keeping ffmpeg in check for over 6+ years in a production environment at this point... it processes thousands of videos a day using a custom queuing and reviewing system."
why do we programmers always feel we need to apologize for something that we did quickly, but has been running without incident for a number of years.
take a bow my friend. that was an awesome patch you came up with all those years ago.