Handle response code 410

This commit is contained in:
斟酌 鵬兄 2016-06-29 12:08:23 +08:00
parent 0a45c7f391
commit 04eb609d5e
3 changed files with 38 additions and 19 deletions

View File

@ -121,7 +121,7 @@ class WNSAuth extends EventEmitter
{ {
var _self = this; var _self = this;
Model.Tokens Model.Tokens
.findOne({ name: NotisQ.id }) .findOne({ name: NotisQ.id, expired: false })
.exec( ( err, data ) => { .exec( ( err, data ) => {
if( err ) if( err )
{ {
@ -134,24 +134,35 @@ class WNSAuth extends EventEmitter
this.__send( data.token, NotisQ, ( sender, e ) => { this.__send( data.token, NotisQ, ( sender, e ) => {
Dragonfly.Debug( "Send: " + e.statusCode ); Dragonfly.Debug( "Send: " + e.statusCode );
if( e.statusCode != 200 ) switch( e.statusCode )
{ {
AuthToken = null; case 200: break;
Dragonfly.Info( "Perhaps access token is expired, retrying ..." ); case 410:
Dragonfly.Info( "Channel is expired: " + NotisQ.id );
if( NotisQ.Retry < 2 ) data.expired = true;
{ data.save( x => {
_self.once( "AuthComplete", () => { Dragonfly.Info( "Mark expired: " + NotisQ.id );
NotisQ.Retry ++;
_self.Deliver( NotisQ );
}); });
}
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; var _self = this;
Model.Tokens.update( Model.Tokens.update(
{ name: uuid } { name: uuid }
, { name: uuid, token: ChannelUri, date_created: Date.now() } , {
name: uuid
, token: ChannelUri
, date_created: Date.now()
, expired: false
}
, { upsert: true } , { upsert: true }
) )
.exec( ( err, data ) => { .exec( ( err, data ) => {
@ -256,7 +272,10 @@ class WNSAuth extends EventEmitter
, { name: AuthTokenName, token: AuthToken, date_created: Date.now() } , { name: AuthTokenName, token: AuthToken, date_created: Date.now() }
, { upsert: true } , { upsert: true }
) )
.exec( ( err, data ) => _self.__emitAuthComplete() ); .exec( ( err, data ) => {
if( err ) Dragonfly.Error( err );
_self.__emitAuthComplete();
});
} }
else else
{ {

View File

@ -7,7 +7,6 @@ var HttpRequest = cl.load( "botanss.net.HttpRequest" );
var Base = cl.load( "botanss.net.PostFrame" ); var Base = cl.load( "botanss.net.PostFrame" );
var WNSAuth = cl.load( "notifyterm.WNSAuth" ); var WNSAuth = cl.load( "notifyterm.WNSAuth" );
var Model = cl.load( "notifyterm.schema" );
var NotisQ = cl.load( "notifyterm.Notis" ); var NotisQ = cl.load( "notifyterm.Notis" );
var SrvAuth = cl.load( "notifyterm.config.auth" ); var SrvAuth = cl.load( "notifyterm.config.auth" );

View File

@ -25,6 +25,7 @@ var R_Tokens = { type: Schema.Types.ObjectId, ref: "Tokens" };
var M_Tokens = new Schema({ var M_Tokens = new Schema({
name: { type: String, unique: true } name: { type: String, unique: true }
, token: { type: String } , token: { type: String }
, expired: { type: Boolean, default: false }
, date_created: { , date_created: {
type: Date type: Date
, default: Date.now , default: Date.now