func (c Creator) creatorWork(body []byte) error { apiJob, err := api.Unmarshal(body) if err != nil { return fmt.Errorf("Umarshalling error with %s: %v", body, err) } repoInfo := github.RepoInfo{ ID: apiJob.RepoInfo.ID, Name: apiJob.RepoInfo.Name, Count: apiJob.RepoInfo.Count, CreationDate: apiJob.RepoInfo.CreationDate, WorkedOn: true, } // Create the repository on the store, claim the work key, err := c.db.AddRepo(repoInfo) if err != nil { return err } timestamps, err := GetAllTimestamps(c.jobQueue, 100, apiJob.Token, repoInfo) if err != nil { return fmt.Errorf("Error with %s: %v", body, err) } lastStar := timestamps[len(timestamps)-1] repoInfo.Timestamps = timestamps repoInfo.WorkedOn = false repoInfo.LastUpdate = time.Now().Format(time.RFC3339) repoInfo.LastStarDate = time.Unix(lastStar, 0).Format(time.RFC3339) err = c.db.PutRepo(repoInfo, key) if err != nil { return fmt.Errorf("Put to store error with %s: %v", body, err) } // TODO: Think if this could be done on the fly first // TODO: lib.CanvasJS(timestamps, repoInfo, buffer) // Then send the buffer to the database // TODO: wrap that into the dbaccess file service.Objects.Insert(*bucketName, object).Media(file).Do() // https://cloud.google.com/storage/docs/json_api/v1/json-api-go-samples return nil }
// GetRepo will fetch the data about the repo from the database. // If the repo exist, return the RepoInfo populated // If the repo doesn't exist in the database, return Repo with Exist = false // Return an error only if something unexepected happened. func (db Datastore) GetRepo(repo string) (github.RepoInfo, *datastore.Key, error) { var repoInfo github.RepoInfo key := new(datastore.Key) q := datastore.NewQuery(kind).Ancestor(repoInfoKey(db.Context)).Filter("Name =", repo) for t := q.Run(db.Context); ; { var info github.RepoInfo var err error key, err = t.Next(&info) if err == datastore.Done { break } if err != nil { return repoInfo, nil, err } repoInfo = info repoInfo.SetExist(true) } return repoInfo, key, nil }
// ClaimWork set the WorkedOn flag of the repoInfo to true and persist it. func (db Datastore) ClaimWork(repoInfo github.RepoInfo, key *datastore.Key) (*datastore.Key, error) { info, _, err := db.GetRepo(repoInfo.Name) if err != nil { return nil, err } if info.WorkedOn { return nil, store.ErrAlreadyWorkedOn } repoInfo.WorkedOn = true return datastore.Put(db.Context, key, &repoInfo) }