| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 |
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. |
| 5 | 5 |
| 6 It contains the definition of the following Behaviors: | 6 It contains the definition of the following Behaviors: |
| 7 | 7 |
| 8 SwarmingBehaviors.CommonBehavior | 8 SwarmingBehaviors.CommonBehavior |
| 9 | 9 |
| 10 To use it, include | 10 To use it, include |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 _cloudConsoleLink: function(zone, bot_id) { | 52 _cloudConsoleLink: function(zone, bot_id) { |
| 53 return `http://console.cloud.google.com/compute/instancesDetail/zones/${
zone}/instances/${bot_id}` | 53 return `http://console.cloud.google.com/compute/instancesDetail/zones/${
zone}/instances/${bot_id}` |
| 54 }, | 54 }, |
| 55 | 55 |
| 56 // _getJsonAsync makes an XHR to a url, parses the response as JSON | 56 // _getJsonAsync makes an XHR to a url, parses the response as JSON |
| 57 // and sticks the resulting object into the property with the name given | 57 // and sticks the resulting object into the property with the name given |
| 58 // by "bindTo". If busy is defined, the property with that name will be | 58 // by "bindTo". If busy is defined, the property with that name will be |
| 59 // set to true while the request is in flight and false afterwards. | 59 // set to true while the request is in flight and false afterwards. |
| 60 // request headers (e.g. authentication) and query params will be used if | 60 // request headers (e.g. authentication) and query params will be used if |
| 61 // provided. Query params is an object like {String:Array<String>}. On | 61 // provided. Query params is an object like {String:Array<String>}. On |
| 62 // error, bindTo will be set to false. It is not set to undefined | 62 // error, bindTo will be set to false and the promise will be rejected |
| 63 // because computed values in Polymer don't fire if a property is | 63 // with {status:Number, reason:String} where the reason is the text |
| 64 // undefined. Clients should check that bindTo is not falsey. | 64 // returned from the server. |
| 65 // The object bindTo is not set to undefined because computed values in |
| 66 // Polymer don't fire if a property is undefined. Clients should check |
| 67 // that bindTo is not falsey. |
| 65 // To avoid multiple requests clobering one another, an object _jsonAsync | 68 // To avoid multiple requests clobering one another, an object _jsonAsync |
| 66 // is created on "this" to debounce requests - the most recent request | 69 // is created on "this" to debounce requests - the most recent request |
| 67 // will win out. | 70 // will win out. |
| 68 _getJsonAsync: function(bindTo, url, busy, headers, params) { | 71 _getJsonAsync: function(bindTo, url, busy, headers, params) { |
| 69 if (!bindTo || !url || !busy) { | 72 if (!bindTo || !url || !busy) { |
| 70 console.log("Need at least a polymer element to bind to, a busy elemen
t, and a url"); | 73 console.log("Need at least a polymer element to bind to, a busy elemen
t, and a url"); |
| 71 return; | 74 return; |
| 72 } | 75 } |
| 73 this.set(busy, true); | 76 this.set(busy, true); |
| 74 var now = new Date(); | 77 var now = new Date(); |
| 75 this._jsonAsync = this._jsonAsync || {}; | 78 this._jsonAsync = this._jsonAsync || {}; |
| 76 this._jsonAsync[bindTo] = now; | 79 this._jsonAsync[bindTo] = now; |
| 77 if (params) { | 80 if (params) { |
| 78 url = url + "?" + sk.query.fromParamSet(params); | 81 url = url + "?" + sk.query.fromParamSet(params); |
| 79 } | 82 } |
| 80 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json)
{ | 83 return sk.request("GET", url, "", headers).then(JSON.parse).then(functio
n(json){ |
| 81 if (this._jsonAsync[bindTo] !== now) { | 84 if (this._jsonAsync[bindTo] !== now) { |
| 82 console.log("ignoring result because a second request happened."); | 85 console.log("ignoring result because a second request happened."); |
| 83 this.set(busy, false); | 86 this.set(busy, false); |
| 84 return; | 87 return; |
| 85 } | 88 } |
| 86 this.set(bindTo, json); | 89 this.set(bindTo, json); |
| 87 this.set(busy, false); | 90 this.set(busy, false); |
| 88 }.bind(this)).catch(function(reason){ | 91 }.bind(this)).catch(function(reason){ |
| 89 console.log("Reason for failure of request to " + url, reason); | 92 console.log("Reason for failure of request to " + url, reason); |
| 90 | 93 |
| 91 if (this._jsonAsync[bindTo] !== now) { | 94 if (this._jsonAsync[bindTo] !== now) { |
| 92 console.log("ignoring failure because a second request happened."); | 95 console.log("ignoring failure because a second request happened."); |
| 93 this.set(busy, false); | 96 this.set(busy, false); |
| 94 return; | 97 return; |
| 95 } | 98 } |
| 96 this.set(bindTo, false); | 99 this.set(bindTo, false); |
| 97 this.set(busy, false); | 100 this.set(busy, false); |
| 101 return Promise.reject(reason); |
| 98 }.bind(this)); | 102 }.bind(this)); |
| 99 }, | 103 }, |
| 100 | 104 |
| 101 _humanDuration: function(timeInSecs) { | 105 _humanDuration: function(timeInSecs) { |
| 102 return sk.human.strDuration(timeInSecs) || "0s"; | 106 return sk.human.strDuration(timeInSecs) || "0s"; |
| 103 }, | 107 }, |
| 104 | 108 |
| 105 _not: function(a) { | 109 _not: function(a) { |
| 106 return !a; | 110 return !a; |
| 107 }, | 111 }, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 155 } |
| 152 return this._humanDuration((second.getTime() - first.getTime())/1000); | 156 return this._humanDuration((second.getTime() - first.getTime())/1000); |
| 153 }, | 157 }, |
| 154 | 158 |
| 155 _truthy: function(a){ | 159 _truthy: function(a){ |
| 156 return !!a; | 160 return !!a; |
| 157 } | 161 } |
| 158 }; | 162 }; |
| 159 })(); | 163 })(); |
| 160 </script> | 164 </script> |
| OLD | NEW |