Example #1
0
File: task.go Project: IRCody/snap
func (s *Server) addTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	task, err := core.CreateTaskFromContent(r.Body, nil, s.mt.CreateTask)
	if err != nil {
		respond(500, rbody.FromError(err), w)
		return
	}
	taskB := rbody.AddSchedulerTaskFromTask(task)
	taskB.Href = taskURI(r.Host, task)
	respond(201, taskB, w)
}
Example #2
0
// Implemented as a separate function so that defer calls
// are properly handled and cleanup done properly
// (like for the removal of temporary Yaml to JSON conversion)
func autoDiscoverTasks(taskFiles []os.FileInfo, fullPath string,
	fp func(sch schedule.Schedule,
		wfMap *wmap.WorkflowMap,
		startOnCreate bool,
		opts ...core.TaskOption) (core.Task, core.TaskErrors)) {
	// Note that the list of files is sorted by name due to ioutil.ReadDir
	// default behaviour. See go doc ioutil.ReadDir
	for _, file := range taskFiles {
		f, err := os.Open(path.Join(fullPath, file.Name()))
		if err != nil {
			log.WithFields(log.Fields{
				"_block":           "autoDiscoverTasks",
				"_module":          "scheduler",
				"autodiscoverpath": fullPath,
				"task":             file.Name(),
			}).Error("Opening file ", err)
			continue
		}
		defer f.Close()
		if !strings.HasSuffix(file.Name(), ".json") {
			fc, err := ioutil.ReadAll(f)
			if err != nil {
				log.WithFields(
					log.Fields{
						"_block":           "autoDiscoverTasks",
						"_module":          "scheduler",
						"autodiscoverpath": fullPath,
						"task":             file.Name(),
					}).Error("Reading Yaml file ", err)
				continue
			}
			js, err := yaml.YAMLToJSON(fc)
			if err != nil {
				log.WithFields(
					log.Fields{
						"_block":           "autoDiscoverTasks",
						"_module":          "scheduler",
						"autodiscoverpath": fullPath,
						"task":             file.Name(),
					}).Error("Parsing Yaml file ", err)
				continue
			}
			tfile, err := ioutil.TempFile(os.TempDir(), "yaml2json")
			if err != nil {
				log.WithFields(
					log.Fields{
						"_block":           "autoDiscoverTasks",
						"_module":          "scheduler",
						"autodiscoverpath": fullPath,
						"task":             file.Name(),
					}).Error("Creating temporary file ", err)
				continue
			}
			defer os.Remove(tfile.Name())
			err = ioutil.WriteFile(tfile.Name(), js, 0644)
			if err != nil {
				log.WithFields(
					log.Fields{
						"_block":           "autoDiscoverTasks",
						"_module":          "scheduler",
						"autodiscoverpath": fullPath,
						"task":             file.Name(),
					}).Error("Writing JSON file from Yaml ", err)
				continue
			}
			f, err = os.Open(tfile.Name())
			if err != nil {
				log.WithFields(log.Fields{
					"_block":           "autoDiscoverTasks",
					"_module":          "scheduler",
					"autodiscoverpath": fullPath,
					"task":             file.Name(),
				}).Error("Opening temporary file ", err)
				continue
			}
			defer f.Close()
		}
		mode := true
		task, err := core.CreateTaskFromContent(f, &mode, fp)
		if err != nil {
			log.WithFields(log.Fields{
				"_block":           "autoDiscoverTasks",
				"_module":          "scheduler",
				"autodiscoverpath": fullPath,
				"task":             file.Name(),
			}).Error(err)
			continue
		}
		//TODO: see if the following is really mandatory
		//in which case mgmt/rest/rbody/task.go contents might also
		//move into pkg/task
		//rbody.AddSchedulerTaskFromTask(task)
		log.WithFields(log.Fields{
			"_block":           "autoDiscoverTasks",
			"_module":          "scheduler",
			"autodiscoverpath": fullPath,
			"task-file-name":   file.Name(),
			"task-ID":          task.ID(),
		}).Info("Loading task")
	}
}