public async Task UpdateServiceProviderSlot_Success() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.djType.Id }; _Context.ServiceSlots.Add(sps); await _Context.SaveChangesAsync(); var newSps = new ServiceSlot { Id = -1, EventId = -1, TypeId = types.catererType.Id }; var r = await _Client.PostAsync($"api/event/{sps.EventId}/sps/{sps.Id}", newSps.ToStringContent()); r.EnsureSuccessStatusCode(); }
public async Task <ServiceSlot> AddServiceSlotAsync(ServiceSlot slot) { _eventContext.ServiceSlots.Add(slot); await _eventContext.SaveChangesAsync(); return(slot); }
public async Task UpdateServiceProviderSlot_422_1_StartBeforeEnd() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.djType.Id }; _Context.ServiceSlots.Add(sps); await _Context.SaveChangesAsync(); var newSps = new ServiceSlot { Id = -1, EventId = -1, TypeId = types.catererType.Id, End = DateTime.UtcNow, Start = DateTime.UtcNow.AddDays(1) }; var r = await _Client.PostAsync($"api/event/{sps.EventId}/sps/{sps.Id}", newSps.ToStringContent()); Assert.Equal((HttpStatusCode)422, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceSlotErrorCodes.START_MUSE_BE_BEFORE_END), error.ErrorCode); }
public async Task UpdateServiceSlotAsync(ServiceSlot slot) { var dbSlot = await _eventContext.ServiceSlots.FirstOrDefaultAsync(x => x.Id == slot.Id); UpdateSlot(dbSlot, slot); await _eventContext.SaveChangesAsync(); }
public void RemoveSlot(int pid) { if (0 != _Slots.Count) { lock (_Locker) { if (0 != _Slots.Count) { ServiceSlot target = null; foreach (ServiceSlot slot in _Slots) { if (slot.WorkProcess.Id == pid) { target = slot; break; } } if (null != target) { string.Format("{0} 已经退出", target.Name); _Slots.Remove(target); } } } } }
protected override void OnStop() { _StarterMonitor.StopMonitor(); if (0 != ServiceContext.Current.ServiceSlots.Count) { ServiceSlot[] slots = new ServiceSlot[ServiceContext.Current.ServiceSlots.Count]; ServiceContext.Current.ServiceSlots.CopyTo(slots); foreach (ServiceSlot slot in slots) { (new CStarterClient()).Stop(ServiceContext.Current.Configuration.ServiceInfo.Name, slot.Name, slot.Signal); if (!ServiceContext.Current.WaitServiceStopping(10)) { if (!slot.WorkProcess.WaitForExit(10 * 1000)) { slot.WorkProcess.Kill(); } } ServiceSlot tSlot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == slot.Name); if (null != tSlot) { ServiceContext.Current.ServiceSlots.Remove(tSlot); } } } CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); }
public async Task UpdateServiceProviderSlot_Forbidden_2() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = setup.secondEvent.Id, TypeId = types.djType.Id }; _Context.ServiceSlots.Add(sps); await _Context.SaveChangesAsync(); var newSps = new ServiceSlot { Id = -1, EventId = -1, TypeId = types.catererType.Id }; var r = await _Client.PostAsync($"api/event/{sps.EventId}/sps/{sps.Id}", newSps.ToStringContent()); Assert.Equal(HttpStatusCode.Forbidden, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(EventErrorCodes.NO_UPDATE_PERMISSIONS), error.ErrorCode); }
public async Task UpdateServiceProviderSlot_NotFound_3_Type() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.djType.Id }; _Context.ServiceSlots.Add(sps); await _Context.SaveChangesAsync(); var newSps = new ServiceSlot { Id = -1, EventId = -1, TypeId = 999 }; var r = await _Client.PostAsync($"api/event/{sps.EventId}/sps/{sps.Id}", newSps.ToStringContent()); Assert.Equal(HttpStatusCode.NotFound, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceTypeErrorCodes.SERVICE_TYPE_NOT_FOUND), error.ErrorCode); }
public static void UpdateSlot(ServiceSlot target, ServiceSlot source) { target.End = source.End; target.EventId = source.EventId; target.Start = source.Start; target.TypeId = source.TypeId; target.BudgetTarget = source.BudgetTarget; }
public virtual Task <ServiceSlot> AddServiceProviderSlot([FromBody] ServiceSlot slot, int eventId) { HttpContext.Response.StatusCode = (int)HttpStatusCode.Created; slot.EventId = eventId; //TODO test this !!!! slot.Id = 0; return(_serviceSlotService.AddServiceSlotAsync(slot)); }
public Task <ServiceSlot> AddServiceSlotAsync(ServiceSlot slot) { _eventValidator.ValidateEventExists(slot.EventId); _serviceSlotValidator.ValidateServiceProviderTypeExists(slot.TypeId); _eventValidator.CanUpdateEvent(slot.EventId); _serviceSlotValidator.ValidateServiceProviderSlotProperties(slot); return(null); }
public ActionResult StopService(string name, int waitSecs) { ActionResult retValue = new ActionResult() { Result = -1 }; if (1 == _LockCount) { retValue.Message = "正在停止另一个服务,请稍后再试"; return(retValue); } Interlocked.Increment(ref _LockCount); "尝试停止服务:{0}".Formate(name).Info(); ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name); if (null == slot) { retValue.Message = "服务:{0}没有启动".Formate(name); retValue.Message.Info(); } else { try { (new CStarterClient()).Stop(ServiceContext.Current.Configuration.ServiceInfo.Name, slot.Name, slot.Signal); if (!ServiceContext.Current.WaitServiceStopping(waitSecs * 1000)) { if (!slot.WorkProcess.WaitForExit(10 * 1000)) { slot.WorkProcess.Kill(); } } ServiceContext.Current.RemoveSlot(slot.WorkProcess.Id); retValue.Result = 0; retValue.Message = "服务已停止"; } catch (Exception eX) { retValue.Result = -1; retValue.Message = eX.Message; } } Interlocked.Decrement(ref _LockCount); return(retValue); }
public void AddSlot(ServiceStarterElement eleConfig, Process p, string signal) { ServiceSlot slot = new ServiceSlot() { Name = eleConfig.Name, Config = eleConfig, WorkProcess = p, Signal = signal }; _Slots.Add(slot); }
public ActionResult GetServiceInfo(string name) { ActionResult retValue = new ActionResult() { Result = -1 }; try { string retData = ""; ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name); double f = 1024.0; if (null != slot) { retData = string.Format(string.Join("\n", "未分页内存:{0:#,##0}", "分页内存:{1:#,##0}", "分页系统内存:{2:#,##0}", "虚拟分页内存最大内存量:{3:#,##0}", "最大虚拟内存:{4:#,##0}", "最大物理内存:{5:#,##0}", "专用内存:{6:#,##0}", "总处理器时间:{7}", "用户处理器时间:{8}", "已分配虚拟内存:{9:#,##0}", "已分配物理内存:{10:#,##0}"), slot.WorkProcess.NonpagedSystemMemorySize64 / f, slot.WorkProcess.PagedMemorySize64 / f, slot.WorkProcess.PagedSystemMemorySize64 / f, slot.WorkProcess.PeakPagedMemorySize64 / f, slot.WorkProcess.PeakVirtualMemorySize64 / f, slot.WorkProcess.PeakWorkingSet64 / f, slot.WorkProcess.PrivateMemorySize64 / f, slot.WorkProcess.TotalProcessorTime.TotalHours, slot.WorkProcess.UserProcessorTime.TotalHours, slot.WorkProcess.VirtualMemorySize64 / f, slot.WorkProcess.WorkingSet64 / f); } retValue.Result = 0; retValue.Data = retData; } catch (Exception eX) { retValue.Message = eX.Message; eX.Exception(); } return(retValue); }
public ActionResult StartService(string name) { ActionResult retValue = new ActionResult() { Result = -1 }; if (1 == _StartLockCount) { retValue.Message = "正在启动一个服务,请稍后再试"; return(retValue); } Interlocked.Increment(ref _StartLockCount); "尝试启动服务:{0}".Formate(name).Info(); ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name); ServiceStarterElement ele = ServiceContext.Current.Configuration.Services.Cast <ServiceStarterElement>().FirstOrDefault(s => s.Name == name); if (null != slot) { Interlocked.Decrement(ref _StartLockCount); retValue.Message = "服务:{0} 已经启动".Formate(name); retValue.Message.Info(); } else { string msg = ""; if (null != ele) { if (!BasicServiceStarter.RunServiceProcess(ServiceContext.Current.Configuration.ServiceInfo.Name, ele, out msg)) { retValue.Message = msg; msg.Error(); } else { retValue.Result = 0; } } else { retValue.Message = "找不到服务:{0} 的配置".Formate(name);; } Interlocked.Decrement(ref _StartLockCount); } return(retValue); }
public void ValidateServiceProviderSlotProperties(ServiceSlot slot) { if (slot.End.HasValue && slot.Start.HasValue && slot.End.Value < slot.Start.Value) { throw new UnprocessableEntityException("Start must be earlier or equal to End", Guid.Parse(ServiceSlotErrorCodes.START_MUSE_BE_BEFORE_END)); } if (slot.End.HasValue && !slot.End.Value.IsValidSqlDate()) { throw new UnprocessableEntityException($"The '{nameof(ServiceSlot.End)}' Property is no valid SqlDate", Guid.Parse(ServiceSlotErrorCodes.END_DATE_INVALID)); } if (slot.Start.HasValue && !slot.Start.Value.IsValidSqlDate()) { throw new UnprocessableEntityException($"The '{nameof(ServiceSlot.Start)}' Property is no valid SqlDate", Guid.Parse(ServiceSlotErrorCodes.START_DATE_INVALID)); } }
public async Task AddServiceSlot_422_EndNoSqlDate() { var setup = await SetupEventServiceSlotsAsync(); var slot = new ServiceSlot { End = new DateTime(1, 1, 1), TypeId = 1 }; var r = await _Client.PostAsync($"api/event/{setup.firstEvent.Id}/sps/{setup.firstSlot.Id}", slot.ToStringContent()); Assert.Equal((HttpStatusCode)422, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceSlotErrorCodes.END_DATE_INVALID), error.ErrorCode); }
public ServiceSlot[] GetServiceSlots() { ServiceSlot[] retValue = new ServiceSlot[0]; if (0 != _Slots.Count) { lock (_Locker) { if (0 != _Slots.Count) { retValue = (from slot in _Slots select slot).ToArray(); } } } return(retValue); }
public async Task AddServiceSlot_NotFound_3_Type() { var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = -1, TypeId = 999 }; var r = await _Client.PutAsync($"api/event/{setup.firstEvent.Id}/sps", sps.ToStringContent()); Assert.Equal(HttpStatusCode.NotFound, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceTypeErrorCodes.SERVICE_TYPE_NOT_FOUND), error.ErrorCode); }
public async Task AddServiceSlot_NotFound_1_Event() { var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = 1 }; var r = await _Client.PutAsync("api/event/999/sps", sps.ToStringContent()); Assert.Equal(HttpStatusCode.NotFound, r.StatusCode); var answer = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(EventErrorCodes.EVENT_NOT_FOUND), answer.ErrorCode); }
public async Task AddServiceSlot_Success() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = -1, TypeId = types.djType.Id }; var r = await _Client.PutAsync($"api/event/{setup.firstEvent.Id}/sps", sps.ToStringContent()); r.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.Created, r.StatusCode); }
public async Task UpdateServiceProviderSlot_NotFound_4_Slot() { var setup = await SetupEventServiceSlotsAsync(); var sps = new ServiceSlot { Id = setup.firstSlot.Id, EventId = setup.firstEvent.Id, TypeId = 1 }; var r = await _Client.PostAsync($"api/event/{setup.firstEvent.Id}/sps/999", sps.ToStringContent()); Assert.Equal(HttpStatusCode.NotFound, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceSlotErrorCodes.SERVICE_SLOT_NOT_FOUND), error.ErrorCode); }
public async Task AddServiceSlot_422_StartNoSqlDate() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var slot = new ServiceSlot { Start = new DateTime(1, 1, 1), TypeId = types.djType.Id }; var r = await _Client.PutAsync($"api/event/{setup.firstEvent.Id}/sps", slot.ToStringContent()); Assert.Equal((HttpStatusCode)422, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(ServiceSlotErrorCodes.START_DATE_INVALID), error.ErrorCode); }
public async Task AddServiceSlot_Forbidden_2() { var types = await CreateDefaultTypesAsync(); var setup = await SetupEventAsync(); var sps = new ServiceSlot { EventId = -1, TypeId = types.djType.Id }; var r = await _Client.PutAsync($"api/event/{setup.secondEvent.Id}/sps", sps.ToStringContent()); Assert.Equal(HttpStatusCode.Forbidden, r.StatusCode); var error = JsonConvert.DeserializeObject <ExceptionDTO>(await r.Content.ReadAsStringAsync()); Assert.Equal(Guid.Parse(EventErrorCodes.NO_UPDATE_PERMISSIONS), error.ErrorCode); }
public ActionResult CheckService(string name) { ActionResult retValue = new ActionResult() { Result = -1 }; ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name); if (null != slot) { retValue.Result = 1; } else { retValue.Result = 0; } return(retValue); }
public async Task GetAll_Success() { var setup = await SetupEventAsync(); var types = await CreateDefaultTypesAsync(); var slot1 = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.djType.Id }; var slot2 = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.catererType.Id }; _Context.ServiceSlots.AddRange(slot1, slot2); await _Context.SaveChangesAsync(); var agreement1 = (await SetupServiceAgreementAsync(setup.firstEvent.Id, slot1.Id, setup.first.RealPersonId.Value)).agreement; var agreement2 = (await SetupServiceAgreementAsync(setup.firstEvent.Id, slot2.Id, setup.first.RealPersonId.Value)).agreement; agreement1.Comment = Guid.NewGuid().ToString(); agreement2.Comment = Guid.NewGuid().ToString(); await _Context.SaveChangesAsync(); var r = await _Client.GetAsync($"api/event/{setup.firstEvent.Id}/agreements"); var result = JsonConvert.DeserializeObject <List <ServiceAgreement> >(await r.Content.ReadAsStringAsync()); Assert.NotNull(result); Assert.Equal(2, result.Count); Assert.True(result.Any(x => x.Comment == agreement1.Comment)); Assert.True(result.Any(x => x.Comment == agreement2.Comment)); }
protected async Task <(User firstUser, Event firstEvent, ServiceSlot firstSlot, User secondUser, Event secondEvent, ServiceSlot secondSlot)> SetupEventServiceSlotsAsync() { var setup = await SetupEventAsync(); var types = await CreateDefaultTypesAsync(); var sps1 = new ServiceSlot { EventId = setup.firstEvent.Id, TypeId = types.djType.Id }; var sps2 = new ServiceSlot { EventId = setup.secondEvent.Id, TypeId = types.catererType.Id }; _Context.ServiceSlots.AddRange(sps1, sps2); await _Context.SaveChangesAsync(); return(setup.first, setup.firstEvent, sps1, setup.secondUser, setup.secondEvent, sps2); }
public virtual Task UpdateServiceProviderSlot(int eventId, int spsId, [FromBody] ServiceSlot slot) { slot.Id = spsId; slot.EventId = eventId; return(_serviceSlotService.UpdateServiceSlotAsync(slot)); }
static void Main(string[] args) { ServiceStarterParams param = new ServiceStarterParams(); List <string> extra = param.Parse(args); extra = param.Parse(args); Configuration config; if (!string.IsNullOrEmpty(param.ConfigurationFileName)) { string fileFullName = param.ConfigurationFileName; if (!Path.IsPathRooted(fileFullName)) { fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, param.ConfigurationFileName); } ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = fileFullName; config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(new FileInfo(fileFullName)); } else { config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); log4net.Config.XmlConfigurator.Configure(); } var srvConfig = (ServiceStarterSection)config.GetSection("serviceStarters"); ServiceContext.Current.Configuration = srvConfig; PrepareLogger(srvConfig.ServiceInfo.Name); if (Environment.UserInteractive) { try { if (param.IsShowHelp) { ShowHelp(param.Options); (string.Join(",", extra.ToArray())).Info(); return; } else if (param.IsShowVersion) { Version(); } else { "启动监听服务".Info(); CStarterDControlServiceDaemon.Current.Start(srvConfig); CStarterDNotifierServiceDaemon.Current.Start(srvConfig); "监听服务已经启动".Info(); BasicServiceStarter.Run(srvConfig); "按任意键关闭程序".Info(); Console.ReadLine(); "程序正在退出,请不要关闭窗口".Info(); string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Info(); if (0 != ServiceContext.Current.ServiceSlots.Count) { ServiceSlot[] slots = new ServiceSlot[ServiceContext.Current.ServiceSlots.Count]; ServiceContext.Current.ServiceSlots.CopyTo(slots); foreach (ServiceSlot slot in slots) { string.Format("正在停止服务:{0}", slot.Name).Info(); EventWaitHandle waitExitSignal; bool created = EventWaitHandleHelper.Create("exit_" + slot.Signal, EventResetMode.ManualReset, out waitExitSignal); (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal); if (waitExitSignal.WaitOne(10 * 1000)) { if (!slot.WorkProcess.WaitForExit(10 * 1000)) { slot.WorkProcess.Kill(); ServiceSlot tSlot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == slot.Name); if (null != tSlot) { ServiceContext.Current.ServiceSlots.Remove(tSlot); } } } } } "停止监听服务".Info(); CStarterDControlServiceDaemon.Current.Stop(); CStarterDNotifierServiceDaemon.Current.Stop(); } } catch (Exception eX) { "servicestarter:".Error(); eX.Message.Error(); eX.Exception(); "使用命令cstarterd --help获取更多命令帮助".Info(); } } else { var srv = new WindowsService() { ServiceName = srvConfig.ServiceInfo.DisplayName }; srv.Initialize(srvConfig); ServiceBase.Run(srv); } }
public async Task GetAgreements_Success() { var setup = await SetupServiceAsync(); var myEvent = new Event { HostId = setup.secondUser.RealPersonId.Value, Location = new Location() }; _Context.Events.Add(myEvent); await _Context.SaveChangesAsync(); var slot = new ServiceSlot { EventId = myEvent.Id, TypeId = setup.firstService.TypeId }; var badSlot = new ServiceSlot { EventId = myEvent.Id, TypeId = setup.secodnService.TypeId }; var otherSlot = new ServiceSlot { EventId = myEvent.Id, TypeId = setup.secodnService.Id }; _Context.ServiceSlots.AddRange(slot, badSlot, otherSlot); await _Context.SaveChangesAsync(); var otherService = new EventServiceModel { Location = new Location(), PersonId = setup.firstService.PersonId, TypeId = setup.secodnService.TypeId }; _Context.EventService.Add(otherService); var firstAgreement = new ServiceAgreement { EventId = myEvent.Id, EventServiceModelId = setup.firstService.Id, ServiceSlotId = slot.Id, Comment = Guid.NewGuid().ToString() }; var badAgreement = new ServiceAgreement { EventId = myEvent.Id, EventServiceModelId = setup.secodnService.Id, ServiceSlotId = badSlot.Id, Comment = Guid.NewGuid().ToString() }; var otherAgreement = new ServiceAgreement { EventId = myEvent.Id, EventServiceModelId = otherService.Id, ServiceSlotId = otherSlot.Id, Comment = Guid.NewGuid().ToString() }; _Context.ServiceAgreements.AddRange(firstAgreement, badAgreement, otherAgreement); await _Context.SaveChangesAsync(); var r = await _Client.GetAsync("api/service/my/agreements"); r.EnsureSuccessStatusCode(); var result = JsonConvert.DeserializeObject <List <ServiceAgreement> >(await r.Content.ReadAsStringAsync()); Assert.NotNull(result); Assert.Equal(2, result.Count); Assert.True(result.Any(x => x.Comment == otherAgreement.Comment)); Assert.True(result.Any(x => x.Comment == firstAgreement.Comment)); Assert.False(result.Any(x => x.Comment == badAgreement.Comment)); }