win32-service-0.6.1 using a Rails Model

D

david

Windows 2000 SP4
Ruby 1.8.6
Rail 1.2.6

I'm trying to create a Rail application for our internal use to
provide a one stop access to services. I'm currently trying to get the
Server.status, Service.stop and Service.start to work through a model
in Rails. The Service.* functions work well through Ruby itself, but
seems to have problem through a Rails Model. I've been trying to
display the status of the MySQL service.

*** Model

require "win32/service"

class Servicectl

include Win32

attr_reader :state

def servicestatus(service_name, host_name)
@state = Service.status(service_name, host_name)
end

def servicestart(service_name, host_name)
Service.start(service_name, host_name)
end

def servicestop(service_name, host_name)
Service.stop(service_name, host_name)
end

end

*** Controller

class ServicectlController < ApplicationController

def show
service_state = Servicectl.new
service_state.servicestatus("MySQL",nil)
@status = service_state.state
end

end

*** Show View

<%= @status %>

The following error is posted to the development.log.

Processing ServicectlController#show (for 127.0.0.1 at 2008-04-19
11:53:46) [GET]
Session ID: d446bf0a6cad825dbf5d1a78e6853706
Parameters: {"action"=>"show", "controller"=>"servicectl"}

Win32::Service::Error (T):
c:/ruby/lib/ruby/gems/1.8/gems/win32-service-0.6.1-x86-mswin32-60/
lib/win32/service.rb:920:in `status'
C:/Shared/SourceCode/Rails/sandbox/app/models/servicectl.rb:14:in
`servicestatus'
C:/Shared/SourceCode/Rails/sandbox/app/controllers/
servicectl_controller.rb:9:in `show'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `perform_action_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:696:in `call_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:688:in `perform_action_without_benchmark'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/rescue.rb:83:in `perform_action'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `process_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:684:in
`process_without_session_management_support'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/session_management.rb:114:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:334:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/dispatcher.rb:41:in
`dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
113:in `handle_dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
79:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
63:in `dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/servers/
webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:342:in `new_constants_in'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/server.rb:
39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3

I have no idea why it is failing, I'm hoping someone might have a clue
for me.

For reference this is the working code which works well though Ruby
itself.

*** service.rb

require "win32/service"

class ServiceController

include Win32

attr_accessor :state

def status(service_name, host_name)
@state = Service.status(service_name, host_name)
end

def start(service_name, host_name)
Service.start(service_name, host_name)
end

def stop(service_name, host_name)
Service.stop(service_name, host_name)
end

end

*** Output:

C:\Shared\SourceCode\Ruby>ruby service.rb
running

Thanks for all your time,
Dave
 
D

Daniel Berger

david said:
Windows 2000 SP4
Ruby 1.8.6
Rail 1.2.6

I'm trying to create a Rail application for our internal use to
provide a one stop access to services. I'm currently trying to get the
Server.status, Service.stop and Service.start to work through a model
in Rails. The Service.* functions work well through Ruby itself, but
seems to have problem through a Rails Model. I've been trying to
display the status of the MySQL service.

The following error is posted to the development.log.

Processing ServicectlController#show (for 127.0.0.1 at 2008-04-19
11:53:46) [GET]
Session ID: d446bf0a6cad825dbf5d1a78e6853706
Parameters: {"action"=>"show", "controller"=>"servicectl"}

Win32::Service::Error (T):
c:/ruby/lib/ruby/gems/1.8/gems/win32-service-0.6.1-x86-mswin32-60/
lib/win32/service.rb:920:in `status'
C:/Shared/SourceCode/Rails/sandbox/app/models/servicectl.rb:14:in
`servicestatus'

<snip>

The line number indicates that OpenService() function call failed, but
I'm not sure why. I've forwarded this to the win32utils-devel list. I
can only speculate at this point that it's something to do with the way
it's interacting with WEBrick, but I'm not sure.

We'll keep you posted.

Regards,

Dan
 
G

Gordon Thiesfeld

Windows 2000 SP4
Ruby 1.8.6
Rail 1.2.6

I'm trying to create a Rail application for our internal use to
provide a one stop access to services. I'm currently trying to get the
Server.status, Service.stop and Service.start to work through a model
in Rails. The Service.* functions work well through Ruby itself, but
seems to have problem through a Rails Model. I've been trying to
display the status of the MySQL service.

*** Model

require "win32/service"

class Servicectl

include Win32

attr_reader :state

def servicestatus(service_name, host_name)
@state = Service.status(service_name, host_name)
end

def servicestart(service_name, host_name)
Service.start(service_name, host_name)
end

def servicestop(service_name, host_name)
Service.stop(service_name, host_name)
end

end

*** Controller

class ServicectlController < ApplicationController

def show
service_state = Servicectl.new
service_state.servicestatus("MySQL",nil)
@status = service_state.state
end

end

*** Show View

<%= @status %>

The following error is posted to the development.log.

Processing ServicectlController#show (for 127.0.0.1 at 2008-04-19
11:53:46) [GET]
Session ID: d446bf0a6cad825dbf5d1a78e6853706
Parameters: {"action"=>"show", "controller"=>"servicectl"}

Win32::Service::Error (T):
c:/ruby/lib/ruby/gems/1.8/gems/win32-service-0.6.1-x86-mswin32-60/
lib/win32/service.rb:920:in `status'
C:/Shared/SourceCode/Rails/sandbox/app/models/servicectl.rb:14:in
`servicestatus'
C:/Shared/SourceCode/Rails/sandbox/app/controllers/
servicectl_controller.rb:9:in `show'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `perform_action_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:696:in `call_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:688:in `perform_action_without_benchmark'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/rescue.rb:83:in `perform_action'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `process_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:684:in
`process_without_session_management_support'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/session_management.rb:114:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:334:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/dispatcher.rb:41:in
`dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
113:in `handle_dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
79:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
63:in `dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/servers/
webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:342:in `new_constants_in'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/server.rb:
39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3

I have no idea why it is failing, I'm hoping someone might have a clue
for me.

For reference this is the working code which works well though Ruby
itself.

*** service.rb

require "win32/service"

class ServiceController

include Win32

attr_accessor :state

def status(service_name, host_name)
@state = Service.status(service_name, host_name)
end

def start(service_name, host_name)
Service.start(service_name, host_name)
end

def stop(service_name, host_name)
Service.stop(service_name, host_name)
end

end

*** Output:

C:\Shared\SourceCode\Ruby>ruby service.rb
running

Thanks for all your time,
Dave


I can't offer help with win32-service, but I could suggest an
alternative. You could do what you're wanting to do with Windows
services via WMI, and I've written a ruby-wmi gem that has a syntax
similar to Activerecord. My rails is rusty, but here's an idea.

*** Model

#nothing in here. ;)


*** Controller

require 'ruby-wmi'

class ServicesController < ApplicationController
# GET /services
# GET /services.xml
def index
@services = WMI::Win32_Service.find:)all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @services }
end
end

# GET /services/1
# GET /services/1.xml
def show
puts params
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
@status = service.state
end

def stop
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
service.stopservice
end

def start
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
service.startservice
end

end

*** Index view

<table>
<tr>
</tr>

<% for service in @services %>
<tr>
<td><%= link_to service.name, service.name %></td>
<td><%= service.state %></td>
<td><%= service.startmode %></td>
</tr>
<% end %>
</table>

*** Show view

<%= @status %>
 
R

Roger Pack

Win32::Service::Error (T):
c:/ruby/lib/ruby/gems/1.8/gems/win32-service-0.6.1-x86-mswin32-60/
lib/win32/service.rb:920:in `status'
It is running as the same user with enough privileges, I'd imagine?
 
D

david

Windows 2000 SP4
Ruby 1.8.6
Rail 1.2.6
I'm trying to create a Rail application for our internal use to
provide a one stop access to services. I'm currently trying to get the
Server.status, Service.stop and Service.start to work through a model
inRails. The Service.* functions work well through Ruby itself, but
seems to have problem through aRailsModel. I've been trying to
display the status of the MySQL service.
*** Model
require "win32/service"
class Servicectl
include Win32
attr_reader :state
def servicestatus(service_name, host_name)
@state = Service.status(service_name, host_name)
end
def servicestart(service_name, host_name)
Service.start(service_name, host_name)
end
def servicestop(service_name, host_name)
Service.stop(service_name, host_name)
end

*** Controller
class ServicectlController < ApplicationController
def show
service_state = Servicectl.new
service_state.servicestatus("MySQL",nil)
@status = service_state.state
end

*** Show View
<%= @status %>
The following error is posted to the development.log.
Processing ServicectlController#show (for 127.0.0.1 at 2008-04-19
11:53:46) [GET]
Session ID: d446bf0a6cad825dbf5d1a78e6853706
Parameters: {"action"=>"show", "controller"=>"servicectl"}
Win32::Service::Error (T):
c:/ruby/lib/ruby/gems/1.8/gems/win32-service-0.6.1-x86-mswin32-60/
lib/win32/service.rb:920:in `status'
C:/Shared/SourceCode/Rails/sandbox/app/models/servicectl.rb:14:in
`servicestatus'
C:/Shared/SourceCode/Rails/sandbox/app/controllers/
servicectl_controller.rb:9:in `show'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:1101:in `perform_action_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:696:in `call_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:688:in `perform_action_without_benchmark'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/rescue.rb:83:in `perform_action'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:435:in `process_without_filters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/filters.rb:684:in
`process_without_session_management_support'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/session_management.rb:114:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
action_controller/base.rb:334:in `process'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/dispatcher.rb:41:in
`dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
113:in `handle_dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
79:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/webrick_server.rb:
63:in `dispatch'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/servers/
webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:342:in `new_constants_in'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in `require'
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/server.rb:
39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3
I have no idea why it is failing, I'm hoping someone might have a clue
for me.
For reference this is the working code which works well though Ruby
itself.
*** service.rb
require "win32/service"
class ServiceController
include Win32
attr_accessor :state
def status(service_name, host_name)
@state = Service.status(service_name, host_name)
end
def start(service_name, host_name)
Service.start(service_name, host_name)
end
def stop(service_name, host_name)
Service.stop(service_name, host_name)
end

*** Output:
C:\Shared\SourceCode\Ruby>ruby service.rb
running
Thanks for all your time,
Dave

I can't offer help with win32-service, but I could suggest an
alternative. You could do what you're wanting to do with Windows
services via WMI, and I've written a ruby-wmi gem that has a syntax
similar to Activerecord. Myrailsis rusty, but here's an idea.

*** Model

#nothing in here. ;)

*** Controller

require 'ruby-wmi'

class ServicesController < ApplicationController
# GET /services
# GET /services.xml
def index
@services = WMI::Win32_Service.find:)all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @services }
end
end

# GET /services/1
# GET /services/1.xml
def show
puts params
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
@status = service.state
end

def stop
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
service.stopservice
end

def start
service = WMI::Win32_Service.find:)first, :conditions => {:name =>
params['id']})
service.startservice
end

end

*** Index view

<table>
<tr>
</tr>

<% for service in @services %>
<tr>
<td><%= link_to service.name, service.name %></td>
<td><%= service.state %></td>
<td><%= service.startmode %></td>
</tr>
<% end %>
</table>

*** Show view

<%= @status %>

Thanks Gordon, I will give that option a try.

Dave
 
G

Gordon Thiesfeld

Roger, yes I'm a local Administrator on the box.

Dave

Also, I'll probably be releasing a new version soon (this weekend?)
which will have a WMI::Base#set_wmi_class_name method, similar to
ActiveRecord's set_table_name for legacy tables.

Then you could write your model like this:

class Service << WMI::Base
set_wmi_class_name 'Win32_Service'
end

and in your controller:

def index
@services = Service.find:)all)
end

I like the looks of this better, I just never really had a need for it
before now.

Gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top