Optimise processExists

suggestion-added

#1

As discussed with @JMSolo, here Update breaks Quickbox web and Deluge web

I had an idea to make the processExists function in config.php just make one call to the ps function per user. I had another try at this last night, and got it working. Its not a complicated thing, just php is not a language I write in usually.

Global variables declared at the top.

global $last_user_name;
global $last_process_list;

function processExists($processName, $username) {

global $last_user_name;
global $last_process_list;

$exists = false;

 if ($username !== $last_user_name) {

    $last_user_name = $username;
	exec("ps axo user:20,comm |grep $username | xargs", $result);
	$last_process_list = $result[0];
	}
	
	
if (strpos($last_process_list, $processName) !== false) {

	$exists = true;
}
 return $exists;
}

I rearranged the list of calls to this function so the same $username is one after another. This reduces the server calls from 17 to 6.

I only have a small VPS, so only Deluge and ruTorrent installed, so it could probably use more testing.

Hope this helps


#2

This is a wonderful idea. When running just rTorrent and Deluge. The only downside so far is this produces bad status readings on other applications such as Sickrage, Sonarr, Jackett, CouchPotato etc. However, with a little elbow grease this will make a wonderful addition to a future iteration.


#3

Sorry to hear that, does the process name not match the command?

I’ve had more fun today making the chart show upload and download simultaneously. Ive also scaled it down to reflect actual % of bandwidth currently being used. (Rather than a very small amount filling the whole chart making it look like the server is busy - if you follow me)

Here it is finishing downloading something and then starting to seed it.

I like learning new stuff and keeping my programming brain working :slight_smile:


#4

Part of what it is results from python processes being read and others which are .exe variables are not. Thus, Plex, PlexRequests etc will register. I will however look through it later today after my meeting to see what can be done about those issues as the results are clearly in favor of your solution.

PS: That bandwidth chart is exactly something I was wanting included!

Can we add you as part of the development team within our GitLab? It looks like you have some solid skills and we could really use you. :slight_smile:


#5

@JMSolo I ended up fixing this quietly in our current config.php in terms of shell exec by modifying the ps command you were using to include cmd which is the actual run command used to start the processes
eg:

ps axo user:20,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,comm,cmd

comm just pulled python, cmd pulls python couchpotato.py which then allows us to grep a match.

We just need to plug it into the exec and it should work, eg:

global $last_user_name;
global $last_process_list;

function processExists($processName, $username) {

global $last_user_name;
global $last_process_list;

$exists = false;

 if ($username !== $last_user_name) {

    $last_user_name = $username;
	exec("ps axo user:20,cmd |grep $username | xargs", $result);
	$last_process_list = $result[0];
	}
	
	
if (strpos($last_process_list, $processName) !== false) {

	$exists = true;
}
 return $exists;
}

#6

That’s very kind of you. Let me think about it, but dont worry anything I do you can have.


#7