Exemple #1
0
func RunLastRevisionNotFoundTrigger(proj *model.ProjectRef, v *version.Version) error {
	ctx := triggerContext{
		projectRef: proj,
		version:    v,
	}
	trigger := LastRevisionNotFound{}
	// only one trigger to act on for now
	shouldExec, err := trigger.ShouldExecute(ctx)
	if err != nil {
		return err
	}
	if !shouldExec {
		return nil
	}
	err = alert.EnqueueAlertRequest(&alert.AlertRequest{
		Id:        bson.NewObjectId(),
		Trigger:   trigger.Id(),
		VersionId: v.Id,
		CreatedAt: time.Now(),
	})

	if err != nil {
		return err
	}
	return storeTriggerBookkeeping(ctx, []Trigger{trigger})
}
// RunTaskTriggers queues alerts for any active triggers on the tasks's state change.
func RunTaskFailureTriggers(task *model.Task) error {
	ctx, err := getTaskTriggerContext(task)
	if err != nil {
		return err
	}
	activeTriggers, err := getActiveTaskFailureTriggers(*ctx)
	if err != nil {
		return err
	}
	for _, trigger := range activeTriggers {
		req := &alert.AlertRequest{
			Id:        bson.NewObjectId(),
			Trigger:   trigger.Id(),
			TaskId:    task.Id,
			Execution: task.Execution,
			BuildId:   task.BuildId,
			VersionId: task.Version,
			ProjectId: task.Project,
			PatchId:   "",
			CreatedAt: time.Now(),
		}
		err := alert.EnqueueAlertRequest(req)
		if err != nil {
			return err
		}
		err = storeTriggerBookkeeping(*ctx, []Trigger{trigger})
		if err != nil {
			return err
		}
	}
	return nil
}
Exemple #3
0
func RunSpawnWarningTriggers(host *host.Host) error {
	ctx := triggerContext{host: host}
	for _, trigger := range SpawnWarningTriggers {
		shouldExec, err := trigger.ShouldExecute(ctx)
		if err != nil {
			return err
		}
		if shouldExec {
			err := alert.EnqueueAlertRequest(&alert.AlertRequest{
				Id:        bson.NewObjectId(),
				Trigger:   trigger.Id(),
				HostId:    host.Id,
				CreatedAt: time.Now(),
			})
			if err != nil {
				return err
			}
			err = storeTriggerBookkeeping(ctx, []Trigger{trigger})
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Exemple #4
0
func TestAlertQueue(t *testing.T) {
	Convey("After queueing a few alerts", t, func() {
		db.Clear(alert.Collection)
		now := time.Now()

		// a bunch of alerts, in order of oldest to newest
		testAlerts := []*alert.AlertRequest{
			{Id: bson.NewObjectId(), Display: "test1", CreatedAt: now.Add(time.Millisecond)},
			{Id: bson.NewObjectId(), Display: "test2", CreatedAt: now.Add(2 * time.Millisecond)},
			{Id: bson.NewObjectId(), Display: "test3", CreatedAt: now.Add(3 * time.Millisecond)},
		}

		for _, a := range testAlerts {
			err := alert.EnqueueAlertRequest(a)
			So(err, ShouldBeNil)
		}

		Convey("dequeuing should return them in order, oldest first", func() {
			found := 0
			for {
				nextAlert, err := alert.DequeueAlertRequest()
				So(err, ShouldBeNil)
				if nextAlert == nil {
					break
				}
				So(nextAlert.Display, ShouldEqual, testAlerts[found].Display)
				So(nextAlert.QueueStatus, ShouldEqual, alert.InProgress)
				found++
			}
			So(found, ShouldEqual, len(testAlerts))
		})
	})
}
Exemple #5
0
func RunHostProvisionFailTriggers(h *host.Host) error {
	ctx := triggerContext{host: h}
	trigger := &ProvisionFailed{}
	// only one provision failure trigger to act on for now
	shouldExec, err := trigger.ShouldExecute(ctx)
	if err != nil {
		return err
	}
	if !shouldExec {
		return nil
	}

	err = alert.EnqueueAlertRequest(&alert.AlertRequest{
		Id:        bson.NewObjectId(),
		Trigger:   trigger.Id(),
		HostId:    h.Id,
		CreatedAt: time.Now(),
	})
	if err != nil {
		return err
	}
	return storeTriggerBookkeeping(ctx, []Trigger{trigger})
}