1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

mongrel_service:

* Upgraded to ServiceFB 'trunk' (but pistoned it, just in case).
* Fixed problems with ruby installations outside PATH or inside folders with spaces.
* Activate FB pedantic warnings by default (is really useful).

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@537 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
luislavena 2007-06-01 22:22:45 +00:00
parent 6a94507c85
commit ffa8bd560a
11 changed files with 144 additions and 109 deletions

View file

@ -1,5 +1,17 @@
*SVN*
* SVN *
* Upgraded to ServiceFB 'trunk' (but pistoned it, just in case).
* Fixed problems with ruby installations outside PATH or inside folders with spaces.
* Activate FB pedantic warnings by default (is really useful).
* 0.3.1 *
* Single Service (SingleMongrel) object type implemented.
* Updated Rakefile to reflect the new building steps.
* Removed SendSignal, too hackish for my taste, replaced with complete FB solution.
* Added basic Process monitoring and re-spawning.

View file

@ -39,18 +39,24 @@ setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
desc "Does a full compile, test run"
task :default => [:compile, :test]
GEM_VERSION = "0.3.1"
GEM_VERSION = "0.3.2"
GEM_NAME = "mongrel_service"
desc "Compile native code"
task :compile => [:native_lib, :native_service]
# global options shared by all the project in this Rakefile
OPTIONS = { :debug => false, :profile => false, :errorchecking => :ex, :mt => true }
OPTIONS = {
:debug => false,
:profile => false,
:errorchecking => :ex,
:mt => true,
:pedantic => true }
OPTIONS[:debug] = true if ENV['DEBUG']
OPTIONS[:profile] = true if ENV['PROFILE']
OPTIONS[:errorchecking] = :exx if ENV['EXX']
OPTIONS[:pedantic] = false if ENV['NOPEDANTIC']
# ServiceFB namespace (lib)
namespace :lib do

View file

@ -1,24 +1,8 @@
'#--
'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
'#
'# Permission is hereby granted, free of charge, to any person obtaining
'# a copy of this software and associated documentation files (the
'# "Software"), to deal in the Software without restriction, including
'# without limitation the rights to use, copy, modify, merge, publish,
'# distribute, sublicense, and/or sell copies of the Software, and to
'# permit persons to whom the Software is furnished to do so, subject to
'# the following conditions:
'#
'# The above copyright notice and this permission notice shall be
'# included in all copies or substantial portions of the Software.
'#
'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
'# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
'# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
'# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
'# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'# This source code is released under the MIT License.
'# See MIT-LICENSE file for details
'#++
#include once "ServiceFB.bi"
@ -113,7 +97,7 @@ namespace svc
'# to avoid SCM shut us down.
'# is not for the the end-user (*you*) to access it, but implemented in this
'# way to reduce needed to pass the right service reference each time
sub ServiceProcess.UpdateState(state as DWORD, checkpoint as integer = 0, waithint as integer = 0)
sub ServiceProcess.UpdateState(byval state as DWORD, byval checkpoint as integer = 0, byval waithint as integer = 0)
_dprint("ServiceProcess.UpdateState()")
'# set the state
select case state
@ -162,7 +146,7 @@ namespace svc
'# (if they take too much time).
'# by default we set a wait hint gap of 10 seconds, but you could specify how many
'# you could specify how many seconds more will require your *work*
sub ServiceProcess.StillAlive(waithint as integer = 10)
sub ServiceProcess.StillAlive(byval waithint as integer = 10)
dim as integer checkpoint
_dprint("ServiceProcess.StillAlive()")
@ -259,7 +243,7 @@ namespace svc
'# because it is a global _main for all the services in the table, looking up
'# in the references for the right service is needed prior registering its
'# control handler.
private sub _main(argc as DWORD, argv as LPSTR ptr)
private sub _main(byval argc as DWORD, byval argv as LPSTR ptr)
dim success as integer
dim service as ServiceProcess ptr
dim run_mode as string
@ -352,7 +336,7 @@ namespace svc
service->UpdateState(SERVICE_RUNNING)
if not (service->onStart = 0) then
_dprint("dispatch onStart() as new thread")
service->_threadHandle = threadcreate(service->onStart, cint(service))
service->_threadHandle = threadcreate(service->onStart, service)
'# my guess? was a hit!
end if
@ -387,7 +371,7 @@ namespace svc
'# (as callback from service manager).
'# we process each control codes and perform the actions using the pseudo-events (callbacks)
'# also we use lpContext to get the right reference when _main registered the control handler.
private function _control_ex(dwControl as DWORD, dwEventType as DWORD, lpEventData as LPVOID, lpContext as LPVOID) as DWORD
private function _control_ex(byval dwControl as DWORD, byval dwEventType as DWORD, byval lpEventData as LPVOID, byval lpContext as LPVOID) as DWORD
dim result as DWORD
dim service as ServiceProcess ptr

View file

@ -1,24 +1,8 @@
'#--
'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
'#
'# Permission is hereby granted, free of charge, to any person obtaining
'# a copy of this software and associated documentation files (the
'# "Software"), to deal in the Software without restriction, including
'# without limitation the rights to use, copy, modify, merge, publish,
'# distribute, sublicense, and/or sell copies of the Software, and to
'# permit persons to whom the Software is furnished to do so, subject to
'# the following conditions:
'#
'# The above copyright notice and this permission notice shall be
'# included in all copies or substantial portions of the Software.
'#
'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
'# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
'# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
'# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
'# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'# This source code is released under the MIT License.
'# See MIT-LICENSE file for details
'#++
#if __FB_VERSION__ <> "0.17"
@ -63,10 +47,10 @@ namespace svc '# fb.svc
'# methods (public)
declare sub Run()
declare sub StillAlive(as integer = 10)
declare sub StillAlive(byval as integer = 10)
'# helper methods (private)
declare sub UpdateState(as DWORD, as integer = 0, as integer = 0)
declare sub UpdateState(byval as DWORD, byval as integer = 0, byval as integer = 0)
'# pseudo-events
'# for onInit you should return FALSE (0) in case you want to abort

View file

@ -89,6 +89,9 @@ namespace utils '# fb.svc.utils
'# now the the name
parent_name = _process_name(parent_pid)
if (parent_name = "<unknown>") then
parent_name = _process_name_dyn_psapi(parent_pid)
end if
_dprint("Parent Name: " + parent_name)
'# this process started as service?
@ -185,7 +188,7 @@ namespace utils '# fb.svc.utils
'# now, fire the main loop (onStart)
if not (service->onStart = 0) then
'# create the thread
working_thread = threadcreate(service->onStart, cint(service))
working_thread = threadcreate(service->onStart, service)
end if
print "Service is in running state."
@ -330,7 +333,7 @@ namespace utils '# fb.svc.utils
'# that launched that process.
'# on fail, it will return 0
'# Thanks to MichaelW (FreeBASIC forums) for his help about this.
private function _parent_pid(PID as uinteger) as uinteger
private function _parent_pid(byval PID as uinteger) as uinteger
dim as uinteger result
dim as HANDLE hProcessSnap
dim as PROCESSENTRY32 pe32
@ -358,7 +361,7 @@ namespace utils '# fb.svc.utils
'# _process_name is used to retrieve the name (ImageName, BaseModule, whatever) of the PID you
'# pass to it. if no module name was found, it should return <unknown>
private function _process_name(PID as uinteger) as string
private function _process_name(byval PID as uinteger) as string
dim result as string
dim hProcess as HANDLE
dim hMod as HMODULE
@ -386,6 +389,92 @@ namespace utils '# fb.svc.utils
result = trim(result)
return result
end function
'# _process_name_dyn_psapi is a workaround for some issues with x64 versions of Windows.
'# by default, 32bits process can't query information from 64bits modules.
private function _process_name_dyn_psapi(byval PID as uinteger) as string
dim result as string
dim chop as uinteger
dim zresult as zstring * MAX_PATH
dim hLib as any ptr
dim hProcess as HANDLE
dim cbNeeded as DWORD
dim GetProcessImageFileName as function (byval as HANDLE, byval as LPTSTR, byval as DWORD) as DWORD
'# assign "<unknown>" to process name, allocate MAX_PATH (260 bytes)
zresult = "<unknown>" + chr(0)
'# get dynlib
hLib = dylibload("psapi.dll")
if not (hlib = 0) then
GetProcessImageFileName = dylibsymbol(hlib, "GetProcessImageFileNameA")
if not (GetProcessImageFileName = 0) then
'# get a handle to the Process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, PID)
'# if valid, get the process name
if not (hProcess = NULL) then
cbNeeded = sizeof(zresult)
if (GetProcessImageFileName(hProcess, @zresult, cbNeeded) = 0) then
_dprint("Error with GetProcessImageFileName")
_dprint("GetLastError: " + str(GetLastError()) + _show_error())
else
result = zresult
chop = InStrRev(0, result, "\")
if (chop > 0) then
result = mid(result, chop + 1, (len(result) - chop))
end if
end if
else
_dprint("Error with OpenProcess")
_dprint("GetLastError: " + str(GetLastError()) + _show_error())
end if
CloseHandle(hProcess)
else
_dprint("Unable to get a reference to dynamic symbol GetProcessImageFileNameA.")
end if
else
_dprint("Unable to dynamic load psapi.dll")
end if
'# return a trimmed result
'result = trim(result)
return result
end function
private function _show_error() as string
dim buffer as string * 1024
dim p as integer
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,_
0,_
GetLastError(),_
0,_
strptr(buffer),_
1024,_
0 )
buffer = rtrim(buffer)
p = instr(buffer, chr(13))
if p then buffer = left(buffer, p - 1)
return buffer
end function
private function InStrRev(byval start as uinteger = 0, byref src as string, byref search as string) as uinteger
dim lensearch as uinteger = len(search)
dim as uinteger b, a = 0, exit_loop = 0
do
b = a
a += 1
a = instr(a, src, search)
if start >= lensearch then if a + lensearch > start then exit_loop = 1
loop while (a > 0) and (exit_loop = 0)
return b
end function
end namespace '# fb.svc.utils
end namespace '# fb.svc
end namespace '# fb

View file

@ -1,24 +1,8 @@
'#--
'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
'#
'# Permission is hereby granted, free of charge, to any person obtaining
'# a copy of this software and associated documentation files (the
'# "Software"), to deal in the Software without restriction, including
'# without limitation the rights to use, copy, modify, merge, publish,
'# distribute, sublicense, and/or sell copies of the Software, and to
'# permit persons to whom the Software is furnished to do so, subject to
'# the following conditions:
'#
'# The above copyright notice and this permission notice shall be
'# included in all copies or substantial portions of the Software.
'#
'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
'# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
'# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
'# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
'# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'# This source code is released under the MIT License.
'# See MIT-LICENSE file for details
'#++
#if __FB_VERSION__ <> "0.17"

View file

@ -1,24 +1,8 @@
'#--
'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
'#
'# Permission is hereby granted, free of charge, to any person obtaining
'# a copy of this software and associated documentation files (the
'# "Software"), to deal in the Software without restriction, including
'# without limitation the rights to use, copy, modify, merge, publish,
'# distribute, sublicense, and/or sell copies of the Software, and to
'# permit persons to whom the Software is furnished to do so, subject to
'# the following conditions:
'#
'# The above copyright notice and this permission notice shall be
'# included in all copies or substantial portions of the Software.
'#
'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
'# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
'# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
'# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
'# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'# This source code is released under the MIT License.
'# See MIT-LICENSE file for details
'#++
'##################################################################
@ -37,8 +21,8 @@ namespace svc
declare sub _terminate() destructor
'# global service procedures (private)
declare sub _main(as DWORD, as LPSTR ptr)
declare function _control_ex(as DWORD, as DWORD, as LPVOID, as LPVOID) as DWORD
declare sub _main(byval as DWORD, byval as LPSTR ptr)
declare function _control_ex(byval as DWORD, byval as DWORD, byval as LPVOID, byval as LPVOID) as DWORD
declare sub _run()
'# global references helper

View file

@ -1,24 +1,8 @@
'#--
'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
'#
'# Permission is hereby granted, free of charge, to any person obtaining
'# a copy of this software and associated documentation files (the
'# "Software"), to deal in the Software without restriction, including
'# without limitation the rights to use, copy, modify, merge, publish,
'# distribute, sublicense, and/or sell copies of the Software, and to
'# permit persons to whom the Software is furnished to do so, subject to
'# the following conditions:
'#
'# The above copyright notice and this permission notice shall be
'# included in all copies or substantial portions of the Software.
'#
'# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
'# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
'# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
'# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
'# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'# This source code is released under the MIT License.
'# See MIT-LICENSE file for details
'#++
'##################################################################
@ -43,8 +27,14 @@ namespace utils '# fb.svc.utils
'# internals functions used to get Parent PID and Process Name
'# using this we automatically determine if the service was started by SCM
'# or by the user, from commandline or from explorer
declare function _parent_pid(as uinteger) as uinteger
declare function _process_name(as uinteger) as string
declare function _parent_pid(byval as uinteger) as uinteger
declare function _process_name(byval as uinteger) as string
declare function _process_name_dyn_psapi(byval as uinteger) as string
declare function _show_error() as string
'# InStrRev (authored by ikkejw @ freebasic forums)
'# http://www.freebasic.net/forum/viewtopic.php?p=49315#49315
declare function InStrRev(byval as uinteger = 0, byref as string, byref as string) as uinteger
'# use a signal (condition) in the console mode to know
'# when the service should be stopped.

View file

@ -57,7 +57,8 @@ namespace mongrel_service
'# because mongrel_service executable (.exe) is located in the same
'# folder than mongrel_rails ruby script, we complete the path with
'# EXEPATH + "\mongrel_rails" to make it work.
mongrel_cmd = "ruby.exe " + EXEPATH + "\mongrel_rails start"
'# FIXED ruby installation outside PATH and inside folders with spaces
mongrel_cmd = !"\"" + EXEPATH + !"\\ruby.exe" + !"\" " + !"\"" + EXEPATH + !"\\mongrel_rails" + !"\"" + " start"
'# due lack of inheritance, we use single_mongrel_ref as pointer to
'# SingleMongrel instance. now we should call StillAlive

View file

@ -161,7 +161,7 @@ namespace process
'# Terminate(PID) will hook the special console handler (_child_console_handler)
'# and try sending CTRL_C_EVENT, CTRL_BREAK_EVENT and TerminateProcess
'# in case of the first two fails.
function Terminate(pid as uinteger) as BOOL
function Terminate(byval pid as uinteger) as BOOL
dim result as BOOL
dim success as BOOL
dim exit_code as DWORD

View file

@ -211,6 +211,7 @@ module FreeBASIC
cmdline << "-#{@options[:errorchecking].to_s}" if @options.has_key?(:errorchecking)
cmdline << "-profile" if (@options.has_key?(:profile) && @options[:profile] == true)
cmdline << "-mt" if (@options.has_key?(:mt) && @options[:mt] == true)
cmdline << "-w pedantic" if (@options.has_key?(:pedantic) && @options[:pedantic] == true)
cmdline << "-c #{source}"
cmdline << "-o #{target}"
cmdline << "-m #{main}" unless main.nil?