public void DispatchExpireOnce(BahamutAppInstance instance)
 {
     if (OnExpireOnce != null)
     {
         OnExpireOnce.DynamicInvoke(this, new KeepAliveObserverEventArgs() { Instance = instance });
     }
 }
 public async Task<bool> RegistAppInstanceAsync(BahamutAppInstance instance)
 {
     instance.RegistTime = DateTime.UtcNow;
     instance.Id = Guid.NewGuid().ToString();
     var suc = await redis.GetDatabase().StringSetAsync(instance.GetInstanceIdKey(), instance.ToJson());
     if (suc)
     {
         return await PublishNotifyAsync(instance, BahamutAppInstanceNotification.TYPE_REGIST_APP_INSTANCE);
     }
     return false;
 }
 public void DispatchExpireError(BahamutAppInstance instance, Exception ex)
 {
     try
     {
         if (OnExpireError != null)
         {
             OnExpireError.DynamicInvoke(this, new KeepAliveObserverEventArgs() { Instance = instance, Exception = ex });
         }
     }
     catch (Exception)
     {
         
     }
     
 }
 public static void RegistAppInstance(ServerControlManagementService ManagementService,BahamutAppInstance appInstance)
 {
     if (BahamutAppInstanceRegister.ManagementService == null)
     {
         BahamutAppInstanceRegister.ManagementService = ManagementService;
     }
     Task.Run(async () =>
     {
         try
         {
             await ManagementService.RegistAppInstanceAsync(appInstance);
             var observer = ManagementService.StartKeepAlive(appInstance);
             observer.OnExpireError += KeepAliveObserver_OnExpireError;
             observer.OnExpireOnce += KeepAliveObserver_OnExpireOnce;
             LogManager.GetLogger("Main").Info("Bahamut App Instance:" + appInstance.Id.ToString());
             LogManager.GetLogger("Main").Info("Keep Server Instance Alive To Server Controller Thread Started!");
         }
         catch (Exception ex)
         {
             LogManager.GetLogger("Main").Error(ex, "Unable To Regist App Instance");
         }
     });
 }
 public static BahamutAppInstanceNotification GenerateNotificationByInstance(BahamutAppInstance instance,string notifyType)
 {
     return new BahamutAppInstanceNotification
     {
         Appkey = instance.Appkey,
         InstanceId = instance.Id,
         NotifyType = notifyType
     };
 }
 public async Task<bool> ReActiveAppInstance(BahamutAppInstance instance)
 {
     var instanceJson = instance.ToJson();
     var suc = await redis.GetDatabase().StringSetAsync(instance.GetInstanceIdKey(), instanceJson);
     if (suc)
     {
         return await PublishNotifyAsync(instance, BahamutAppInstanceNotification.TYPE_REGIST_APP_INSTANCE);
     }
     return false;
 }
 public async Task<bool> NotifyAppInstanceOfflineAsync(BahamutAppInstance instance)
 {
     return await PublishNotifyAsync(instance, BahamutAppInstanceNotification.TYPE_INSTANCE_OFFLINE);
 }
 private async Task<bool> PublishNotifyAsync(BahamutAppInstance instance, string type)
 {
     var notify = BahamutAppInstanceNotification.GenerateNotificationByInstance(instance, type).ToJson();
     var x = await redis.GetSubscriber().PublishAsync(instance.Channel, notify);
     return x > 0;
 }
 public KeepAliveObserver StartKeepAlive(BahamutAppInstance instance)
 {
     var observer = new KeepAliveObserver();
     var thread = new Thread(() =>
     {
         var idKey = instance.GetInstanceIdKey();
         while (true)
         {
             try
             {
                 var db = redis.GetDatabase();
                 if (db.KeyExpire(idKey, AppInstanceExpireTime))
                 {
                     observer.DispatchExpireOnce(instance);
                 }else
                 {
                     observer.DispatchExpireError(instance, new Exception("Expire Instance Error"));
                 }
             }
             catch (Exception ex)
             {
                 observer.DispatchExpireError(instance, ex);
             }
             Thread.Sleep((int)(AppInstanceExpireTime.TotalMilliseconds * 3 / 4));
         }
     });
     thread.IsBackground = true;
     thread.Start();
     return observer;
 }
 public async Task<bool> NotifyAppInstanceHeartBeatAsync(BahamutAppInstance instance)
 {
     return await PublishNotifyAsync(instance, BahamutAppInstanceNotification.TYPE_INSTANCE_HEART_BEAT);
 }
Example #11
0
        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ServicesProvider = app.ApplicationServices;

            //Log
            var logConfig = new LoggingConfiguration();
            LoggerLoaderHelper.LoadLoggerToLoggingConfig(logConfig, Configuration, "Data:Log:fileLoggers");

            if (env.IsDevelopment())
            {
                LoggerLoaderHelper.AddConsoleLoggerToLogginConfig(logConfig);
            }
            LogManager.Configuration = logConfig;

            //Regist App Instance
            var serverMgrService = ServicesProvider.GetServerControlManagementService();
            var appInstance = new BahamutAppInstance()
            {
                Appkey = Appkey,
                InstanceServiceUrl = Configuration["Data:App:url"],
                Region = Configuration["Data:App:region"]
            };
            try
            {
                BahamutAppInstance = serverMgrService.RegistAppInstance(appInstance);
                var observer = serverMgrService.StartKeepAlive(BahamutAppInstance);
                observer.OnExpireError += KeepAliveObserver_OnExpireError;
                observer.OnExpireOnce += KeepAliveObserver_OnExpireOnce;
                LogManager.GetLogger("Main").Info("Bahamut App Instance:" + BahamutAppInstance.Id.ToString());
                LogManager.GetLogger("Main").Info("Keep Server Instance Alive To Server Controller Thread Started!");
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("Main").Error(ex, "Unable To Regist App Instance");
            }

            //Authentication
            var openRoutes = new string[]
            {
                "/Tokens",
                "/NewSharelinkers"
            };
            app.UseMiddleware<BahamutAspNetCommon.TokenAuthentication>(Appkey, ServicesProvider.GetTokenService(), openRoutes);

            // Add MVC to the request pipeline.
            app.UseMvc();
            // Add the following route for porting Web API 2 controllers.
            // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
            LogManager.GetLogger("Main").Info("Toronto Started!");
        }