// controlService allows commands which change the status of the service.  It
// also waits for up to 10 seconds for the service to change to the passed
// state.
512961
func controlService(c svc.Cmd, to svc.State) error {
	// Connect to the windows service manager.
	serviceManager, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer serviceManager.Disconnect()

	service, err := serviceManager.OpenService(svcName)
	if err != nil {
		return fmt.Errorf("could not access service: %v", err)
	}
	defer service.Close()

	status, err := service.Control(c)
	if err != nil {
		return fmt.Errorf("could not send control=%d: %v", c, err)
	}

	// Send the control message.
	timeout := time.Now().Add(10 * time.Second)
	for status.State != to {
		if timeout.Before(time.Now()) {
			return fmt.Errorf("timeout waiting for service to go "+
				"to state=%d", to)
		}
		time.Sleep(300 * time.Millisecond)
		status, err = service.Query()
		if err != nil {
			return fmt.Errorf("could not retrieve service "+
				"status: %v", err)
		}
	}

	return nil
}
869371
func (s *GetReservationPurchaseRecommendationInput) SetService(v string) *GetReservationPurchaseRecommendationInput {
	s.Service = &v
	return s
}
676168
func (s *EventType) SetService(v string) *EventType {
	s.Service = &v
	return s
}
1204745
func Control(s Service, action string) error {
	var err error
	switch action {
	case ControlAction[0]:
		err = s.Start()
	case ControlAction[1]:
		err = s.Stop()
	case ControlAction[2]:
		err = s.Restart()
	case ControlAction[3]:
		err = s.Install()
	case ControlAction[4]:
		err = s.Uninstall()
	default:
		err = fmt.Errorf("Unknown action %s", action)
	}
	if err != nil {
		return fmt.Errorf("Failed to %s %v: %v", action, s, err)
	}
	return nil
}
666822
func (ep *endpoint) enableService() {
	ep.Lock()
	defer ep.Unlock()
	ep.serviceEnabled = true
}
57192
func (s *Store) RemoveService(service string) error {
	// Serialize command.
	cmd, err := json.Marshal(&removeServiceCommand{Service: service})
	if err != nil {
		return err
	}

	if _, err := s.raftApply(removeServiceCommandType, cmd); err != nil {
		return err
	}
	return nil
}
7933
func WaitService(cancel <-chan struct{}, conn client.Connection, poolID, serviceID string, checkCount func(count int) bool, checkState func(s *State, exists bool) bool) error {
	basepth := ""
	if poolID != "" {
		basepth = path.Join("/pools", poolID)
	}
	pth := path.Join(basepth, "/services", serviceID)

	logger := plog.WithFields(log.Fields{
		"serviceid": serviceID,
		"zkpath":    pth,
	})

	done := make(chan struct{})
	defer func() { close(done) }()
	for {

		// clean any bad service states
		if err := CleanServiceStates(conn, poolID, serviceID); err != nil {
			return err
		}

		// get the list of states
		ch, ev, err := conn.ChildrenW(pth, done)
		if err != nil {

			logger.WithError(err).Debug("Could not watch states for service")

			// TODO: wrap error?
			return err
		}

		// make sure the count satisfies the requirements
		if checkCount(len(ch)) {
			ok := true

			for _, stateID := range ch {
				st8log := logger.WithField("stateid", stateID)

				// parse the state id and set up the request
				hostID, _, instanceID, err := ParseStateID(stateID)
				if err != nil {

					// This should never happen, but handle it
					st8log.WithError(err).Error("Invalid state id while monitoring service")
					return err
				}

				req := StateRequest{
					PoolID:     poolID,
					HostID:     hostID,
					ServiceID:  serviceID,
					InstanceID: instanceID,
				}

				// wait for the state to satisfy the requirements
				if _, err := MonitorState(cancel, conn, req, checkState); err != nil {
					st8log.WithError(err).Debug("Stopped monitoring state")

					// return if cancel was triggered
					select {
					case <-cancel:
						return nil
					default:
					}

					ok = false
					break
				}
			}

			// if all of the requirements are satisfied, return
			if ok {
				return nil
			}
		}

		// otherwise, wait for the number of states to change
		select {
		case <-ev:
		case <-cancel:
			return nil
		}
		close(done)
		done = make(chan struct{})
	}
}
936477
func (s *Finding) SetService(v *Service) *Finding {
	s.Service = v
	return s
}
299283
func (ctx *ServiceContext) Service(service interface{}) error {
	element := reflect.ValueOf(service).Elem()
	if running, ok := ctx.services[element.Type()]; ok {
		element.Set(reflect.ValueOf(running))
		return nil
	}
	return ErrServiceUnknown
}
571772
func (c *Client) TaskService() tasks.TasksClient {
	if c.taskService != nil {
		return c.taskService
	}
	c.connMu.Lock()
	defer c.connMu.Unlock()
	return tasks.NewTasksClient(c.conn)
}
477845
func (_m *ClientInterface) WaitService(serviceIDs []string, state service.DesiredState, timeout time.Duration, recursive bool) error {
	ret := _m.Called(serviceIDs, state, timeout, recursive)

	var r0 error
	if rf, ok := ret.Get(0).(func([]string, service.DesiredState, time.Duration, bool) error); ok {
		r0 = rf(serviceIDs, state, timeout, recursive)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}
341210
func (f *Facade) WaitSingleService(svc *service.Service, dstate service.DesiredState, cancel <-chan interface{}) error {
	// error out if the desired state is invalid
	if dstate.String() == "unknown" {
		return fmt.Errorf("desired state unknown")
	}

	return f.zzk.WaitService(svc, dstate, cancel)

}
540933
func (l *ServiceListener) Pause(reqs []StateRequest) (int, bool) {
	delta := 0
	ok := true

	for _, req := range reqs {

		logger := plog.WithFields(log.Fields{
			"hostid":     req.HostID,
			"serviceid":  req.ServiceID,
			"instanceid": req.InstanceID,
		})

		// ONLY pause services with a desired state of run
		if err := UpdateState(l.conn, req, func(s *State) bool {
			if s.DesiredState == service.SVCRun {
				s.DesiredState = service.SVCPause
				return true
			}
			return false
		}); err != nil {

			logger.WithError(err).Warn("Could not pause service state")
			ok = false
			continue
		}
		delta++
	}

	return delta, ok
}
393978
func StopService() error {
	svc, err := discoverService(ServiceName)
	if err != nil {
		return errors.Trace(err)
	}
	return svc.Stop()
}
245674
func (_m *API) StartService(_a0 api.SchedulerConfig) (int, error) {
	ret := _m.Called(_a0)

	var r0 int
	if rf, ok := ret.Get(0).(func(api.SchedulerConfig) int); ok {
		r0 = rf(_a0)
	} else {
		r0 = ret.Get(0).(int)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(api.SchedulerConfig) error); ok {
		r1 = rf(_a0)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
686179
func (c *AvailableConditionController) addService(obj interface{}) {
	for _, apiService := range c.getAPIServicesFor(obj.(*v1.Service)) {
		c.enqueue(apiService)
	}
}
1039685
func (l *State) RemoveService(id string) error {
	l.Lock()
	defer l.Unlock()
	return l.removeServiceLocked(id)
}