Exemplo n.º 1
0
func (s *Scheduler) handle(msg sarif.Message) {
	var t ScheduleMessage
	if err := msg.DecodePayload(&t); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}

	now := time.Now()
	t.Task.Time = now
	if t.Time != "" {
		t.Task.Time = futureTime(util.ParseTime(t.Time, now))
	}
	if t.RandomAfter != "" && t.RandomBefore != "" {
		after := futureTime(util.ParseTime(t.RandomAfter, t.Task.Time))
		before := futureTime(util.ParseTime(t.RandomBefore, t.Task.Time))
		if before.Before(after) {
			after, before = before, after
		}
		maxDur := int64(before.Sub(after))
		ranDur := time.Duration(rand.Int63n(maxDur))
		t.Task.Time = after.Add(ranDur)
	}
	if t.Duration != "" {
		dur, err := util.ParseDuration(t.Duration)
		if err != nil {
			s.ReplyBadRequest(msg, err)
			return
		}
		t.Task.Time = t.Task.Time.Add(dur)
	}
	if t.Task.Reply.Action == "" {
		text := msg.Text
		if text == "" {
			text = "Reminder from " + time.Now().Format(time.RFC3339) + " finished."
		}
		t.Task.Reply = sarif.Message{
			Action:      "schedule/finished",
			Destination: msg.Source,
			Text:        text,
		}
	}
	if t.Task.Reply.CorrId == "" {
		t.Reply.CorrId = msg.Id
	}
	s.Log("info", "new task:", t)

	if _, err := s.Store.Put(t.Task.Key(), &t.Task); err != nil {
		s.ReplyInternalError(msg, err)
		return
	}

	go s.recalculateTimer()
	s.Reply(msg, sarif.CreateMessage("schedule/created", t.Task))
}
Exemplo n.º 2
0
func ParseValue(v string) interface{} {
	if b, err := strconv.ParseBool(v); err == nil {
		return b
	}
	if f, err := strconv.ParseFloat(v, 64); err == nil {
		return f
	}
	if t := util.ParseTime(v, time.Now()); !t.IsZero() {
		return t
	}
	return v
}
Exemplo n.º 3
0
func (s *Service) handleDate(msg sarif.Message) {
	text := msg.Text
	if text == "" {
		s.ReplyText(msg, printTime(time.Now()))
		return
	}

	if t := util.ParseTime(text, time.Now()); !t.IsZero() {
		s.ReplyText(msg, printTime(t))
		return
	}

	d, err := util.ParseDuration(text)
	if err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	s.ReplyText(msg, printTime(time.Now().Add(d)))
}
Exemplo n.º 4
0
func (s *Service) handleUnix(msg sarif.Message) {
	text := msg.Text
	if text == "" {
		s.ReplyText(msg, strconv.FormatInt(time.Now().Unix(), 10))
		return
	}

	if u, err := strconv.ParseInt(text, 10, 64); err == nil {
		s.ReplyText(msg, printTime(time.Unix(u, 0)))
		return
	}

	if t := util.ParseTime(text, time.Now()); !t.IsZero() {
		s.ReplyText(msg, strconv.FormatInt(t.Unix(), 10))
		return
	}

	d, err := util.ParseDuration(text)
	if err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}
	s.ReplyText(msg, strconv.FormatInt(time.Now().Add(d).Unix(), 10))
}
Exemplo n.º 5
0
func (s *Service) handleFetchFddb(msg sarif.Message) {
	day := util.ParseTime(msg.Text, time.Now())
	if err := s.FetchFddb(day); err != nil {
		s.ReplyInternalError(msg, err)
	}
}