private static void Adicionar(IList <Siscori> entity) { var status = string.Empty; using (var db = new BWContext()) { try { db.BulkInsert(entity); Console.WriteLine("Capitulo {0} data {1} importados {2} arquivos.", entity[0].NcmCodigo, entity[0].AnoMes, entity.Count); status = "Processado"; System.Threading.Thread.Sleep(5000); } catch (Exception e) { Console.WriteLine("Ocorreu um erro: {0} no Capitulo {1} data {2}", e.Message, entity[0].NcmCodigo, entity[0].AnoMes); status = e.Message; } finally { Log.LogGravacao(new LogEntity { AnoMes = entity[0].AnoMes, Capitulo = entity[0].NcmCodigo.Substring(0, 2), Linhas = entity.Count, Status = status, Data = DateTime.Now }); } } }
private bool ProcessNonblockingRequest(TokenRequest request, BWContext context) { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; return(true); } } else { if (context.tokenRc[request.channel] < 0) { return(true); } if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; return(true); } } (context.pendingRequestArray[request.channel] as IList).Add(request); return(false); }
protected CommandHandlerBase(HandlerContext context) { DBContext = context.DBContext; Logger = context.Logger; Mediator = context.Mediator; Mapper = context.Mapper; Repository = context.Repository; }
protected HandlerContext(IMediator mediator, BioRepository bioRepository, BWContext dbContext, IMapper mapper, ILogger logger) { Mediator = mediator; DBContext = dbContext; Logger = logger; Mapper = mapper; Repository = bioRepository; }
public ITokenBucket GetVideoBucket(IBWControlContext context) { if (!(context is BWContext)) { return(null); } BWContext context2 = (BWContext)context; return(context2.buckets[1]); }
public ITokenBucket GetDataBucket(IBWControlContext context) { if (!(context is BWContext)) { return(null); } BWContext c = (BWContext)context; return(c.buckets[2]); }
private bool ProcessRequest(TokenRequest request) { IBWControllable bc = request.initialBC; while (bc != null) { BWContext context = _contextMap[bc] as BWContext; if (context == null) { RollbackRequest(request); return(false); } lock (context) { if (context.bwConfig != null) { bool result; if (request.type == TokenRequestType.BLOCKING) { result = ProcessBlockingRequest(request, context); } else if (request.type == TokenRequestType.NONBLOCKING) { result = ProcessNonblockingRequest(request, context); } else { result = ProcessBestEffortRequest(request, context); } if (!result) { // for non-blocking mode, the callback is // recorded and will be rolled back when being reset, // so we don't need to do rollback here. if (request.type != TokenRequestType.NONBLOCKING) { RollbackRequest(request); } return(false); } } TokenRequestContext requestContext = new TokenRequestContext(); requestContext.acquiredToken = request.requestToken; requestContext.bc = bc; request.acquiredStack.Push(requestContext); } bc = bc.GetParentBWControllable(); } // for best effort request, we need to rollback over-charged tokens if (request.type == TokenRequestType.BEST_EFFORT) { RollbackRequest(request); } return(true); }
public void ResetBuckets(IBWControlContext context) { if (context is BWContext) { BWContext context2 = (BWContext)context; for (int i = 0; i < 3; i++) { context2.buckets[i].Reset(); } } }
public void ResetBuckets(IBWControlContext context) { if (!(context is BWContext)) { return; } BWContext c = (BWContext)context; for (int i = 0; i < 3; i++) { c.buckets[i].Reset(); } }
private void InvokeCallback(BWContext context) { // loop through all channels in a context for (int i = 0; i < 3; i++) { IList pendingList = context.pendingRequestArray[i] as IList; if (pendingList.Count > 0) { // loop through all pending requests in a channel foreach (TokenRequest request in pendingList) { IBWControllable bc = context.GetBWControllable(); while (bc != null) { BWContext c = _contextMap[bc] as BWContext; if (c == null) { // context has been unregistered, we should ignore // this callback break; } lock (c) { if (c.bwConfig != null && !ProcessNonblockingRequest(request, c)) { break; } } TokenRequestContext requestContext = new TokenRequestContext(); requestContext.acquiredToken = request.requestToken; requestContext.bc = bc; request.acquiredStack.Push(requestContext); bc = bc.GetParentBWControllable(); } if (bc == null) { // successfully got the required tokens try { request.callback.Available(context.buckets[request.channel], (long)request.requestToken); } catch (Exception ex) { log.Error("Error calling request's callback", ex); } } } pendingList.Clear(); } } }
private bool ProcessRequest(TokenRequest request) { for (IBWControllable controllable = request.initialBC; controllable != null; controllable = controllable.GetParentBWControllable()) { BWContext context = this._contextMap[controllable] as BWContext; if (context == null) { this.RollbackRequest(request); return(false); } lock (context) { if (context.bwConfig != null) { bool flag; if (request.type == TokenRequestType.BLOCKING) { flag = this.ProcessBlockingRequest(request, context); } else if (request.type == TokenRequestType.NONBLOCKING) { flag = this.ProcessNonblockingRequest(request, context); } else { flag = this.ProcessBestEffortRequest(request, context); } if (!flag) { if (request.type != TokenRequestType.NONBLOCKING) { this.RollbackRequest(request); } return(false); } } TokenRequestContext context2 = new TokenRequestContext { acquiredToken = request.requestToken, bc = controllable }; request.acquiredStack.Push(context2); } } if (request.type == TokenRequestType.BEST_EFFORT) { this.RollbackRequest(request); } return(true); }
public static void LogGravacao(LogEntity log) { using (var db = new BWContext()) { try { db.Add(log); db.SaveChanges(); } catch (Exception e) { throw new Exception(e.Message); } } }
private void InvokeCallback(BWContext context) { for (int i = 0; i < 3; i++) { IList list = context.pendingRequestArray[i] as IList; if (list.Count > 0) { foreach (TokenRequest request in list) { IBWControllable bWControllable = context.GetBWControllable(); while (bWControllable != null) { BWContext context2 = this._contextMap[bWControllable] as BWContext; if (context2 == null) { break; } lock (context2) { if (!((context2.bwConfig == null) || this.ProcessNonblockingRequest(request, context2))) { break; } } TokenRequestContext context3 = new TokenRequestContext { acquiredToken = request.requestToken, bc = bWControllable }; request.acquiredStack.Push(context3); bWControllable = bWControllable.GetParentBWControllable(); } if (bWControllable == null) { try { request.callback.Available(context.buckets[request.channel], (long)request.requestToken); } catch (Exception exception) { log.Error("Error calling request's callback", exception); } } } list.Clear(); } } }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { int num; BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (num = 0; num < 4; num++) { context.bwConfig[num] = bc.BandwidthConfiguration.GetChannelBandwidth()[num]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { for (num = 0; num < 4; num++) { if (channelInitialBurst[num] >= 0L) { context.tokenRc[num] = channelInitialBurst[num]; } else { context.tokenRc[num] = this._defaultCapacity / 2L; } } context.lastSchedule = Environment.TickCount; } else { context.lastSchedule = -1L; } lock (this.SyncRoot) { this._contextMap.Add(bc, context); } return(context); }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { context.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { context.tokenRc[i] = channelInitialBurst[i]; } else { context.tokenRc[i] = _defaultCapacity / 2; } } context.lastSchedule = System.Environment.TickCount; } else { context.lastSchedule = -1; } lock (this.SyncRoot) { _contextMap.Add(bc, context); } return(context); }
public void Reset() { // TODO wake up all blocked threads IBWControllable bc = _bc; while (bc != null) { BWContext context = _simpleBWControlService.ContextMap[bc] as BWContext; if (context == null) { break; } lock (context) { IList pendingList = context.pendingRequestArray[_channel] as IList; TokenRequest toRemove = null; foreach (TokenRequest request in pendingList) { if (request.initialBC == _bc) { _simpleBWControlService.RollbackRequest(request); toRemove = request; break; } } if (toRemove != null) { pendingList.Remove(toRemove); try { toRemove.callback.Reset(this, (long)toRemove.requestToken); } catch (Exception ex) { log.Error("Error reset request's callback", ex); } break; } } bc = bc.GetParentBWControllable(); } }
/** * Give back the acquired tokens due to failing to accomplish the requested * operation or over-charged tokens in the case of best-effort request. * @param request */ private void RollbackRequest(TokenRequest request) { while (request.acquiredStack.Count > 0) { TokenRequestContext requestContext = request.acquiredStack.Pop() as TokenRequestContext; BWContext context = _contextMap[requestContext.bc] as BWContext; if (context != null) { lock (context) { if (context.bwConfig != null) { if (context.bwConfig[3] >= 0) { if (request.type == TokenRequestType.BEST_EFFORT) { context.tokenRc[3] += requestContext.acquiredToken - request.requestToken; } else { context.tokenRc[3] += requestContext.acquiredToken; } } else { if (context.bwConfig[request.channel] >= 0) { if (request.type == TokenRequestType.BEST_EFFORT) { context.tokenRc[request.channel] += requestContext.acquiredToken - request.requestToken; } else { context.tokenRc[request.channel] += requestContext.acquiredToken; } } } } } } } }
private bool ProcessBestEffortRequest(TokenRequest request, BWContext context) { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; } else { request.requestToken = context.tokenRc[3]; context.tokenRc[3] = 0; } } else { if (context.tokenRc[request.channel] < 0) { return(true); } if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; } else { request.requestToken = context.tokenRc[request.channel]; context.tokenRc[request.channel] = 0; } } if (request.requestToken == 0) { return(false); } else { return(true); } }
private bool ProcessBlockingRequest(TokenRequest request, BWContext context) { context.timeToWait = request.timeout; do { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; request.timeout = context.timeToWait; return(true); } } else { if (context.tokenRc[request.channel] < 0) { return(true); } if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; request.timeout = context.timeToWait; return(true); } } long beforeWait = System.Environment.TickCount; try { Monitor.Wait(this, (int)context.timeToWait); } catch (ThreadInterruptedException) { } context.timeToWait -= System.Environment.TickCount - beforeWait; } while (context.timeToWait > 0); return(false); }
private void CreateBackup(string name, string comment, string rootDirectory, IEnumerable <string> replayFiles) { // Dispatcher.Invoke(() => backupProgressBarLabel.Content = "Creating backup..."); var dbName = _activeUow.DatabaseName; var createBackup = Models.CreateBackup.Create(name, comment, rootDirectory, replayFiles); var backupId = _activeUow.BackupRepository.Create(createBackup.ToBackup()); _activeUow.Commit(); using (var uow = BWContext.Create(dbName, false)) { var backup = uow.BackupRepository.Get(backupId); var count = uow.BackupRepository.GetNumberOfBackedUpReplays(backupId).Value; _backupWithCount = new BackupWithCount { Id = backup.Id, Name = backup.Name, Comment = backup.Comment, RootDirectory = backup.RootDirectory, Date = backup.Date, Count = count }; } // Dispatcher.Invoke(() => backupProgressBarLabel.Content = "Finished creating backup!"); }
public GalleryRepository(BWContext context, ParentEntityProvider parentEntityProvider) : base(context, parentEntityProvider) { }
public ParentEntityProvider(BWContext dbContext) { _dbContext = dbContext; }
public void UpdateBWConfigure(IBWControlContext context) { if (!(context is BWContext)) { return; } BWContext c = (BWContext)context; IBWControllable bc = c.GetBWControllable(); lock (c) { if (bc.BandwidthConfiguration == null) { c.bwConfig = null; c.lastSchedule = -1; } else { long[] oldConfig = c.bwConfig; c.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { c.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } if (oldConfig == null) { // Initialize the last schedule timestamp if necessary c.lastSchedule = System.Environment.TickCount; long[] channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { c.tokenRc[i] = channelInitialBurst[i]; } else { c.tokenRc[i] = _defaultCapacity / 2; } } } else { // we have scheduled before, so migration of token is needed if (c.bwConfig[Constants.OverallChannel] >= 0 && oldConfig[Constants.OverallChannel] < 0) { c.tokenRc[Constants.OverallChannel] += c.tokenRc[Constants.AudioChannel] + c.tokenRc[Constants.VideoChannel] + c.tokenRc[Constants.DataChannel]; for (int i = 0; i < 3; i++) { c.tokenRc[i] = 0; } } else if (c.bwConfig[Constants.OverallChannel] < 0 && oldConfig[Constants.OverallChannel] >= 0) { for (int i = 0; i < 3; i++) { if (c.bwConfig[i] >= 0) { c.tokenRc[i] += c.tokenRc[Constants.OverallChannel]; break; } } c.tokenRc[Constants.OverallChannel] = 0; } } } } }
private bool ProcessBestEffortRequest(TokenRequest request, BWContext context) { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; } else { request.requestToken = context.tokenRc[3]; context.tokenRc[3] = 0; } } else { if (context.tokenRc[request.channel] < 0) return true; if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; } else { request.requestToken = context.tokenRc[request.channel]; context.tokenRc[request.channel] = 0; } } if (request.requestToken == 0) return false; else return true; }
private bool ProcessNonblockingRequest(TokenRequest request, BWContext context) { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; return true; } } else { if (context.tokenRc[request.channel] < 0) return true; if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; return true; } } (context.pendingRequestArray[request.channel] as IList).Add(request); return false; }
private bool ProcessBlockingRequest(TokenRequest request, BWContext context) { context.timeToWait = request.timeout; do { if (context.bwConfig[3] >= 0) { if (context.tokenRc[3] >= request.requestToken) { context.tokenRc[3] -= request.requestToken; request.timeout = context.timeToWait; return true; } } else { if (context.tokenRc[request.channel] < 0) return true; if (context.tokenRc[request.channel] >= request.requestToken) { context.tokenRc[request.channel] -= request.requestToken; request.timeout = context.timeToWait; return true; } } long beforeWait = System.Environment.TickCount; try { Monitor.Wait(this, (int)context.timeToWait); } catch (ThreadInterruptedException) { } context.timeToWait -= System.Environment.TickCount - beforeWait; } while (context.timeToWait > 0); return false; }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { context.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { context.tokenRc[i] = channelInitialBurst[i]; } else { context.tokenRc[i] = _defaultCapacity / 2; } } context.lastSchedule = System.Environment.TickCount; } else { context.lastSchedule = -1; } lock (this.SyncRoot) { _contextMap.Add(bc, context); } return context; }
protected BaseBioRepository(BWContext context, ParentEntityProvider parentEntityProvider) { Context = context; ParentEntityProvider = parentEntityProvider; }
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, BWContext context) { if (_env.IsProduction() && context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } app.UseAuthentication(); app.UseCors("allorigins"); app.UseMvc(routes => { routes.MapRoute( "default", "{controller=Index}/{action=Index}/{id?}"); routes.UseBioEngineRouting(); }); applicationLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose()); }
public void UpdateBWConfigure(IBWControlContext context) { if (context is BWContext) { BWContext context2 = (BWContext)context; IBWControllable bWControllable = context2.GetBWControllable(); lock (context2) { if (bWControllable.BandwidthConfiguration == null) { context2.bwConfig = null; context2.lastSchedule = -1L; } else { int num; long[] bwConfig = context2.bwConfig; context2.bwConfig = new long[4]; for (num = 0; num < 4; num++) { context2.bwConfig[num] = bWControllable.BandwidthConfiguration.GetChannelBandwidth()[num]; } if (bwConfig == null) { context2.lastSchedule = Environment.TickCount; long[] channelInitialBurst = bWControllable.BandwidthConfiguration.GetChannelInitialBurst(); for (num = 0; num < 4; num++) { if (channelInitialBurst[num] >= 0L) { context2.tokenRc[num] = channelInitialBurst[num]; } else { context2.tokenRc[num] = this._defaultCapacity / 2L; } } } else if ((context2.bwConfig[3] >= 0L) && (bwConfig[3] < 0L)) { context2.tokenRc[3] += (context2.tokenRc[0] + context2.tokenRc[1]) + context2.tokenRc[2]; for (num = 0; num < 3; num++) { context2.tokenRc[num] = 0.0; } } else if ((context2.bwConfig[3] < 0L) && (bwConfig[3] >= 0L)) { for (num = 0; num < 3; num++) { if (context2.bwConfig[num] >= 0L) { context2.tokenRc[num] += context2.tokenRc[3]; break; } } context2.tokenRc[3] = 0.0; } } } } }