From 04eb609d5e002883ad95ae3cbc2f1a8447be4d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Wed, 29 Jun 2016 12:08:23 +0800 Subject: [PATCH] Handle response code 410 --- notify-term/WNSAuth.js | 55 ++++++++++++++++++++++++++++-------------- notify-term/app.js | 1 - notify-term/schema.js | 1 + 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/notify-term/WNSAuth.js b/notify-term/WNSAuth.js index 37ecb56..5c48092 100644 --- a/notify-term/WNSAuth.js +++ b/notify-term/WNSAuth.js @@ -121,7 +121,7 @@ class WNSAuth extends EventEmitter { var _self = this; Model.Tokens - .findOne({ name: NotisQ.id }) + .findOne({ name: NotisQ.id, expired: false }) .exec( ( err, data ) => { if( err ) { @@ -134,24 +134,35 @@ class WNSAuth extends EventEmitter this.__send( data.token, NotisQ, ( sender, e ) => { Dragonfly.Debug( "Send: " + e.statusCode ); - if( e.statusCode != 200 ) + switch( e.statusCode ) { - AuthToken = null; - Dragonfly.Info( "Perhaps access token is expired, retrying ..." ); - - if( NotisQ.Retry < 2 ) - { - _self.once( "AuthComplete", () => { - NotisQ.Retry ++; - _self.Deliver( NotisQ ); + case 200: break; + case 410: + Dragonfly.Info( "Channel is expired: " + NotisQ.id ); + data.expired = true; + data.save( x => { + Dragonfly.Info( "Mark expired: " + NotisQ.id ); }); - } - else - { - Dragonfly.Info( "Retrying exceeded the limit, dropping the message" ); - } - _self.Authenticate(); + break; + + default: + AuthToken = null; + Dragonfly.Info( "Perhaps access token is expired, retrying ..." ); + + if( NotisQ.Retry < 2 ) + { + _self.once( "AuthComplete", () => { + NotisQ.Retry ++; + _self.Deliver( NotisQ ); + }); + } + else + { + Dragonfly.Info( "Retrying exceeded the limit, dropping the message" ); + } + + _self.Authenticate(); } } ); } @@ -167,7 +178,12 @@ class WNSAuth extends EventEmitter var _self = this; Model.Tokens.update( { name: uuid } - , { name: uuid, token: ChannelUri, date_created: Date.now() } + , { + name: uuid + , token: ChannelUri + , date_created: Date.now() + , expired: false + } , { upsert: true } ) .exec( ( err, data ) => { @@ -256,7 +272,10 @@ class WNSAuth extends EventEmitter , { name: AuthTokenName, token: AuthToken, date_created: Date.now() } , { upsert: true } ) - .exec( ( err, data ) => _self.__emitAuthComplete() ); + .exec( ( err, data ) => { + if( err ) Dragonfly.Error( err ); + _self.__emitAuthComplete(); + }); } else { diff --git a/notify-term/app.js b/notify-term/app.js index 4c6a2c2..e2aa30f 100644 --- a/notify-term/app.js +++ b/notify-term/app.js @@ -7,7 +7,6 @@ var HttpRequest = cl.load( "botanss.net.HttpRequest" ); var Base = cl.load( "botanss.net.PostFrame" ); var WNSAuth = cl.load( "notifyterm.WNSAuth" ); -var Model = cl.load( "notifyterm.schema" ); var NotisQ = cl.load( "notifyterm.Notis" ); var SrvAuth = cl.load( "notifyterm.config.auth" ); diff --git a/notify-term/schema.js b/notify-term/schema.js index f04e6fc..2fb995e 100644 --- a/notify-term/schema.js +++ b/notify-term/schema.js @@ -25,6 +25,7 @@ var R_Tokens = { type: Schema.Types.ObjectId, ref: "Tokens" }; var M_Tokens = new Schema({ name: { type: String, unique: true } , token: { type: String } + , expired: { type: Boolean, default: false } , date_created: { type: Date , default: Date.now