Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
_ "github.com/mdg-iitr/Codephile/conf"
"github.com/astaxie/beego"
_ "github.com/mdg-iitr/Codephile/routers"
"github.com/mdg-iitr/Codephile/services/scheduler"
)

func main() {

//goroutine for the scheduler
go scheduler.StartScheduling()

if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/docs"] = "swagger"
Expand Down
26 changes: 26 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func AddUser(u User) (string, error) {
log.Println(err)
return "", errors.New("Could not create user: Username already exists")
}
err = RefreshSubmissions()
//handle error

return u.ID.Hex(), nil
}

Expand Down Expand Up @@ -471,3 +474,26 @@ func CompareUser(uid1 bson.ObjectId, uid2 string) (Follow.AllWorldRanks, error)
return worldRanksComparison, errors.New("UID Invalid")
}
}

func RefreshSubmissions() error {
users := GetAllUsers()
websites := [4]string{"codechef","codeforces","spoj","hackerrank"}
var ErrorSubmissions error

//establishing a goroutine for this "for" loop
for _ , user := range users {
// executing every iteration in a separate goroutine (to be implemented later)
// go func(user User) {
for _ , website := range websites {
err := AddSubmissions(&user, website)
if err != nil {
//Submissions for this particular user is not updated
ErrorSubmissions = errors.New("Submission fetching altered")
} else {
continue
}
}
// }(user)
}
return ErrorSubmissions
}
25 changes: 25 additions & 0 deletions services/scheduler/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scheduler

import (
"github.com/jasonlvhit/gocron"
"github.com/mdg-iitr/Codephile/models"
// "github.com/mdg-iitr/Codephile/models"

)

func task() {
//Updates profiles and submissions
_ = models.RefreshSubmissions()
//handle error
}

func taskWithParams(a int, b string) {
//
}

func StartScheduling() {
// task()
// gocron.Every(10).Seconds().Do(task)
gocron.Every(1).Day().Do(task) //task to be done everyday
<- gocron.Start()
}