Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/core/autoscaling.go b/core/autoscaling.go
- index f08f705..bd68227 100644
- --- a/core/autoscaling.go
- +++ b/core/autoscaling.go
- @@ -378,7 +378,7 @@ func (a *autoScalingGroup) replaceOnDemandInstanceWithSpot(spotInstanceID string
- } else {
- - if err := a.region.sqsSendMessageSpotInstanceLaunch(&a.name, &spotInstanceID, spotInst.State.Name); err != nil {
- + if err := a.region.sqsSendMessageOnInstanceLaunch(&a.name, &spotInstanceID, spotInst.State.Name, "spot"); err != nil {
- return err
- }
- // add to FinalRecap
- diff --git a/core/main.go b/core/main.go
- index 6d7f8ca..7b06eab 100644
- --- a/core/main.go
- +++ b/core/main.go
- @@ -195,6 +195,7 @@ func (a *AutoSpotting) convertRawEventToCloudwatchEvent(event *json.RawMessage)
- if sqsEvent.Records != nil {
- sqsRecord := sqsEvent.Records[0]
- parseEvent = []byte(sqsRecord.Body)
- + // this will tell us later if the current run was triggered from SQS events
- a.config.sqsReceiptHandle = sqsRecord.ReceiptHandle
- } else {
- a.config.sqsReceiptHandle = ""
- @@ -256,7 +257,7 @@ func (a *AutoSpotting) processEvent(event *json.RawMessage) error {
- if (eventType == InstanceStateChangeNotificationCode ||
- eventType == SpotInstanceInterruptionWarningCode ||
- eventType == InstanceRebalanceRecommendationCode) && instanceID != nil {
- - // Hanlde Instance Events
- + // Handle Instance Events
- log.SetPrefix(fmt.Sprintf("%s:%s ", eventType, *instanceID))
- a.processEventInstance(eventType, cloudwatchEvent.Region, instanceID, instanceState)
- } else if eventType == AWSAPICallCloudTrailCode {
- @@ -357,7 +358,7 @@ func (a *AutoSpotting) handleLifecycleHookEvent(event events.CloudWatchEvent) er
- "attempting to swap it against a running on-demand instance",
- i.region.name, *i.InstanceId)
- - i.region.sqsSendMessageSpotInstanceLaunch(asgName, i.InstanceId, i.State.Name)
- + i.region.sqsSendMessageOnInstanceLaunch(asgName, i.InstanceId, i.State.Name, "spot")
- return nil
- }
- @@ -411,6 +412,19 @@ func (a *AutoSpotting) handleNewInstanceLaunch(regionName string, instanceID str
- func (a *AutoSpotting) handleNewOnDemandInstanceLaunch(r *region, i *instance) error {
- if i.shouldBeReplacedWithSpot(false) {
- +
- + // In case we're not triggered by SQS event we generate such an event and send it to the queue.
- + // We want to delay the further below code for until we're processing it through the SQS queue,
- + // in order to avoid launching Spot instances too early and having them run outside their ASG
- + // for too long.
- + if len(a.config.sqsReceiptHandle) == 0 {
- + if i.asg.isEnabledForEventBasedInstanceReplacement() {
- + return i.region.sqsSendMessageOnInstanceLaunch(&i.asg.name, i.InstanceId, i.State.Name, "on-demand")
- + }
- + return nil
- + }
- + defer i.region.sqsDeleteMessage(i.InstanceId, "on-demand")
- +
- log.Printf("%s instance %s belongs to an enabled ASG and should be "+
- "replaced with spot, attempting to launch spot replacement",
- i.region.name, *i.InstanceId)
- @@ -447,13 +461,14 @@ func (a *AutoSpotting) handleNewSpotInstanceLaunch(r *region, i *instance) error
- return fmt.Errorf("region %s is missing asg data", i.region.name)
- }
- + // in case we're not triggered by SQS event
- if len(a.config.sqsReceiptHandle) == 0 {
- if asg.isEnabledForEventBasedInstanceReplacement() {
- - i.region.sqsSendMessageSpotInstanceLaunch(asgName, i.InstanceId, i.State.Name)
- + return i.region.sqsSendMessageOnInstanceLaunch(asgName, i.InstanceId, i.State.Name, "spot")
- }
- return nil
- }
- - defer i.region.sqsDeleteMessage(i.InstanceId)
- + defer i.region.sqsDeleteMessage(i.InstanceId, "spot")
- log.Printf("%s Found instance %s is not yet attached to its ASG, "+
- "attempting to swap it against a running on-demand instance",
- diff --git a/core/region.go b/core/region.go
- index a3eed6e..3c1c762 100644
- --- a/core/region.go
- +++ b/core/region.go
- @@ -478,7 +478,7 @@ func (r *region) findEnabledASGByName(name string) *autoScalingGroup {
- return nil
- }
- -func (r *region) sqsSendMessageSpotInstanceLaunch(asgName *string, instanceID *string, instanceState *string) error {
- +func (r *region) sqsSendMessageOnInstanceLaunch(asgName, instanceID, instanceState *string, instanceLifecycle string) error {
- inputJSON := "{\"version\":\"0\",\"id\":\"890abcde-f123-4567-890a-bcdef1234567\"," +
- "\"detail-type\":\"EC2 Instance State-change Notification\",\"source\":\"aws.events\"," +
- "\"account\":\"\",\"time\":\"" + time.Now().Format(time.RFC3339) + "\"," +
- @@ -497,18 +497,18 @@ func (r *region) sqsSendMessageSpotInstanceLaunch(asgName *string, instanceID *s
- })
- if err != nil {
- - log.Printf("%s Error sending spot instance %s launch event message "+
- - "to the SQS Queue %s: %s", r.name, *instanceID, r.conf.SQSQueueURL, err)
- + log.Printf("%s Error sending %s instance %s launch event message "+
- + "to the SQS Queue %s: %s", r.name, instanceLifecycle, *instanceID, r.conf.SQSQueueURL, err)
- return err
- }
- - log.Printf("%s Successfully sent spot instance %s launch event message"+
- - "to the SQS Queue %s", r.name, *instanceID, r.conf.SQSQueueURL)
- + log.Printf("%s Successfully sent %s instance %s launch event message"+
- + "to the SQS Queue %s", r.name, instanceLifecycle, *instanceID, r.conf.SQSQueueURL)
- return nil
- }
- -func (r *region) sqsDeleteMessage(instanceID *string) error {
- +func (r *region) sqsDeleteMessage(instanceID *string, instanceLifecycle string) error {
- svc := r.services.sqs
- _, err := svc.DeleteMessage(
- @@ -517,8 +517,8 @@ func (r *region) sqsDeleteMessage(instanceID *string) error {
- ReceiptHandle: &r.conf.sqsReceiptHandle,
- })
- if err != nil {
- - log.Printf("%s Error deleting spot instance %s launch event message "+
- - "from the SQS Queue %s: %s", r.name, *instanceID, r.conf.SQSQueueURL, err)
- + log.Printf("%s Error deleting %s instance %s launch event message "+
- + "from the SQS Queue %s: %s", r.name, instanceLifecycle, *instanceID, r.conf.SQSQueueURL, err)
- return err
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement