示例#1
0
文件: main.go 项目: Joffcom/silver
func execStartupTasks() {
	for _, task := range config.StartupTasks {
		runTask := func() {
			defer done.Done()
			done.Add(1)

			cc := new(run.CommandConfig)
			cc.Path = pathutils.FindLastFile(task.Path)
			cc.Args = task.Args
			cc.Logger = logger
			cc.TimeoutSecs = task.TimeoutSecs
			if task.Async {
				cc.StartupDelaySecs = task.StartupDelaySecs
				cc.StartupRandomDelaySecs = task.StartupRandomDelaySecs
			} else if cc.StartupDelaySecs > 0 || cc.StartupRandomDelaySecs > 0 {
				logger.Printf("WARNING: Only Async startup tasks may have startup delays.")
			}
			if _, err := run.RunCommand(cc, terminate); err != nil {
				logger.Printf("ERROR: Startup task '%s' reported: %v", cc.Path, err)
			}
		}
		if task.Async {
			go runTask()
		} else {
			runTask()
		}
	}
}
示例#2
0
文件: main.go 项目: Joffcom/silver
func execCommand() {
	requestedCmd := ""
	if len(os.Args) > 2 {
		requestedCmd = os.Args[2]
	}
	var cmd *Command
	for _, c := range config.Commands {
		if c.Name == requestedCmd {
			cmd = c
			break
		}
	}

	if cmd == nil {
		fmt.Fprintf(os.Stderr, "ERROR: Unknown command '%s'. ", requestedCmd)
		if len(config.Commands) == 0 {
			fmt.Fprintf(os.Stderr, "There are no commands configured!\n")
		} else {
			fmt.Fprintf(os.Stderr, "Valid commands are:\n")
			for _, cmd = range config.Commands {
				fmt.Fprintf(os.Stderr, "    %s\n", cmd.Name)
			}
		}
		os.Exit(1)
	}

	// Exec the command
	c := new(run.CommandConfig)
	c.Path = pathutils.FindLastFile(cmd.Path)
	if len(os.Args) > 3 {
		c.Args = append(cmd.Args, os.Args[3:]...)
	} else {
		c.Args = cmd.Args
	}
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	c.Logger = nil // No logger as we're in console mode (e.g. end-user)

	exitCode, err := run.RunCommand(c, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	os.Exit(exitCode)
}
示例#3
0
文件: main.go 项目: Joffcom/silver
func setupScheduledTasks() {
	cronManager = cron.New()
	for _, task := range config.ScheduledTasks {
		cc := new(run.CommandConfig)
		cc.Path = pathutils.FindLastFile(task.Path)
		cc.Args = task.Args
		cc.Logger = logger
		cc.TimeoutSecs = task.TimeoutSecs
		cc.StartupDelaySecs = task.StartupDelaySecs
		cc.StartupRandomDelaySecs = task.StartupRandomDelaySecs
		runTask := func() {
			defer done.Done()
			done.Add(1)
			if _, err := run.RunCommand(cc, terminate); err != nil {
				logger.Printf("Error raised by scheduled task '%s': %v", cc.Path, err)
			}
		}
		err := cronManager.AddFunc(task.Schedule, runTask)
		if err != nil {
			logger.Printf("Unable to schedule task '%s': %v", task.Path, err)
		}
	}
	cronManager.Start()
}