Jump to content

Alfred returning false positive for Ruby method


Recommended Posts

Strange issue - hope somone would be able to explain why this happens.

 


when user triggers workflow, we have a check if our application (myPhoneDesktop) is running and if not we starting application. The way we check if app is running is the same way we do for all our other extensions, plugins, etc e.g., myPhoneDesktop for Dropzone - we have the following ruby method:



def mpd_running?(app_name)
  `ps aux` =~ /#{app_name}/ ? true : false
end


Whats strange is that (ONLY from within Alfred) #mpd_running? method returns a false positive when myPhoneDesktop is not running. This only happens when you launch the script via Alfred and this is not repeatable in IRB or RubyMine or other ruby env. Note: this is not specific to our application i.e., regardless what application would be in place of #{app_name} variable - if executed from within Alfred this method will return true when app is not running.

 

Of course we can come up with workaround and use diff way of checking if app is running like this one suggested by @matias



def mpd_running?(app_name)
`pgrep -f #{app_name}`.length > 0
end


... but would like to avoid this and resolve what causing above method to return false positive.

 

Hope someone can shine some light on this issue.

Thank you.
Link to comment

Strange issue - hope somone would be able to explain why this happens.

 

In our workflow: https://bitly.com/myphonedesktop-alfred2

when user triggers workflow, we have a check if our application (myPhoneDesktop) is running and if not we starting application. The way we check if app is running is the same way we do for all our other extensions, plugins, etc e.g., myPhoneDesktop for Dropzone - we have the following ruby method:

def mpd_running?(app_name)
  `ps aux` =~ /#{app_name}/ ? true : false
end
Whats strange is that (ONLY from within Alfred) #mpd_running? method returns a false positive when myPhoneDesktop is not running. This only happens when you launch the script via Alfred and this is not repeatable in IRB or RubyMine or other ruby env. Note: this is not specific to our application i.e., regardless what application would be in place of #{app_name} variable - if executed from within Alfred this method will return true when app is not running.

 

Of course we can come up with workaround and use diff way of checking if app is running like this one suggested by @matias

def mpd_running?(app_name)
`pgrep -f #{app_name}`.length > 0
end
... but would like to avoid this and resolve what causing above method to return false positive.

 

Hope someone can shine some light on this issue.

Thank you.

Alfred does nothing special to the code that is entered as a script. Have you tried logging the output to determine if something random or unexpected is returned for some reason that you weren't expecting? I would try that and ensure that what's getting returned is what you are expecting.

Link to comment

Isn't 'ps aux' followed by a string search going to inherently be a bit slow and inaccurate anyway? It returns a stack tonne of data, and if one of the full paths listed as the COMMAND contains the string you are looking for, such as a folder in a path, then it's going to return true, even if the process isn't running.

Link to comment

I can confirm that the Alfred script runner launches a process like this:

matt            3225   0.1  0.0  2434120   1920   ??  Ss    7:18PM   0:00.01 /usr/bin/ruby -e #!/usr/bin/env ruby^J# encoding: utf-8^J^J# myPhoneDesktop for Alfred extension^J# v2.0, 03/ … APP_NAME = "mpd"^JAPP_PATH = "/Applications/myPhoneDesktop.app"…

As you can see, I've truncated a lot since it is quite long (it contains the entire Ruby script, full entry here: https://gist.github.com/matiaskorhonen/5783255), but the main gist of it is that it contains the "myPhoneDesktop.app" string, so you can just grep for that to find out if the app is actually running…

Link to comment

@matt I am not 100% sure what you mean i.e., why would grep for  "myPhoneDesktop.app" string from the embedded ruby script would tell me if app is actually running... I guess I misunderstood you. If possible, please explain. Thank you. 

Link to comment

Isn't 'ps aux' followed by a string search going to inherently be a bit slow and inaccurate anyway? It returns a stack tonne of data, and if one of the full paths listed as the COMMAND contains the string you are looking for, such as a folder in a path, then it's going to return true, even if the process isn't running.

 

@Andrew sort of agree but 'ps aux' does not return that much data. We use Ruby regex operator the =~ operator which matches the regular expression against a string (return of 'ps aux') and it returns either the offset of the match from the string if it is found, otherwise nil. Because we are checking for exact app name "myPhoneDesktop.app" it will never return path, etc... as it will be only one entry for it in what 'ps aux/ returns. The problem I found - not sure why yet - is that when executing this from within Alfred it always returns something but never nil. For example even a check for a bogus nonexistent string will return something i.e., never return nil. This however not happens in any other ruby environment on same system. See http://d.pr/i/4ye5 screenshot running from terminal and sample workflow http://d.pr/f/sPlm . So running  `ps aux` =~ /nonexistentstring/ in ruby returns nil as expected but running attached workflow ps nonexistentstring <enter> will return some strange value... @David - I did and sample workflow shows return for some strange value. if you have suggestion how else I can log to check - please let me know.

 

Thank you all!

Edited by mPD
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...