예제 #1
0
// Transactional function to enqueue a bid, while keeping accounts in balance
func EnqueueBid(c appengine.Context, bid *Bid) (*datastore.Key, error) {
	var bidKey *datastore.Key
	f := func(c appengine.Context) error {
		dao := NewGaeAccountingDao(c, true)

		if err := bid.CheckBalance(dao); err != nil {
			return err
		}

		//parentKey := ArticleKey(c, bid.Article)
		//parentKey := AccountKey(c, bid.Participant)
		if key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Bid", nil), bidCodec{bid}); err != nil {
			return err
		} else {
			bidKey = key
		}

		if err := bid.Book(dao, bidKey.Encode()); err != nil {
			return err
		}

		if err := addRetireBidTask(c, bidKey.Encode(), bid); err != nil {
			return err
		}

		// Encode the new bid as a hotBid and put it into a pull queue
		hot := newHotBid(bidKey, bid)
		if bytes, err := json.Marshal(*hot); err != nil {
			return err
		} else {
			var task taskqueue.Task
			task.Method = "PULL"
			task.Payload = bytes
			task.Tag = string(bid.Article)
			taskqueue.Add(c, &task, "hotbids")
		}

		return dao.Flush()
	}

	if err := datastore.RunInTransaction(c, f, &datastore.TransactionOptions{XG: true}); err != nil {
		return nil, err
	}

	return bidKey, nil
}
예제 #2
0
func ProcessEvent(event MeetupEvent, r *http.Request) *taskqueue.Task {
	const layout = "Jan 2, 2006 at 3:04pm (MST)"
	now := time.Now().Unix() * 1000
	opentime := event.Rsvp_rules.Open_time

	var task *taskqueue.Task = nil

	data := url.Values{"event_id": {event.Id}, "agree_to_refund": {"false"}, "rsvp": {"yes"}}

	if opentime <= now {
		PostRSVP(data, r)
		t := time.Unix(event.Time/1000, 0)
		log.Printf("RSVPing for %v on %s\n", event.Name, t.Format(layout))
	} else if opentime > now {
		task = taskqueue.NewPOSTTask("/rsvpeventworker", data)
		task.ETA = time.Unix(opentime/1000, 0)
	}

	return task

}