Example #1
0
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()
		}
	}
}
Example #2
0
func copyOp(args []string) error {
	if len(args) != 2 {
		return errors.New("Invalid copy operation format - two args expected.")
	}
	src := pathutils.FindLastFile(args[0])
	fmt.Printf("Copying '%s' to '%s'...\n", src, args[1])
	return copyFile(src, args[1])
}
Example #3
0
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)
}
Example #4
0
func startServices() {
	for _, service := range config.Services {
		runService := func() {
			defer done.Done()
			done.Add(1)

			sc := new(run.ServiceConfig)
			sc.Path = pathutils.FindLastFile(service.Path)
			sc.Args = service.Args
			sc.Logger = logger
			sc.MaxCrashCount = service.MaxCrashCount
			sc.RestartDelaySecs = service.RestartDelaySecs
			sc.StartupDelaySecs = service.StartupDelaySecs

			if err := run.RunService(sc, terminate); err != nil {
				logger.Printf("ERROR: Service '%s' reported: %v", sc.Path, err)
			}
		}
		go runService()
	}
}
Example #5
0
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()
}