本文整理汇总了Golang中go/skia/org/infra/go/timer.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: update
// update syncs the source code repository and loads any new commits.
func (c *CommitCache) update() (rv error) {
defer timer.New("CommitCache.update()").Stop()
glog.Info("Reloading commits.")
if err := c.repo.Update(true, true); err != nil {
return fmt.Errorf("Failed to update the repo: %v", err)
}
from := time.Time{}
n := c.NumCommits()
if n > 0 {
last, err := c.Get(n - 1)
if err != nil {
return fmt.Errorf("Failed to get last commit: %v", err)
}
from = last.Timestamp
}
newCommitHashes := c.repo.From(from)
glog.Infof("Processing %d new commits.", len(newCommitHashes))
newCommits := make([]*gitinfo.LongCommit, len(newCommitHashes))
if len(newCommitHashes) > 0 {
for i, h := range newCommitHashes {
d, err := c.repo.Details(h)
if err != nil {
return fmt.Errorf("Failed to obtain commit details for %s: %v", h, err)
}
newCommits[i] = d
}
}
branchHeads, err := c.repo.GetBranches()
if err != nil {
return fmt.Errorf("Failed to read branch information from the repo: %v", err)
}
// Load new builds for the BuildCache.
allCommits := append(c.Commits, newCommits...)
buildCacheHashes := make([]string, 0, c.requestSize)
for _, commit := range allCommits[len(allCommits)-c.requestSize:] {
buildCacheHashes = append(buildCacheHashes, commit.Hash)
}
byId, byCommit, builderStatuses, err := build_cache.LoadData(buildCacheHashes)
if err != nil {
return fmt.Errorf("Failed to update BuildCache: %v", err)
}
// Update the cached values all at once at at the end.
glog.Infof("Updating the cache.")
// Write the cache to disk *after* unlocking it.
defer func() {
rv = c.toFile()
}()
defer timer.New(" CommitCache locked").Stop()
c.mutex.Lock()
defer c.mutex.Unlock()
c.BranchHeads = branchHeads
c.Commits = allCommits
c.buildCache.UpdateWithData(byId, byCommit, builderStatuses)
glog.Infof("Finished updating the cache.")
return nil
}
示例2: RemoveChange
// RemoveChange, see ExpectationsStore interface.
func (s *SQLExpectationsStore) RemoveChange(changedDigests map[string][]string) (retErr error) {
defer timer.New("removing exp change").Stop()
const markRemovedStmt = `UPDATE exp_test_change
SET removed = IF(removed IS NULL, ?, removed)
WHERE (name=?) AND (digest=?)`
// start a transaction
tx, err := s.vdb.DB.Begin()
if err != nil {
return err
}
defer func() { retErr = database.CommitOrRollback(tx, retErr) }()
// Mark all the digests as removed.
now := util.TimeStampMs()
for testName, digests := range changedDigests {
for _, digest := range digests {
if _, err = tx.Exec(markRemovedStmt, now, testName, digest); err != nil {
return err
}
}
}
return nil
}
示例3: addBuildCommentHandler
func addBuildCommentHandler(w http.ResponseWriter, r *http.Request) {
defer timer.New("addBuildCommentHandler").Stop()
if !userHasEditRights(r) {
util.ReportError(w, r, fmt.Errorf("User does not have edit rights."), "User does not have edit rights.")
return
}
w.Header().Set("Content-Type", "application/json")
cache, err := getCommitCache(w, r)
if err != nil {
return
}
buildId, err := strconv.ParseInt(mux.Vars(r)["buildId"], 10, 32)
if err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Invalid build id: %v", err))
return
}
comment := struct {
Comment string `json:"comment"`
}{}
if err := json.NewDecoder(r.Body).Decode(&comment); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to add comment: %v", err))
return
}
defer util.Close(r.Body)
c := buildbot.BuildComment{
BuildId: int(buildId),
User: login.LoggedInAs(r),
Timestamp: float64(time.Now().UTC().Unix()),
Message: comment.Comment,
}
if err := cache.AddBuildComment(int(buildId), &c); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to add comment: %v", err))
return
}
}
示例4: deleteBuildCommentHandler
func deleteBuildCommentHandler(w http.ResponseWriter, r *http.Request) {
defer timer.New("deleteBuildCommentHandler").Stop()
if !userHasEditRights(r) {
util.ReportError(w, r, fmt.Errorf("User does not have edit rights."), "User does not have edit rights.")
return
}
w.Header().Set("Content-Type", "application/json")
cache, err := getCommitCache(w, r)
if err != nil {
return
}
buildId, err := strconv.ParseInt(mux.Vars(r)["buildId"], 10, 32)
if err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Invalid build id: %v", err))
return
}
commentId, err := strconv.ParseInt(mux.Vars(r)["commentId"], 10, 32)
if err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Invalid comment id: %v", err))
return
}
if err := cache.DeleteBuildComment(int(buildId), int(commentId)); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to delete comment: %v", err))
return
}
}
示例5: addCommitCommentHandler
func addCommitCommentHandler(w http.ResponseWriter, r *http.Request) {
defer timer.New("addCommitCommentHandler").Stop()
if !userHasEditRights(r) {
util.ReportError(w, r, fmt.Errorf("User does not have edit rights."), "User does not have edit rights.")
return
}
w.Header().Set("Content-Type", "application/json")
commit := mux.Vars(r)["commit"]
comment := struct {
Comment string `json:"comment"`
}{}
if err := json.NewDecoder(r.Body).Decode(&comment); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to add comment: %v", err))
return
}
defer util.Close(r.Body)
c := buildbot.CommitComment{
Commit: commit,
User: login.LoggedInAs(r),
Timestamp: time.Now().UTC(),
Message: comment.Comment,
}
if err := db.PutCommitComment(&c); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to add commit comment: %v", err))
return
}
}
示例6: getCandidatesForBuilder
// getCandidatesForBuilder finds all BuildCandidates for the given builder, in order.
func (q *BuildQueue) getCandidatesForBuilder(bf *buildFinder, recentCommits []string, now time.Time) ([]*BuildCandidate, error) {
defer timer.New(fmt.Sprintf("getCandidatesForBuilder(%s)", bf.Builder)).Stop()
repo, err := q.repos.Repo(bf.Repo)
if err != nil {
return nil, err
}
candidates := []*BuildCandidate{}
for {
score, newBuild, stoleFrom, err := q.getBestCandidate(bf, recentCommits, now)
if err != nil {
return nil, fmt.Errorf("Failed to get build candidates for %s: %v", bf.Builder, err)
}
if score < q.scoreThreshold {
break
}
d, err := repo.Details(newBuild.GotRevision)
if err != nil {
return nil, err
}
// "insert" the new build.
bf.add(newBuild)
if stoleFrom != nil {
bf.add(stoleFrom)
}
candidates = append(candidates, &BuildCandidate{
Author: d.Author,
Builder: newBuild.Builder,
Commit: newBuild.GotRevision,
Score: score,
Repo: bf.Repo,
})
}
return candidates, nil
}
示例7: New
// New creates and returns a new CommitCache which watches the given repo.
// The initial update will load ALL commits from the repository, so expect
// this to be slow.
func New(repo *gitinfo.GitInfo, cacheFile string, requestSize int) (*CommitCache, error) {
defer timer.New("commit_cache.New()").Stop()
c, err := fromFile(cacheFile)
if err != nil {
glog.Warningf("Failed to read commit cache from file; starting from scratch. Error: %v", err)
c = &CommitCache{}
}
c.buildCache = &build_cache.BuildCache{}
c.cacheFile = cacheFile
c.repo = repo
c.requestSize = requestSize
// Update the cache.
if err := c.update(); err != nil {
return nil, err
}
// Update in a loop.
go func() {
for _ = range time.Tick(time.Minute) {
if err := c.update(); err != nil {
glog.Errorf("Failed to update commit cache: %v", err)
}
}
}()
return c, nil
}
示例8: LoadData
// LoadData loads the build data for the given commits.
func LoadData(commits []string) (map[int]*buildbot.Build, map[string]map[string]*buildbot.BuildSummary, map[string][]*buildbot.BuilderComment, error) {
defer timer.New("build_cache.LoadData()").Stop()
builds, err := buildbot.GetBuildsForCommits(commits, nil)
if err != nil {
return nil, nil, nil, err
}
byId := map[int]*buildbot.Build{}
byCommit := map[string]map[string]*buildbot.BuildSummary{}
builders := map[string]bool{}
for hash, buildList := range builds {
byBuilder := map[string]*buildbot.BuildSummary{}
for _, b := range buildList {
byId[b.Id] = b
if !util.AnyMatch(BOT_BLACKLIST, b.Builder) {
byBuilder[b.Builder] = b.GetSummary()
builders[b.Builder] = true
}
}
byCommit[hash] = byBuilder
}
builderList := make([]string, 0, len(builders))
for b, _ := range builders {
builderList = append(builderList, b)
}
builderComments, err := buildbot.GetBuildersComments(builderList)
if err != nil {
return nil, nil, nil, err
}
return byId, byCommit, builderComments, nil
}
示例9: Run
// Run executes the function for the given test and returns an error if it fails.
func (t test) Run() error {
defer timer.New(t.Name).Stop()
err, output := t.run()
if err != nil {
return fmt.Errorf(TEST_FAILURE, t.Name, t.Cmd, err, output)
}
return nil
}
示例10: UpdateWithData
// UpdateWithData replaces the contents of the BuildCache with the given
// data. Not intended to be used by consumers of BuildCache, but exists to
// allow for loading and storing the cache data separately so that the cache
// may be locked for the minimum amount of time.
func (c *BuildCache) UpdateWithData(byId map[int]*buildbot.Build, byCommit map[string]map[string]*buildbot.BuildSummary, builders map[string][]*buildbot.BuilderComment) {
defer timer.New(" BuildCache locked").Stop()
c.mutex.Lock()
defer c.mutex.Unlock()
c.byId = byId
c.byCommit = byCommit
c.builders = builders
}
示例11: toFile
// toFile saves the CommitCache to a file.
func (c *CommitCache) toFile() error {
defer timer.New("commit_cache.toFile()").Stop()
f, err := os.Create(c.cacheFile)
if err != nil {
return err
}
defer util.Close(f)
if err := gob.NewEncoder(f).Encode(c); err != nil {
return err
}
return nil
}
示例12: infraHandler
func infraHandler(w http.ResponseWriter, r *http.Request) {
defer timer.New("infraHandler").Stop()
w.Header().Set("Content-Type", "text/html")
// Don't use cached templates in testing mode.
if *testing {
reloadTemplates()
}
if err := infraTemplate.Execute(w, struct{}{}); err != nil {
util.ReportError(w, r, err, fmt.Sprintf("Failed to expand template: %v", err))
}
}
示例13: hostsHandler
func hostsHandler(w http.ResponseWriter, r *http.Request) {
defer timer.New("hostsHandler").Stop()
w.Header().Set("Content-Type", "text/html")
// Don't use cached templates in testing mode.
if *testing {
reloadTemplates()
}
if err := hostsTemplate.Execute(w, struct{}{}); err != nil {
glog.Errorf("Failed to write or encode output: %s", err)
}
}
示例14: fromFile
// fromFile attempts to load the CommitCache from the given file.
func fromFile(cacheFile string) (*CommitCache, error) {
defer timer.New("commit_cache.fromFile()").Stop()
c := CommitCache{}
if _, err := os.Stat(cacheFile); err != nil {
return nil, fmt.Errorf("Could not stat cache file: %v", err)
}
f, err := os.Open(cacheFile)
if err != nil {
return nil, fmt.Errorf("Failed to open cache file %s: %v", cacheFile, err)
}
defer util.Close(f)
if err := gob.NewDecoder(f).Decode(&c); err != nil {
return nil, fmt.Errorf("Failed to read cache file %s: %v", cacheFile, err)
}
return &c, nil
}
示例15: GetBuildsFromDateRange
// GetBuildsFromDateRange retrieves all builds which finished in the given date range.
func GetBuildsFromDateRange(start, end time.Time) ([]*Build, error) {
defer timer.New("GetBuildsFromDateRange").Stop()
var ids []int
if err := DB.Select(&ids, fmt.Sprintf("SELECT id FROM %s WHERE started > ? and started < ?", TABLE_BUILDS), float64(start.UTC().Unix()), float64(end.UTC().Unix())); err != nil {
return nil, fmt.Errorf("Failed to obtain builds from date range: %v", err)
}
builds, err := GetBuildsFromDB(ids)
if err != nil {
return nil, err
}
rv := make([]*Build, 0, len(builds))
for _, b := range builds {
rv = append(rv, b)
}
return rv, nil
}