• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang mgr.Connect函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中code/google/com/p/winsvc/mgr.Connect函数的典型用法代码示例。如果您正苦于以下问题:Golang Connect函数的具体用法?Golang Connect怎么用?Golang Connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Connect函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: controlService

func controlService(name string, c svc.Cmd, to svc.State) error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err != nil {
		return fmt.Errorf("could not access service: %v", err)
	}
	defer s.Close()
	status, err := s.Control(c)
	if err != nil {
		return fmt.Errorf("could not send control=%d: %v", c, err)
	}
	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 = s.Query()
		if err != nil {
			return fmt.Errorf("could not retrieve service status: %v", err)
		}
	}
	return nil
}
开发者ID:ComeOn-Wuhan,项目名称:lantern,代码行数:28,代码来源:manage.go


示例2: Install

func (ws *windowsService) Install() error {
	exepath, err := osext.Executable()
	if err != nil {
		return err
	}
	// Used if path contains a space.
	exepath = `"` + exepath + `"`
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(ws.name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", ws.name)
	}
	s, err = m.CreateService(ws.name, exepath, mgr.Config{
		DisplayName: ws.displayName,
		Description: ws.description,
		StartType:   mgr.StartAutomatic,
	})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(ws.name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
	}
	return nil
}
开发者ID:sneha29shukla,项目名称:mig,代码行数:33,代码来源:service_windows.go


示例3: RemoveService

func (this *Service) RemoveService() error {
	l, err := eventlog.Open(this.name)
	if err != nil {
		return fmt.Errorf("Cannot open eventlog")
	}

	m, err := mgr.Connect()
	if err != nil {
		l.Error(3, fmt.Sprintf("remove service: cannot connect to windows service manager.", err))
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(this.name)
	if err != nil {
		l.Error(3, fmt.Sprintf("remove service: service %s doesn't exists", this.name))
		return fmt.Errorf("service %s doesn't exists", this.name)
	}

	err = s.Delete()
	if err != nil {
		return err
	}

	l.Info(1, "Service Removed. removing event log")
	l.Close()

	eventlog.Remove(this.name)
	return nil
}
开发者ID:homingway,项目名称:hickwall,代码行数:30,代码来源:servicelib_win.go


示例4: installService

func installService(name, desc string) error {
	exepath, err := exePath()
	if err != nil {
		return err
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", name)
	}
	s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc, StartType: mgr.StartAutomatic})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("SetupEventLogSource() failed: %s", err)
	}
	return nil
}
开发者ID:IMQS,项目名称:sente,代码行数:27,代码来源:install.go


示例5: ConfigServiceAutoStart

func (this *Service) ConfigServiceAutoStart() error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	// log.Debug("Connected mgr\r\n")

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

	c, err := s.Config()
	if err != nil {
		return fmt.Errorf("could not get service config: %v", err)
	}

	// make service automatically start
	c.StartType = mgr.StartAutomatic
	err = s.UpdateConfig(c)
	if err != nil {
		return fmt.Errorf("could not update service config: %v", err)
	}
	return nil
}
开发者ID:homingway,项目名称:hickwall,代码行数:27,代码来源:servicelib_win.go


示例6: Start

func (ws *windowsService) Start() error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(ws.Name)
	if err != nil {
		return err
	}
	defer s.Close()
	return s.Start([]string{})
}
开发者ID:omusico,项目名称:gitlab-ci-multi-runner,代码行数:14,代码来源:service_windows.go


示例7: Install

func (ws *windowsService) Install() error {
	exepath, err := ws.execPath()
	if err != nil {
		return err
	}

	binPath := &bytes.Buffer{}
	// Quote exe path in case it contains a string.
	binPath.WriteRune('"')
	binPath.WriteString(exepath)
	binPath.WriteRune('"')

	// Arguments are encoded with the binary path to service.
	// Enclose arguments in quotes. Escape quotes with a backslash.
	for _, arg := range ws.Arguments {
		binPath.WriteRune(' ')
		binPath.WriteString(`"`)
		binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
		binPath.WriteString(`"`)
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(ws.Name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", ws.Name)
	}
	s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
		DisplayName:      ws.DisplayName,
		Description:      ws.Description,
		StartType:        mgr.StartAutomatic,
		ServiceStartName: ws.UserName,
		Password:         ws.Option.string("Password", ""),
		Dependencies:     ws.Dependencies,
	})
	if err != nil {
		return err
	}
	defer s.Close()
	err = eventlog.InstallAsEventCreate(ws.Name, eventlog.Error|eventlog.Warning|eventlog.Info)
	if err != nil {
		s.Delete()
		return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
	}
	return nil
}
开发者ID:omusico,项目名称:gitlab-ci-multi-runner,代码行数:49,代码来源:service_windows.go


示例8: Stop

func (ws *windowsService) Stop() error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(ws.Name)
	if err != nil {
		return err
	}
	defer s.Close()
	_, err = s.Control(svc.Stop)
	return err
}
开发者ID:omusico,项目名称:gitlab-ci-multi-runner,代码行数:15,代码来源:service_windows.go


示例9: TestOpenLanManServer

func TestOpenLanManServer(t *testing.T) {
	m, err := mgr.Connect()
	if err != nil {
		t.Fatalf("SCM connection failed: %s", err)
	}
	defer m.Disconnect()
	s, err := m.OpenService("lanmanserver")
	if err != nil {
		t.Fatalf("OpenService(lanmanserver) failed: %s", err)
	}
	_, err = s.Config()
	if err != nil {
		t.Fatalf("Config failed: %s", err)
	}
	defer s.Close()
}
开发者ID:jeppeter,项目名称:go-winsvc,代码行数:16,代码来源:mgr_test.go


示例10: InstallService

func (this *Service) InstallService() error {
	// open logger
	elog, err := eventlog.Open(this.name)
	if err != nil {
		// install logger
		err = eventlog.InstallAsEventCreate(this.name, eventlog.Error|eventlog.Warning|eventlog.Info)
		if err != nil {
			return fmt.Errorf("SetupEventLogSource() failed: %s", err)
		}

		// open
		elog, err = eventlog.Open(this.name)
		if err != nil {
			return fmt.Errorf("Cannot open eventlog")
		}

	}
	defer elog.Close()

	m, err := mgr.Connect()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot connect to windows service manager.", err))
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(this.name)
	if err == nil {
		s.Close()
		elog.Error(3, fmt.Sprintf("install service: service %s already exists", this.name))
		return fmt.Errorf("service %s already exists", this.name)
	}

	s, err = m.CreateService(this.name, this.path, mgr.Config{DisplayName: this.desc})
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: Failed to create service: %v", err))
		return err
	}
	defer s.Close()

	err = this.ConfigServiceAutoStart()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot set service to auto start: %v", err))
		return err
	}
	return nil
}
开发者ID:homingway,项目名称:hickwall,代码行数:47,代码来源:servicelib_win.go


示例11: startService

func startService(name string) error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err != nil {
		return fmt.Errorf("could not access service: %v", err)
	}
	defer s.Close()
	err = s.Start([]string{"p1", "p2", "p3"})
	if err != nil {
		return fmt.Errorf("could not start service: %v", err)
	}
	return nil
}
开发者ID:ComeOn-Wuhan,项目名称:lantern,代码行数:17,代码来源:manage.go


示例12: TestMyService

func TestMyService(t *testing.T) {
	const name = "myservice"

	m, err := mgr.Connect()
	if err != nil {
		t.Fatalf("SCM connection failed: %s", err)
	}
	defer m.Disconnect()

	c := mgr.Config{
		StartType:    mgr.StartDisabled,
		DisplayName:  "my service",
		Description:  "my service is just a test",
		Dependencies: []string{"LanmanServer", "W32Time"},
	}

	exename := os.Args[0]
	exepath, err := filepath.Abs(exename)
	if err != nil {
		t.Fatalf("filepath.Abs(%s) failed: %s", exename, err)
	}

	install(t, m, name, exepath, c)

	s, err := m.OpenService(name)
	if err != nil {
		t.Fatalf("service %s is not installed", name)
	}
	defer s.Close()

	c.BinaryPathName = exepath
	c = testConfig(t, s, c)

	c.StartType = mgr.StartManual
	err = s.UpdateConfig(c)
	if err != nil {
		t.Fatalf("UpdateConfig failed: %v", err)
	}

	testConfig(t, s, c)

	remove(t, s)
}
开发者ID:ComeOn-Wuhan,项目名称:lantern,代码行数:43,代码来源:mgr_test.go


示例13: removeService

func removeService(name string) error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err != nil {
		return fmt.Errorf("service %s is not installed", name)
	}
	defer s.Close()
	err = s.Delete()
	if err != nil {
		return err
	}
	err = eventlog.Remove(name)
	if err != nil {
		return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
	}
	return nil
}
开发者ID:IMQS,项目名称:sente,代码行数:21,代码来源:install.go


示例14: Stop

func Stop() error {
	ctrl, err := mgr.Connect()
	if err != nil {
		ctrl.Disconnect()
		return err
	}

	defer ctrl.Disconnect()

	service, err := ctrl.OpenService(ServiceName)
	if err != nil {
		return fmt.Errorf("error creating handle to %s: %v", ServiceName, err)
	}
	defer service.Close()

	if _, err := service.Control(svc.Stop); err != nil {
		return fmt.Errorf("error stopping service %s: %v", ServiceName, err)
	}

	return nil
}
开发者ID:postfix,项目名称:ossecsvc,代码行数:21,代码来源:services_windows.go


示例15: Uninstall

func (ws *windowsService) Uninstall() error {
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(ws.Name)
	if err != nil {
		return fmt.Errorf("service %s is not installed", ws.Name)
	}
	defer s.Close()
	err = s.Delete()
	if err != nil {
		return err
	}
	err = eventlog.Remove(ws.Name)
	if err != nil {
		return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
	}
	return nil
}
开发者ID:omusico,项目名称:gitlab-ci-multi-runner,代码行数:21,代码来源:service_windows.go


示例16: queryService

func queryService(name string, fn func(s *mgr.Service, l *eventlog.Log) (State, error)) (State, error) {
	l, err := eventlog.Open(name)
	if err != nil {
		return State(Unknown), fmt.Errorf("Cannot open eventlog")
	}
	defer l.Close()

	m, err := mgr.Connect()
	if err != nil {
		l.Error(3, fmt.Sprintf("cannot connect to mgr: %v", err))
		return State(Unknown), err
	}
	defer m.Disconnect()

	s, err := m.OpenService(name)
	if err != nil {
		l.Error(3, fmt.Sprintf("cannot open service: %v", err))
		return State(Unknown), fmt.Errorf("service %s is not installed", name)
	}
	defer s.Close()

	return fn(s, l)
}
开发者ID:homingway,项目名称:hickwall,代码行数:23,代码来源:servicelib_win.go


示例17: TestExample

func TestExample(t *testing.T) {
	const name = "myservice"

	m, err := mgr.Connect()
	if err != nil {
		t.Fatalf("SCM connection failed: %s", err)
	}
	defer m.Disconnect()

	dir, err := ioutil.TempDir("", "svc")
	if err != nil {
		t.Fatalf("failed to create temp directory: %v", err)
	}
	defer os.RemoveAll(dir)

	exepath := filepath.Join(dir, "a.exe")
	o, err := exec.Command("go", "build", "-o", exepath, "code.google.com/p/winsvc/example").CombinedOutput()
	if err != nil {
		t.Fatalf("failed to build service program: %v\n%v", err, string(o))
	}

	s, err := m.OpenService(name)
	if err == nil {
		err = s.Delete()
		if err != nil {
			s.Close()
			t.Fatalf("Delete failed: %s", err)
		}
		s.Close()
	}
	s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: "my service"})
	if err != nil {
		t.Fatalf("CreateService(%s) failed: %v", name, err)
	}
	defer s.Close()

	testState(t, s, svc.Stopped)
	err = s.Start(nil)
	if err != nil {
		t.Fatalf("Start(%s) failed: %s", s.Name, err)
	}
	waitState(t, s, svc.Running)
	time.Sleep(1 * time.Second)

	// testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
	_, err = s.Control(svc.Interrogate)
	if err != nil {
		t.Fatalf("Control(%s) failed: %s", s.Name, err)
	}
	_, err = s.Control(svc.Interrogate)
	if err != nil {
		t.Fatalf("Control(%s) failed: %s", s.Name, err)
	}
	time.Sleep(1 * time.Second)

	_, err = s.Control(svc.Stop)
	if err != nil {
		t.Fatalf("Control(%s) failed: %s", s.Name, err)
	}
	waitState(t, s, svc.Stopped)

	err = s.Delete()
	if err != nil {
		t.Fatalf("Delete failed: %s", err)
	}
}
开发者ID:ComeOn-Wuhan,项目名称:lantern,代码行数:66,代码来源:svc_test.go



注:本文中的code/google/com/p/winsvc/mgr.Connect函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang wombat.Context类代码示例发布时间:2022-05-24
下一篇:
Golang util.Uint64toBytes函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap