Example #1
0
func (s *ApiServer) RefetchFriendFeed() error {
	prefix := store.TableProfile
	j := 0
	n, err := store.ForwardTableScan(s.mdb, prefix, func(i int, k, v []byte) error {
		profile := &pb.Profile{}
		if err := proto.Unmarshal(v, profile); err != nil {
			return err
		}

		if profile.RemoteKey != "" {
			return nil
		}

		oldjob, err := store.GetArchiveHistory(s.mdb, profile.Id)
		if err != nil {
			return err
		}

		if oldjob.Id == "" || oldjob.RemoteKey == "" {
			log.Println("Refetch Friendfeed: unknown remote key")
			return nil
		}

		job := &pb.FeedJob{
			Uuid:        profile.Uuid,
			Id:          oldjob.Id,
			RemoteKey:   oldjob.RemoteKey,
			TargetId:    profile.Id,
			Start:       0,
			PageSize:    100,
			MaxLimit:    99,
			ForceUpdate: true,
			Created:     time.Now().Unix(),
			Updated:     time.Now().Unix(),
		}

		log.Println(job)
		_, err = s.EnqueJob(context.Background(), job)
		j++
		return err
	})
	if err != nil {
		log.Println("Error on scanning user profiles:", err)
	}
	log.Printf("Jobs pulled: %d scanned, %d friendfeed feeds scheduled.", n, j)
	return err
}
Example #2
0
func TestFinishJobQueue(t *testing.T) {
	setup()
	defer teardown()

	Convey("Given ApiServer, enqueue job, deque job, finish job", t, func() {
		ctx := context.Background()
		key := store.NewFlakeKey(store.TableJobFeed, srv.mdb.NextId())
		job.Key = key.String()
		job.TargetId = "targetId"

		srv.EnqueJob(ctx, job)

		worker := &pb.Worker{
			Id: "123456",
		}

		// running job
		newjob, err := srv.GetFeedJob(ctx, worker)
		So(err, ShouldBeNil)
		So(newjob.Id, ShouldEqual, job.Id)
		So(newjob.RemoteKey, ShouldEqual, job.RemoteKey)
		So(newjob.Key, ShouldNotEqual, job.Key)

		// finished job
		key1 := job.Key
		newjob.Id = "name"
		newjob.Uuid = "c6f8dca854f011ddb489003048343a40"
		finjob, err := srv.FinishJob(ctx, newjob)
		So(err, ShouldBeNil)
		So(finjob.Key, ShouldNotEqual, key1)
		So(finjob.Status, ShouldEqual, "done")

		dbjob, err := store.GetArchiveHistory(srv.mdb, newjob.TargetId)
		So(err, ShouldBeNil)
		So(finjob.Key, ShouldEqual, dbjob.Key)
		So(dbjob.Status, ShouldEqual, "done")

		// check running job states
		jobs, err := srv.ListJobQueue(store.TableJobRunning)
		So(err, ShouldBeNil)
		So(len(jobs), ShouldEqual, 0)
	})
}