func dumpApp(app *models.App, starts map[string]models.PendingStartMessage, stops map[string]models.PendingStopMessage, timeProvider timeprovider.TimeProvider) { fmt.Printf("\n") fmt.Printf("Guid: %s | Version: %s\n", app.AppGuid, app.AppVersion) if app.IsDesired() { fmt.Printf(" Desired: [%d] instances, (%s, %s)\n", app.Desired.NumberOfInstances, app.Desired.State, app.Desired.PackageState) } else { fmt.Printf(" Desired: NO\n") } if len(app.InstanceHeartbeats) == 0 { fmt.Printf(" Heartbeats: NONE\n") } else { fmt.Printf(" Heartbeats:\n") for _, heartbeat := range app.InstanceHeartbeats { fmt.Printf(" [%d %s] %s on %s\n", heartbeat.InstanceIndex, heartbeat.State, heartbeat.InstanceGuid, heartbeat.DeaGuid[0:5]) } } if len(app.CrashCounts) != 0 { fmt.Printf(" CrashCounts:") for _, crashCount := range app.CrashCounts { fmt.Printf(" [%d]:%d", crashCount.InstanceIndex, crashCount.CrashCount) } fmt.Printf("\n") } appStarts := []models.PendingStartMessage{} appStops := []models.PendingStopMessage{} for _, start := range starts { if start.AppGuid == app.AppGuid && start.AppVersion == app.AppVersion { appStarts = append(appStarts, start) } } for _, stop := range stops { if stop.AppGuid == app.AppGuid && stop.AppVersion == app.AppVersion { appStops = append(appStops, stop) } } if len(appStarts) > 0 { fmt.Printf(" Pending Starts:\n") for _, start := range appStarts { message := []string{} message = append(message, fmt.Sprintf("[%d]", start.IndexToStart)) message = append(message, fmt.Sprintf("priority:%.2f", start.Priority)) if start.SkipVerification { message = append(message, "NO VERIFICATION") } if start.SentOn != 0 { message = append(message, "send:SENT") message = append(message, fmt.Sprintf("delete:%s", time.Unix(start.SentOn+int64(start.KeepAlive), 0).Sub(timeProvider.Time()))) } else { message = append(message, fmt.Sprintf("send:%s", time.Unix(start.SendOn, 0).Sub(timeProvider.Time()))) } fmt.Printf(" %s\n", strings.Join(message, " ")) } } if len(appStops) > 0 { fmt.Printf(" Pending Stops:\n") for _, stop := range appStops { message := []string{} message = append(message, stop.InstanceGuid) if stop.SentOn != 0 { message = append(message, "send:SENT") message = append(message, fmt.Sprintf("delete:%s", time.Unix(stop.SentOn+int64(stop.KeepAlive), 0).Sub(timeProvider.Time()))) } else { message = append(message, fmt.Sprintf("send:%s", time.Unix(stop.SendOn, 0).Sub(timeProvider.Time()))) } fmt.Printf(" %s\n", strings.Join(message, " ")) } } }
func (sender *Sender) Send(timeProvider timeprovider.TimeProvider) error { sender.currentTime = timeProvider.Time() err := sender.store.VerifyFreshness(sender.currentTime) if err != nil { sender.logger.Error("Store is not fresh", err) return err } pendingStartMessages, err := sender.store.GetPendingStartMessages() if err != nil { sender.logger.Error("Failed to fetch pending start messages", err) return err } pendingStopMessages, err := sender.store.GetPendingStopMessages() if err != nil { sender.logger.Error("Failed to fetch pending stop messages", err) return err } sender.apps, err = sender.store.GetApps() if err != nil { sender.logger.Error("Failed to fetch apps", err) return err } sender.sendStartMessages(pendingStartMessages) sender.sendStopMessages(pendingStopMessages) err = sender.metricsAccountant.IncrementSentMessageMetrics(sender.sentStartMessages, sender.sentStopMessages) if err != nil { sender.logger.Error("Failed to increment metrics", err) sender.didSucceed = false } err = sender.store.SavePendingStartMessages(sender.startMessagesToSave...) if err != nil { sender.logger.Error("Failed to save start messages", err) sender.didSucceed = false } err = sender.store.DeletePendingStartMessages(sender.startMessagesToDelete...) if err != nil { sender.logger.Error("Failed to delete start messages", err) sender.didSucceed = false } err = sender.store.SavePendingStopMessages(sender.stopMessagesToSave...) if err != nil { sender.logger.Error("Failed to save stop messages", err) sender.didSucceed = false } err = sender.store.DeletePendingStopMessages(sender.stopMessagesToDelete...) if err != nil { sender.logger.Error("Failed to delete stop messages", err) sender.didSucceed = false } if !sender.didSucceed { return errors.New("Sender failed. See logs for details.") } return nil }