Esempio n. 1
0
File: init.go Progetto: huntaub/list
func StartApp() {
	revel.INFO.Printf("Beginning Initialization Process...")

	// Load Database Connections
	revel.INFO.Printf("Connecting to Database...")
	session, err := mgo.Dial("mongodb://*****:*****@oceanic.mongohq.com:10000/list")
	if err != nil {
		panic(err)
	}

	collection = session.DB("list").C("classes")
	users = session.DB("list").C("users")
	rand.Seed(time.Now().UnixNano())

	// Start Parsing Lou's List
	revel.INFO.Printf("Launching Parser...")
	jobs.Now(Parser{})
	jobs.Every(1*time.Hour, Parser{})

	// Regex to Recognize Classes
	revel.INFO.Printf("Compiling Regular Expressions...")
	classRegex = regexp.MustCompile(`([A-z]{1,4})\s?(\d{4})\s?(?::{((?:,?\s?\d{1,3})+)})?`)
	sectionRegex = regexp.MustCompile(`\d{1,3}`)

	/*	revel.INFO.Printf("Adding Template Functions...")
		CreateTemplateFunctions()*/

	// Interceptions
	revel.INFO.Printf("Starting Interceptors...")
	revel.InterceptMethod(App.Init, revel.BEFORE)

	revel.INFO.Printf("Initialization Complete")
}
Esempio n. 2
0
func init() {

	Joblist = make(map[int]*models.Job)
	revel.OnAppStart(func() {
		jobs.Now(PopulateJobs{})
		jobs.Schedule("@every 24h", PopulateJobs{})
	})

}
Esempio n. 3
0
func (c Users) Show(login string) revel.Result {
	users, _ := c.Txn.Select(models.User{},
		"select * from users where lower(login) = lower($1)",
		login)
	if len(users) == 0 {
		if token, found := revel.Config.String("api_token"); !found {
			revel.ERROR.Printf("no api token in the config")
		} else {
			t := &oauth.Transport{Token: &oauth.Token{AccessToken: token}}
			client := github.NewClient(t.Client())

			user, _, err := client.Users.Get(login)
			if err != nil {
				revel.ERROR.Printf("err getting user %s\n", err.Error())
				c.Flash.Error("Could not find user, are you sure the login name is correct?")
				return c.Redirect(routes.Users.Index())
			}
			if user == nil {
				revel.ERROR.Printf("user does not exist %s\n", login)
				c.Flash.Error("Could not find user, are you sure the login name is correct?")
				return c.Redirect(routes.Users.Index())
			}
			name := ""
			email := ""
			if user.Name != nil {
				name = *(user.Name)
			}
			if user.Email != nil {
				email = *(user.Email)
			}
			newUser := models.User{
				Id:        *(user.ID),
				Name:      name,
				Login:     *(user.Login),
				AvatarUrl: *(user.AvatarURL),
				Email:     email,
				Followers: *(user.Followers),
				Following: *(user.Following),
				CreatedAt: (*(user.CreatedAt)).Unix()}
			c.Txn.Insert(&newUser)
			c.Flash.Error("User not found. User has been added to queue to process. Come back shortly!")
			jobs.Now(background.AddUser{User: newUser})
		}
		return c.Redirect(routes.Users.Show(login))
	} else {
		user := users[0]
		repos, e := c.Txn.Select(models.Repo{},
			"select * from repos where owner = $1", user.(*models.User).Login)
		if e != nil {
			revel.INFO.Printf(e.Error())
		}
		repoCount := len(repos)
		working := user.(*models.User).LastProcessed == 0
		if repoCount > 0 {
			fileStats, _ := c.Txn.Select(models.FileStat{}, "select language, sum(code + comment + blank) as lines "+
				"from files where repoid in (select id from repos where owner = $1) group by language "+
				"order by lines desc limit 10", user.(*models.User).Login)
			return c.Render(repos, fileStats, login, repoCount, user, working)
		} else {
			return c.Render(repos, login, repoCount, user, working)
		}
	}
}
Esempio n. 4
0
func (pjob ProbeJob) Run() {
	var nodes []models.Node
	var probes []models.Probe

	txn, err := Dbm.Begin()
	if err != nil {
		panic(err)
	}

	t := time.Now()

	_, err = txn.Select(&nodes, "select * from node order by id")
	if err != nil {
		revel.ERROR.Printf("runchecks: %s", err)
		return
	}

	for _, n := range nodes {
		revel.TRACE.Printf("node %d", n.Id)
		probes = nil
		_, err := txn.Select(&probes, "select * from probe where nid=$1 order by id", n.Id)
		if err != nil {
			revel.ERROR.Printf("runchecks: %s", err)
			continue
		}

		for _, p := range probes {
			/* make sure we run the test at the right frequency */
			last := p.LastRun
			if t.Sub(last).Seconds() < p.Frequency {
				revel.INFO.Printf("skipping probe %d - timeout not hit", p.Id)
				/* not enough time elapsed - skip */
				continue
			}

			/* get the script for this probe */
			script, err := txn.Get(&models.Script{}, p.ScriptId)
			if err != nil {
				/* if we can't get the script, the probe can't be run */
				revel.ERROR.Printf("can't run probe: missing script %d", p.ScriptId)
				continue
			}

			if script == nil {
				revel.ERROR.Printf("can't run probe: missing script %d", p.ScriptId)
				continue
			}

			/* set up and run the probe via revel jobs api.
			 * the result comes back through the Error channel. */

			scr := script.(*models.Script)

			revel.TRACE.Printf("node %d probe %d script (%d) %q args %s", n.Id, p.Id, scr.Id, scr.Code, p.Arguments)
			prunner := NewProbeRunner(&p, scr)

			jobs.Now(prunner)

			res := &models.Result{
				NodeId:  n.Id,
				ProbeId: p.Id,
			}

			err = <-prunner.Error
			if err != nil {
				res.Passed = false
				res.StatusMsg = err.Error()
			} else {
				res.Passed = true
				res.StatusMsg = ""
			}
			res.Timestamp = time.Now()
			err = txn.Insert(res)
			if err != nil {
				revel.ERROR.Printf("runchecks: %s", err)
				continue
			}
			p.LastResultId = res.Id
			_, err = txn.Update(&p)
			if err != nil {
				revel.ERROR.Printf("runchecks: %s", err)
				continue
			}

			revel.TRACE.Printf("probe passed? %d %t %q", p.Id, res.Passed, res.StatusMsg)

			// if the probe failed, send the result to the callback url in the node.
			if res.Passed == false {
				revel.WARN.Printf("probe failed, running callback")
				go pjob.Failed(&n, &p, res)
			}
		}
	}

	if err := txn.Commit(); err != nil && err != sql.ErrTxDone {
		panic(err)
	}
}