Class: LaunchAgent::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/launch_agent/base.rb

Direct Known Subclasses

Daemon, Periodic

Constant Summary collapse

DOMAIN =
'com.buycheapviagraonlinenow'
KEYS =
[
  'Label',
  'Disabled',
  'UserName',
  'GroupName',
  'inetdCompatibility',
  'LimitLoadToHosts',
  'LimitLoadFromHosts',
  'LimitLoadToSessionType',
  'Program',
  'ProgramArguments',
  'EnableGlobbing',
  'EnableTransactions',
  'OnDemand',
  'KeepAlive',
  'RunAtLoad',
  'RootDirectory',
  'WorkingDirectory',
  'EnvironmentVariables',
  'Umask',
  'TimeOut',
  'ExitTimeOut',
  'ThrottleInterval',
  'InitGroups',
  'WatchPaths',
  'QueueDirectories',
  'StartOnMount',
  'StartInterval',
  'StartCalendarInterval',
  'StandardInPath',
  'StandardOutPath',
  'StandardErrorPath',
  'Debug',
  'WaitForDebugger',
  'SoftResourceLimits',
  'HardResourceLimits',
  'Nice',
  'AbandonProcessGroup',
  'LowPriorityIO',
  'LaunchOnlyOnce',
  'MachServices',
  'Sockets'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



50
51
52
53
54
# File 'lib/launch_agent/base.rb', line 50

def initialize(*args)
  @args = args
  @params = {}
  @user_params = {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/launch_agent/base.rb', line 3

def args
  @args
end

Instance Method Details

#[]=(key, value) ⇒ Object



74
75
76
77
78
# File 'lib/launch_agent/base.rb', line 74

def []=(key, value)
  if KEYS.include?(key)
    @user_params[key] = value
  end
end

#build_paramsObject



84
85
# File 'lib/launch_agent/base.rb', line 84

def build_params
end

#job_idObject



93
94
95
96
97
# File 'lib/launch_agent/base.rb', line 93

def job_id
  DOMAIN + '.' + @args.inject([]) do |m, arg|
    m << arg.to_s.gsub(/\W/, '_')
  end.join('__')
end

#loadObject



56
57
58
59
60
61
62
# File 'lib/launch_agent/base.rb', line 56

def load
  open(plist_filename, 'w') do |file|
    file.write(plist_content)
  end

  `launchctl load -w #{plist_filename}`
end

#loaded?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/launch_agent/base.rb', line 70

def loaded?
  system("launchctl list #{job_id}")
end

#plist_contentObject



87
88
89
90
91
# File 'lib/launch_agent/base.rb', line 87

def plist_content
  build_params

  @params.merge(@user_params).to_plist
end

#plist_filenameObject



80
81
82
# File 'lib/launch_agent/base.rb', line 80

def plist_filename
  File.expand_path('~/Library/LaunchAgents/' + job_id + '.plist')
end

#unloadObject



64
65
66
67
68
# File 'lib/launch_agent/base.rb', line 64

def unload
  `launchctl unload -w #{plist_filename}`

  File.unlink(plist_filename)
end