Skip to content

Commit

Permalink
Add Service.call method
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Moravec committed May 30, 2014
1 parent 61fc77d commit f7ca0b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 11 additions & 0 deletions library/runlevel/src/modules/Service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ def initialize
@error = ""
end

# Send whatever command you need to call for a specific service
# Command name must be a member of instance methods defined in SystemdUnit class
# @return [Boolean] Result of the action, true means success
def call command_name, service_name
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service
result = service.send(command_name)
self.error = service.error
result
end

# Check if service is active/running
#
# @param [String] name service name
Expand Down
11 changes: 11 additions & 0 deletions library/runlevel/test/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def stub_service_with method, result
stub_services
end

describe ".call" do
it "raises error if command name or service name parameter is missing" do
expect(Service.call(:start)).to raise_error
end

it "returns the result of the original result of the command call" do
stub_service_with(:"try-restart", false)
expect(Service.call(:"try-restart", "sshd")).to be_false
end
end

describe ".Active" do
it "returns true if a service is active" do
expect(Service.Active('sshd')).to be_true
Expand Down
4 changes: 1 addition & 3 deletions library/systemd/src/modules/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def restart
return false unless stop

sleep(1)

# Return false if service was not started succesffuly
return false unless start
start
end

private
Expand Down

0 comments on commit f7ca0b4

Please sign in to comment.