-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #741 from eriknelson/bz1293985-2
1293985 - Disconnected content mirror validation
- Loading branch information
Showing
9 changed files
with
349 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import Ember from 'ember'; | ||
import TextFComponent from './text-f'; | ||
import request from 'ic-ajax'; | ||
|
||
const CDN_VERIFY_TIMEOUT = 3000; | ||
|
||
export default TextFComponent.extend({ | ||
responseCounter: 0, | ||
validationTrigger: null, | ||
isVerifyingContentMirror: false, | ||
dirty: false, | ||
|
||
didInsertElement: function (){ | ||
if(this.get('cdnUrl')) { | ||
this.queueValidation(); | ||
} | ||
}, | ||
|
||
contentMirrorObserver: Ember.observer('cdnUrl', function() { | ||
this.queueValidation(); | ||
}), | ||
|
||
queueValidation: function() { | ||
if(this.get('isVerifyingContentMirror') === false) { | ||
this.setIsVerifyingContentMirror(true); | ||
} | ||
|
||
this.set('dirty', true); | ||
const validationTrigger = this.get('validationTrigger'); | ||
|
||
if(validationTrigger) { | ||
Ember.run.cancel(validationTrigger); | ||
} | ||
|
||
this.set( | ||
'validationTrigger', | ||
Ember.run.later(this, () => this.onValidate(), CDN_VERIFY_TIMEOUT) | ||
); | ||
}, | ||
|
||
onValidate: function() { | ||
const cdnUrl = this.get('cdnUrl'); | ||
const protocolCheckRx = /^https?:\/\//; | ||
|
||
if(!protocolCheckRx.test(cdnUrl)) { | ||
this.setIsVerifyingContentMirror(false); | ||
this.setContentMirrorValidation(false, 'Missing http protocol'); | ||
return; | ||
} | ||
|
||
// Guard against race condition of newer responses returning faster | ||
// than old responses that could result in valid content mirrors | ||
// being marked invalid, or vice versa | ||
const responseCounter = this.get('responseCounter') + 1; | ||
this.set('responseCounter', responseCounter); | ||
|
||
const token = Ember.$('meta[name="csrf-token"]').attr('content'); | ||
const deploymentId = this.get('deploymentId'); | ||
|
||
const shouldUpdate = () => { | ||
return responseCounter === this.get('responseCounter') && | ||
!this.get('dirty'); | ||
}; | ||
|
||
this.set('dirty', false); | ||
this.set('validationTrigger', null); | ||
|
||
request({ | ||
url: `/fusor/api/v21/deployments/${deploymentId}/validate_cdn`, | ||
headers: { | ||
"Accept": "application/json", | ||
"X-CSRF-Token": token | ||
}, | ||
data: { | ||
cdn_url: encodeURIComponent(cdnUrl) | ||
} | ||
}).then((res) => { | ||
// If the response is not the newest response local responseCounter | ||
// will be less than the responseCounter member field), | ||
// we want throw away the result since we know a more accurate | ||
// result is incoming or already has updated our state | ||
if(shouldUpdate()) { | ||
this.setContentMirrorValidation(res.cdn_url_code === '200'); | ||
} | ||
}).catch((err) => { | ||
if(shouldUpdate()) { | ||
this.setContentMirrorValidation(false); | ||
} | ||
}).finally(() => { | ||
if(shouldUpdate()) { | ||
this.setIsVerifyingContentMirror(false); | ||
} | ||
}); | ||
}, | ||
|
||
setContentMirrorValidation: function(isValid, validationMsg) { | ||
this.set('isContentMirrorValid', isValid); | ||
|
||
if(isValid) { | ||
if(!validationMsg) { | ||
this.set( | ||
'contentMirrorValidationMsg', | ||
'Content mirror verified' | ||
); | ||
} | ||
this.sendAction('mirrorStatusUpdate', this.get('MirrorStatus').VALID); | ||
} else { | ||
if(!validationMsg) { | ||
this.set( | ||
'contentMirrorValidationMsg', | ||
'Invalid content mirror' | ||
); | ||
} | ||
this.sendAction('mirrorStatusUpdate', this.get('MirrorStatus').INVALID); | ||
} | ||
|
||
if(validationMsg) { | ||
this.set( | ||
'contentMirrorValidationMsg', | ||
validationMsg | ||
); | ||
} | ||
}, | ||
setIsVerifyingContentMirror: function(isVerifying) { | ||
this.set('isVerifyingContentMirror', isVerifying); | ||
|
||
if(isVerifying) { | ||
this.sendAction( | ||
'mirrorStatusUpdate', this.get('MirrorStatus').VALIDATING); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
fusor-ember-cli/app/templates/components/content-mirror-f.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{#text-f label="Content Mirror URL" value=cdnUrl disabled=false}} | ||
<div class="content-mirror-status"> | ||
{{#if isVerifyingContentMirror}} | ||
<span>Verifying mirror...</span> | ||
<span class="spinner spinner-md spinner-inline"></span> | ||
{{else if isContentMirrorValid}} | ||
<span class="valid-msg">{{contentMirrorValidationMsg}}</span> | ||
{{else}} | ||
<span class="invalid-msg">{{contentMirrorValidationMsg}}</span> | ||
{{/if}} | ||
</div> | ||
{{/text-f}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
put :deploy | ||
put :redeploy | ||
get :validate | ||
get :validate_cdn | ||
get :log | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
inherit_from: ../../.rubocop.yml | ||
|
||
# Disable class length offenses for test files | ||
Metrics/ClassLength: | ||
Enabled: false |
Oops, something went wrong.