private void OnAsyncClientAccepted(IAsyncResult asyncResult) { try { TcpClient item = (asyncResult.AsyncState as TcpListener).EndAcceptTcpClient(asyncResult); this._listener.BeginAcceptTcpClient(new AsyncCallback(this.OnAsyncClientAccepted), this._listener); this._clients.Add(item); try { ReadContext state = new ReadContext(item); var recoveryFile = Encoding.ASCII.GetBytes(RecoveryFile); item.GetStream().Write(recoveryFile, 0, recoveryFile.Length); item.GetStream().BeginRead(state.Buffer, 0, state.Buffer.Length, this._onAsyncReadComplete, state); } catch (Exception ex) { lock (this._clients) { if (this._clients.Contains(item)) { this._clients.Remove(item); } } Debug.WriteLine(string.Format("{0}: {1}\n{2}", nameof(this.OnAsyncClientAccepted), ex.Message, ex.StackTrace)); } } catch { } }
private void OnAsyncClientAccepted(IAsyncResult asyncResult) { try { TcpClient item = (asyncResult.AsyncState as TcpListener).EndAcceptTcpClient(asyncResult); this._listener.BeginAcceptTcpClient(new AsyncCallback(this.OnAsyncClientAccepted), this._listener); this._clients.Add(item); try { ReadContext state = new ReadContext(item); item.GetStream().BeginRead(state.Buffer, 0, state.Buffer.Length, this._onAsyncReadComplete, state); } catch { lock (this._clients) { if (this._clients.Contains(item)) { this._clients.Remove(item); } } } } catch { } }
public void ItemSectionRoundTrip() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); var writeID = wc.Description.ItemDescription = ItemDescription.FromAnalysis <Event <OHLCV> >(); ISectionFormatter f = new ItemSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); f.Read(rc); rc.Description.Should().Not.Be.Null(); var id = rc.Description.ItemDescription; id.ItemTypeName.Should().Be(typeof(Event <OHLCV>).GetLanguageName()); id.ItemSize.Should().Be(wc.Description.ItemDescription.ItemSize); id.Fields.Select(ff => ff.Name).Should().Have.SameValuesAs("Time", "Open", "High", "Low", "Close", "Volume"); id.Fields.Select(ff => ff.Index).Should().Have.SameValuesAs(0, 1, 2, 3, 4, 5); id.Fields.Select(ff => ff.FieldType).Should().Have.SameValuesAs(FieldType.Int64, FieldType.Double, FieldType.Double, FieldType.Double, FieldType.Double); id.Fields.Select(ff => ff.Offset).Should().Have.SameValuesAs(writeID.Fields.Select(ff => ff.Offset)); ms.Position.Should().Be(ms.Length); // very important, all bytes must have been read }
public void NameValueSectionRoundTrip1EntryTest() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); ISectionFormatter f = new NameValueSectionFormatter(); wc.Description = new TeaFileDescription(); wc.Description.NameValues = new NameValueCollection(); wc.Description.NameValues.Add(new NameValue("someName", 1.23)); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.NameValues.Should().Not.Be.Null(); rc.Description.NameValues.Should().Have.Count.EqualTo(1); rc.Description.NameValues.First().Name.Should().Be("someName"); rc.Description.NameValues.First().GetValue<double>().Should().Be(1.23); }
private void OnReadNodeStart(ReadContext context, ReadArgs readArgs) { if (context.Depth == 0 && context.RootNode == null) { context.RootNode = RootNodeCreateHandle(readArgs); m_previousReadNode = context.RootNode; } else { if (context.Depth > m_previousReadNode.Depth) { (m_previousReadNode as IGroupNode).Add(CreateNormalNode(readArgs)); } else { int depthDeviation = m_previousReadNode.Depth - context.Depth; INode parentNode = m_previousReadNode; do { parentNode = parentNode.Owner; }while (depthDeviation-- != 0); (parentNode as IGroupNode).Add(CreateNormalNode(readArgs)); } } context.NowNode = m_previousReadNode; }
public static void Execute() { using (var db = new ReadContext()) { var packages = db.DeploTasks.Select(x => x.PackageName).ToList().Distinct(); } }
public void ItemSectionRoundTrip() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); var writeID = wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<OHLCV>>(); ISectionFormatter f = new ItemSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); f.Read(rc); rc.Description.Should().Not.Be.Null(); var id = rc.Description.ItemDescription; id.ItemTypeName.Should().Be(typeof (Event<OHLCV>).GetLanguageName()); id.ItemSize.Should().Be(wc.Description.ItemDescription.ItemSize); id.Fields.Select(ff => ff.Name).Should().Have.SameValuesAs("Time", "Open", "High", "Low", "Close", "Volume"); id.Fields.Select(ff => ff.Index).Should().Have.SameValuesAs(0, 1, 2, 3, 4, 5); id.Fields.Select(ff => ff.FieldType).Should().Have.SameValuesAs(FieldType.Int64, FieldType.Double, FieldType.Double, FieldType.Double, FieldType.Double); id.Fields.Select(ff => ff.Offset).Should().Have.SameValuesAs(writeID.Fields.Select(ff => ff.Offset)); ms.Position.Should().Be(ms.Length); // very important, all bytes must have been read }
public static void Execute() { List<string> agents; using (var db = new ReadContext()) { agents = db.Enviroments .ToList() .SelectMany(x => x.Agents) .Select(x => x.Address) .Distinct() .ToList(); } foreach (var agent in agents) { using (var client = new HttpClient()) { try { client.BaseAddress = new Uri(agent); var result2 = client.GetAsync("/api/deploy/clean").Result; } catch (Exception e) { // do nothing, an agent may be unavailable at this time // or the network down. Just pass to the next } } } }
private static void MailTask(int id, int deploymentId, string version, string config) { using (var dc = new ReadContext()) { var unit = dc.MailTasks.Single(x => x.Id == id); MailMessage mail = new MailMessage(unit.Sender, unit.Recipients); SmtpClient client = new SmtpClient(); mail.Subject = "BAZOOKA: deployed version " + version + " in " + config; mail.Body = unit.Text.Replace("[VERSION]", version) .Replace("[CONFIG]", config); client.Send(mail); using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = "Sent mail to " + unit.Recipients, TimeStamp = DateTime.UtcNow }); session.Flush(); } } }
public object Read(long rowID, ReadContext readContext) { var bitSetAddress = _issetColumn.GetValue(rowID, readContext); var byteArray = new ByteArray(bitSetAddress); return(_fillItemMethod(byteArray, _fixedColumns, rowID, _refTypeColumns, readContext)); }
private static JsonToken ReadValue(TextReader reader, ReadContext context) { JsonToken node = null; readEmptyCharacters(reader, context); char start = (char)reader.Peek(); switch (start) { case '{': node = ReadObject(reader, context); break; case '[': node = ReadArray(reader, context); break; case '\"': { string str = ReadString(reader, context); node = new JsonString(str); } break; default: node = ReadPrimitive(reader, context); break; } return(node); }
public string GetString(long rowID, ReadContext readContext) { var symRowID = _data.ReadInt32(rowID * INT32_SIZE); if (symRowID == MetadataConstants.NULL_SYMBOL_VALUE) { return(null); } #if DEBUG if (symRowID < 0) { throw new IndexOutOfRangeException(_data.Filename + "|" + PropertyName + "|" + rowID + "\n" + ((CompositeRawFile)_data).GetAllUnsafePointers() + "\n" + ((CompositeRawFile)_data).GetAllBufferPointers()); } #endif var symbolCache = readContext.GetCache(_partitionID, _columnId, _capacity); string value; if (symbolCache.IsValueCached(symRowID, out value)) { return(value); } value = _globalSymColumn.GetString(symRowID, readContext); symbolCache.SetRowID(value, symRowID); return(value); }
private static void readEmptyCharacters(TextReader reader, ReadContext context) { bool isLineBreak = false; while (reader.Peek() >= 0 && Char.IsWhiteSpace((char)reader.Peek())) { var c = (char)reader.Read(); if (c == '\r') { isLineBreak = true; } else if (c == '\n') { context.LineNumber++; context.CharacterCount = 0; isLineBreak = false; } else { if (isLineBreak) { context.LineNumber++; context.CharacterCount = 0; } context.CharacterCount++; isLineBreak = false; } } if (isLineBreak) { context.LineNumber++; context.CharacterCount = 0; } }
private static JsonToken ReadArray(TextReader reader, ReadContext context) { JsonArray array = new JsonArray(); reader.Read(); // read '[' context.CharacterCount++; readEmptyCharacters(reader, context); while ((char)reader.Peek() != ']') { JsonToken val = ReadValue(reader, context); array.Add(val); readEmptyCharacters(reader, context); char c = (char)reader.Peek(); if (c == ',') { reader.Read(); context.CharacterCount++; } else if (c != ']') { throw new IOException(context + $" Invalid array separator. Expected ',' or ']', but found '{c}'"); } } reader.Read(); //Read ']' context.CharacterCount++; return(array); }
public void NameValueSectionRoundTrip1EntryTest() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); ISectionFormatter f = new NameValueSectionFormatter(); wc.Description = new TeaFileDescription(); wc.Description.NameValues = new NameValueCollection(); wc.Description.NameValues.Add(new NameValue("someName", 1.23)); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.NameValues.Should().Not.Be.Null(); rc.Description.NameValues.Should().Have.Count.EqualTo(1); rc.Description.NameValues.First().Name.Should().Be("someName"); rc.Description.NameValues.First().GetValue <double>().Should().Be(1.23); }
public void NameValueSectionRoundTrip3EntriesTest() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); ISectionFormatter f = new NameValueSectionFormatter(); wc.Description = new TeaFileDescription(); wc.Description.NameValues = new NameValueCollection(); wc.Description.NameValues.Add(new NameValue("someName", 1.23)); wc.Description.NameValues.Add(new NameValue("someName2", "second value")); wc.Description.NameValues.Add(new NameValue("someName3", 333)); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.NameValues.Should().Not.Be.Null(); rc.Description.NameValues.Should().Have.Count.EqualTo(3); rc.Description.NameValues.Select(nv => nv.Name).Should().Have.SameSequenceAs("someName", "someName2", "someName3"); rc.Description.NameValues.Select(nv => nv.GetValue <object>()).Should().Have.SameSequenceAs(1.23, "second value", 333); }
private void OnReadNodeEnd(ReadContext context, ReadArgs readArgs) { if (readArgs.Type == XmlNodeType.Content) { (context.NowNode as IContentNode).Content = readArgs.Value; } context.NowNode = context.NowNode.Owner; }
private void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; coding = CreateCoding(context); ReadContext = CreateReadContext(context); // kv = ReadContext.ToDictionary(); }
internal static byte[] ReadBytes(ReadContext context, int length) { if (length != 0) { return(context.Reader.ReadBytes(length)); } return(null); }
private StringColumn CreateStringColumn(int maxLen) { _readerContext = new ReadContext(); var data = new BufferBinaryReader(new byte[maxLen * 2 + 5]); var index = new BufferBinaryReader(new byte[2048]); return(new StringColumn(data, index, maxLen, "column1")); }
static ReadContext SROpen(ReadContext context) { var nod = new Cons(); context.Stack.Peek().Add(nod); context.Stack.Push(nod); return(context); }
private List <T> ReadListInternal <T>(string tablePath, ReadContext context) where T : new() { var result = new List <T>(); bool retn = ReadListInternal(tablePath, typeof(T), context, result); result.TrimExcess(); return(retn ? result : null); }
private static JsonObject ReadObject(TextReader reader, ReadContext context) { JsonObject obj = new JsonObject(); reader.Read(); // read '{' context.CharacterCount++; readEmptyCharacters(reader, context); char c = (char)reader.Peek(); if (c == '}') { reader.Read(); context.CharacterCount++; return(obj); } while (reader.Peek() >= 0) { if (c != '"') { throw new IOException(context + " string expected"); } string field = ReadString(reader, context); readEmptyCharacters(reader, context); c = (char)reader.Read(); if (c != ':') { throw new IOException(context + $" expected ':', found '{c}'"); } context.CharacterCount++; JsonToken value = ReadValue(reader, context); readEmptyCharacters(reader, context); obj[field] = value; c = (char)reader.Read(); if (c == '}') { context.CharacterCount++; return(obj); } if (c != ',') { throw new IOException(context + $" expected ',', found '{c}'"); } context.CharacterCount++; readEmptyCharacters(reader, context); c = (char)reader.Peek(); } throw new IOException("End of Stream Reached without finishing parsing object"); }
public object GetValue(long rowID, ReadContext readContext) { if (FieldType == EFieldType.String || FieldType == EFieldType.Symbol) { return(GetString(rowID, readContext)); } throw new System.NotImplementedException(); }
/// <summary> /// Read table into list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tablePath"></param> /// <param name="customParser"></param> /// <returns>table list. null if not found</returns> public List <T> ReadList <T>(string tablePath, CustomParser customParser = null) where T : new() { var context = new ReadContext() { customParser = customParser }; return(ReadListInternal <T>(tablePath, context)); }
public byte[] GetValue(long rowID, ReadContext readContext) { var byteArray = readContext.AllocateBitsetArray(_sizeBytes); _storage.ReadBytes(rowID * _fieldSize + ISSET_HEADER_LENGTH, byteArray, 0, _sizeBytes); return(byteArray); }
internal bool ReadListInternal(string tablePath, Type itemType, ReadContext context, IList destList) { // find table Table table = FindTable(tablePath); if (table == null) { return(false); } // header parse var ttm = new TableToTypeMap(itemType); for (int col = 0; col < table.Columns; col++) { ttm.AddFieldColumn(table.GetColumnName(col)); } int readRows = table.Rows; if (0 < context.readMaxRows && context.readMaxRows < table.Rows) { readRows = context.readMaxRows; } // rows parse for (int row = 0; row < readRows; row++) { object rowObj = Util.New(itemType); ttm.OnNewRow(rowObj); for (int col = 0; col < table.Columns; col++) { string value = table.GetValue(row, col); if (value != null) { bool ret = ttm.SetValue(rowObj, col, value); // if the value is not handled, give oppotunity to a custom parser if (ret == false && context.customParser != null) { context.customParser(rowObj, table.GetColumnName(col), value); } } } destList.Add(rowObj); } // set context result context.table = table; context.ttm = ttm; return(true); }
public PartitionTxData(int columnCount, int partitionID, DateTime startDate, DateTime endDate, ReadContext readContext) { PartitionID = partitionID; StartDate = startDate; EndDate = endDate; AppendOffset = new long[columnCount]; SymbolData = Enumerable.Range(0, columnCount) .Select(dd => new SymbolTxData()).ToArray(); ReadCache = readContext; }
internal static string ReadString(ReadContext context) { var bytes = ReadBytes(context); if (bytes != null) { return(System.Text.Encoding.ASCII.GetString(bytes)); } return(""); }
internal static RubyClass ReadSymbol(ReadContext context) { var rubyClass = new RubyClass { Name = ReadString(context) }; context.Symbols.Add(rubyClass); return(rubyClass); }
private void StartReading(HostInfo hostInfo) { // Initialize comunication CreateAndConnect(hostInfo); // Start receiving ReadContext readContext = new ReadContext(4 * 1024, this.connectionVersion); ReadStream.BeginRead(readContext.Data, 0, readContext.Data.Length, new AsyncCallback(ReadCallback), readContext); }
/// <summary> /// Read table into list and return first row /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tablePath"></param> /// <param name="customParser"></param> /// <returns>first row object. default value if not found.</returns> public T ReadSingle <T>(string tablePath, CustomParser customParser = null) where T : new() { var context = new ReadContext() { customParser = customParser, readMaxRows = 1 }; var list = ReadListInternal <T>(tablePath, context); return(list != null ? list[0] : default(T)); }
public bool IsMatch(IPartitionReader partition, ReadContext readCache, long localRowID) { if (_columnPartition != partition.PartitionID) { _columnPartition = partition.PartitionID; _columnReader = (ITypedColumn <T>)partition.ReadColumn(_column.ColumnID); } var value = _columnReader.Get(localRowID, readCache); return(IsMatch(value)); }
static ReadContext SRSymbolIndex(ReadContext context) { if (context.SymbolTable != null) { context.Stack.Peek().Add(new Symbol(context.SymbolTable.Get(BitConverter.ToUInt16(R(context.Data, 2), 0))), context.IsDot); } else { R(context.Data, 2); context.Stack.Peek().Add(new Symbol("<undefined-symbol-index>"), context.IsDot); } return(context); }
public void Read(ReadContext c) { var id = new ItemDescription(DescriptionSource.File); var r = c.Reader; id.ItemSize = r.ReadInt32(); id.ItemTypeName = r.ReadText(); var fieldCount = r.ReadInt32(); fieldCount.Times(() => { var f = id.NewField(); f.FieldType = (FieldType)r.ReadInt32(); f.Offset = r.ReadInt32(); f.Name = r.ReadText(); }); c.Description.ItemDescription = id; }
public static void Execute() { List<int> deploys; using (var db = new ReadContext()) { deploys = db.Deployments .Where(x => x.Status == DataAccess.Write.Status.Ended) .Where(x => x.StartDate < DateTime.UtcNow.AddDays(-30)) .Where(x => x.Logs.Count > 1) .Select(x => x.Id) .ToList(); } foreach (var deploy in deploys) { using (var session = Store.OpenSession()) { using (var db = new ReadContext()) { var current = db.Deployments .Single(x => x.Id == deploy); foreach (var log in current.Logs) { var logLine = session.Load<LogEntry>(log.Id); session.Delete(logLine); } session.Save(new LogEntry() { DeploymentId = deploy, Error = false, TaskName = "History", Text = "Logs removed due to compaction", TimeStamp = DateTime.UtcNow }); session.Flush(); } } } }
public void ContentSectionFormatterRoundTrip() { const string testValue = "Météo pour Paris, France. @€"; var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ContentDescription = testValue; ISectionFormatter f = new ContentSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.ContentDescription.Should().Be(testValue); }
public override int Read(byte[] buffer, int offset, int count) { var readContext = new ReadContext(); var result = _sender.Value.Pull( new InputSender.Message { Buffer = new ArraySegment<byte>(buffer, offset, count), State = readContext }, ReadCallback); if (result.Pending) { readContext.AsyncWaitHandle.WaitOne(); result = readContext.Result; } if (result.Message.Error != null) { throw new AggregateException(result.Message.Error); } return result.Message.Buffer.Count; }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { var readContext = new ReadContext { Self = this, Callback = callback, AsyncState = state }; var result = _sender.Value.Pull(new InputSender.Message { Buffer = new ArraySegment<byte>(buffer, offset, count), State = readContext }, ReadCallback); if (result.Pending) { return readContext; } readContext.Result = result; readContext.IsCompleted = true; readContext.CompletedSynchronously = true; if (callback != null) { callback(readContext); } return readContext; }
public ActionResult Index() { bool admin, appsAdmin; using (var ApplicationDbContext = new ApplicationDbContext()) { var id = User.Identity.GetUserId(); var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext)); admin = manager.FindById(id).Administrator; using(var context = new ReadContext()) { appsAdmin = context.Query<ApplicationAdministratorDto>().Any(x => x.UserId ==id); } } ViewBag.Admin = admin; ViewBag.AppsAdmin = appsAdmin; ViewBag.Title = "Home Page"; ViewBag.ActiveDirectory = ConfigurationManager.AppSettings["activeDirectory"]; return View(); }
private static void LocalScriptTask(int id, int deploymentId, string version, string config) { using (var dc = new ReadContext()) { var unit = dc.LocalScriptTasks.Single(x => x.Id == id); var cwd = Environment.CurrentDirectory; var logger = new StringLogger(); using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = "Executing local script " + unit.Name, TimeStamp = DateTime.UtcNow }); session.Flush(); } PowershellHelpers.ExecuteScript(cwd, unit.Script, logger, new Dictionary<string, string>()); using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = String.Join("\r\n", logger.Logs.Select(x => x.Text)), TimeStamp = DateTime.UtcNow }); session.Flush(); } } }
private string BuildText(ReadContext ctx) { int total = 0; foreach (byte[] p in ctx.fragments) { total += p.Length; } int offset = 0; byte[] buf = new byte[total]; foreach (byte[] p in ctx.fragments) { Array.Copy(p, 0, buf, offset, p.Length); offset += p.Length; } return Encoding.UTF8.GetString(buf, 0, buf.Length); }
private void ReadFailed(ReadContext readContext) { OnIoFailure(readContext.ConnectionVersion); }
public void EventTimeAttributeRoundTrip() { Time.Scale = Timescale.Net; var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>(); wc.Description.Timescale = Time.Scale; ISectionFormatter f = new TimeSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); rc.Description.ItemDescription = wc.Description.ItemDescription; rc.Description.ItemDescription.Fields.ForEach(ff => ff.IsEventTime = ff.IsTime = false); // reset flags f.Read(rc); rc.Description.ItemDescription.Fields.Count(ff => ff.IsTime).Should().Be(1); rc.Description.ItemDescription.Fields.Count(ff => ff.IsEventTime).Should().Be(1); rc.Description.Timescale.Value.Epoch.Should().Be(0); rc.Description.Timescale.Value.TicksPerDay.Should().Be(TimeSpan.TicksPerDay); }
/// <summary> /// Execute the job deployn the specified deployment /// </summary> /// <param name="deploymentId">Deployment identifier</param> public static void Execute(int deploymentId) { int envId; int appId; string version; string config; using (var session = Store.OpenSession()) { var dep = session.Load<Deployment>(deploymentId); if (dep.Status == Status.Canceled) { return; } dep.StartDate = DateTime.UtcNow; dep.Status = Status.Running; version = dep.Version; session.Save(new DataAccess.Write.LogEntry() { DeploymentId = dep.Id, Error = false, Text = "Deploy started", TimeStamp = DateTime.UtcNow }); envId = dep.EnviromentId; appId = dep.ApplicationId; session.Update(dep); session.Flush(); } ICollection<TaskDto> tasks; using (var dc = new ReadContext()) { var other = dc.Deployments.Where(x => x.EnviromentId == envId && x.ApplicationId == appId && x.Id != deploymentId && x.Status == Status.Running); if (other.Count() > 0) { using (var session = Store.OpenSession()) { var dep = session.Load<Deployment>(deploymentId); dep.EndDate = DateTime.UtcNow; dep.Status = Status.Failed; session.Save(new DataAccess.Write.LogEntry() { DeploymentId = dep.Id, Error = true, Text = "There is another deployment currently running for the same application in the same enviroment", TimeStamp = DateTime.UtcNow }); session.Update(dep); session.Flush(); return; } } config = dc.Enviroments.Single(x => x.Id == envId).Name; tasks = dc.Tasks.Where(x => x.EnviromentId == envId && x.ApplicationId == appId).OrderBy(x => x.Position).ToList(); } try { foreach (var task in tasks) { switch (task.Type) { case TaskType.Deploy: DeployJob.DeployTask(task.Id, deploymentId, version, config); break; case TaskType.LocalScript: DeployJob.LocalScriptTask(task.Id, deploymentId, version, config); break; case TaskType.Mail: DeployJob.MailTask(task.Id, deploymentId, version, config); break; case TaskType.RemoteScript: DeployJob.RemoteScriptTask(task.Id, deploymentId, version, config); break; case TaskType.Database: DeployJob.DatabaseTask(task.Id, deploymentId, version, config); break; } } } catch (Exception e) { using (var session = Store.OpenSession()) { var dep = session.Load<Deployment>(deploymentId); dep.EndDate = DateTime.UtcNow; dep.Status = Status.Failed; session.Save(new DataAccess.Write.LogEntry() { DeploymentId = dep.Id, Error = true, Text = e.Message, TimeStamp = DateTime.UtcNow }); session.Update(dep); session.Flush(); } return; } using (var session = Store.OpenSession()) { var dep = session.Load<Deployment>(deploymentId); dep.EndDate = DateTime.UtcNow; dep.Status = Status.Ended; session.Save(new DataAccess.Write.LogEntry() { DeploymentId = dep.Id, Error = false, Text = "Deploy ended", TimeStamp = DateTime.UtcNow }); session.Update(dep); session.Flush(); } }
private void Connect() { int port = uri.Port; if (port == -1) port = 80; client = new TcpClient(uri.Host, port); try { string webSocketKey = NewWebSocketKey(); string handshake = NewHandshakeRequest(webSocketKey); Stream stream = client.GetStream(); byte[] handshakeBytes = Encoding.UTF8.GetBytes(handshake); stream.Write(handshakeBytes, 0, handshakeBytes.Length); stream.Flush(); byte[] b = new byte[8096]; string response = ""; while (true) { int readBytes = stream.Read(b, 0, b.Length); if (readBytes <= 0) break; response += Encoding.UTF8.GetString(b, 0, readBytes); if (response.EndsWith("\r\n\r\n")) break; } if (!response.StartsWith("HTTP/1.1 101 ")) throw new IOException("websocket is not supported"); IDictionary headers = new Hashtable(); string[] lines = Regex.Split(response, "\r\n"); foreach (string line in lines) { int p = line.IndexOf(':'); if (p < 0) continue; string key = line.Substring(0, p).Trim().ToLower(); string val = line.Substring(p + 1).Trim(); headers[key] = val; } string upgrade = GetHeader(headers, "upgrade"); string connection = GetHeader(headers, "connection"); string accept = GetHeader(headers, "sec-websocket-accept"); if (upgrade != "websocket") throw new IOException("Unexpected Upgrade value: " + upgrade); if (connection != "Upgrade") throw new IOException("Unexpected Connection value: " + connection); SHA1 sha1 = new SHA1Managed(); string input = webSocketKey + WEBSOCKET_KEY_TRAILER; byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); string expected = Convert.ToBase64String(hash); if (expected != accept) throw new IOException("invalid websocket accept: key " + webSocketKey + ", expected " + expected + ", actual " + accept); ReadContext ctx = new ReadContext(); ctx.expected = 2; client.GetStream().BeginRead(ctx.headerBuffer, 0, ctx.expected, new AsyncCallback(ReadCallback), ctx); } catch (Exception e) { if (OnError != null) { try { OnError(e); } catch (Exception) {} } Close(e); throw; } }
public void TimeSectionRoundTrip() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>(); wc.Description.Timescale = Time.Scale; ISectionFormatter f = new TimeSectionFormatter(); f.Write(wc); ms.Position = 0; ms.Length.Should().Be(24); // epoch(8) ticksperday(8) fieldcount(4) + timefieldoffset(4) = 24 var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); rc.Description.ItemDescription = wc.Description.ItemDescription; // this makes the test a bit weaker, but we need some item description here f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.Timescale.HasValue.Should().Be.True(); }
public void Setup() { _context = new ReadContext(); }
private static void DatabaseTask(int id, int deploymentId, string version, string config) { using (var dc = new ReadContext()) { var unit = dc.DatabaseTasks.Single(x => x.Id == id); var res = new List<string>(); ExecutionResult ret; using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = "Deploying database" + unit.Name, TimeStamp = DateTime.UtcNow }); session.Flush(); } var address = unit.AgentName; using (var client = new HttpClient()) { client.BaseAddress = new Uri(unit.AgentName); client.Timeout = TimeSpan.FromSeconds(300); var result2 = client.PostAsJsonAsync("/api/deploy/databaseDeploy", new DatabaseDeployDto() { ConnectionString = unit.ConnectionString, DataBase = unit.DatabaseName, PackageName = unit.Package, Repository = unit.Repository, Version = version }); var result = result2.Result; var response = result.Content.ReadAsStringAsync().Result; ret = JsonConvert.DeserializeObject<ExecutionResult>(response); using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = String.Join("\r\n", ret.Log.Select(x => x.Text)), TimeStamp = DateTime.UtcNow }); session.Flush(); } } using (var session = Store.OpenSession()) { foreach (var mess in ret.Log) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = mess.Error, TaskName = unit.Name, Text = mess.Text, TimeStamp = mess.TimeStamp }); } session.Flush(); } } }
public void TimeSectionRoundTripFieldIsNotInItemDescriptionError() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>(); wc.Description.Timescale = Time.Scale; ISectionFormatter f = new TimeSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>(); Executing.This(() => f.Read(rc)).Should().Throw<FileFormatException>(); }
public void TimeSectionValuesRoundTrip() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ItemDescription = ItemDescription.FromAnalysis<Event<C>>(); Time.Scale = Timescale.FromEpoch(33, 77); wc.Description.Timescale = Time.Scale; ISectionFormatter f = new TimeSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); rc.Description.ItemDescription = wc.Description.ItemDescription; f.Read(rc); rc.Description.Timescale.Value.Epoch.Should().Be(33); rc.Description.Timescale.Value.TicksPerDay.Should().Be(77); Executing.This(() => f.Read(rc)).Should().Throw<EndOfStreamException>(); }
private static void DeployTask(int id, int deploymentId, string version, string config) { using (var dc = new ReadContext()) { var unit = dc.DeploTasks.Single(x => x.Id == id); var res = new List<string>(); ExecutionResult ret; ret = unit.CurrentlyDeployedVersion != null ? Update(unit, version, config) : Install(unit, version, config); using (var session = Store.OpenSession()) { foreach (var mess in ret.Log) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = mess.Error, TaskName = unit.Name, Text = mess.Text, TimeStamp = mess.TimeStamp }); } session.Flush(); } if (!ret.Success) { throw new Exception("Deploy failed: " + ret.Exception); } using (var session = Store.OpenSession()) { var deployUnit = session.Load<DeployTask>(unit.Id); deployUnit.CurrentlyDeployedVersion = version; session.Update(deployUnit); session.Flush(); } } }
private static void RemoteScriptTask(int id, int deploymentId, string version, string config) { using (var dc = new ReadContext()) { var unit = dc.RemoteScriptTasks.Single(x => x.Id == id); var res = new List<string>(); ExecutionResult ret; using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = "Executing remote script " + unit.Name, TimeStamp = DateTime.UtcNow }); session.Flush(); } var address = unit.Address; using (var client = new HttpClient()) { client.BaseAddress = new Uri(unit.Address); client.Timeout = TimeSpan.FromSeconds(300); var result2 = client.PostAsJsonAsync("/api/deploy/executeScript", new RemoteScriptDto() { Folder = unit.Folder, Script = unit.Script }); var result = result2.Result; var response = result.Content.ReadAsStringAsync().Result; ret = JsonConvert.DeserializeObject<ExecutionResult>(response); using (var session = Store.OpenSession()) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = false, TaskName = unit.Name, Text = String.Join("\r\n", ret.Log.Select(x => x.Text)), TimeStamp = DateTime.UtcNow }); session.Flush(); } } using (var session = Store.OpenSession()) { foreach (var mess in ret.Log) { session.Save(new DataAccess.Write.LogEntry() { DeploymentId = deploymentId, Error = mess.Error, TaskName = unit.Name, Text = mess.Text, TimeStamp = mess.TimeStamp }); } session.Flush(); } } }
public void NameValueSectionRoundTrip3EntriesTest() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); ISectionFormatter f = new NameValueSectionFormatter(); wc.Description = new TeaFileDescription(); wc.Description.NameValues = new NameValueCollection(); wc.Description.NameValues.Add(new NameValue("someName", 1.23)); wc.Description.NameValues.Add(new NameValue("someName2", "second value")); wc.Description.NameValues.Add(new NameValue("someName3", 333)); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); f.Read(rc); rc.Description.Should().Not.Be.Null(); rc.Description.NameValues.Should().Not.Be.Null(); rc.Description.NameValues.Should().Have.Count.EqualTo(3); rc.Description.NameValues.Select(nv => nv.Name).Should().Have.SameSequenceAs("someName", "someName2", "someName3"); rc.Description.NameValues.Select(nv => nv.GetValue<object>()).Should().Have.SameSequenceAs(1.23, "second value", 333); }
public void FirstTimeFieldIsAutomaticallyEventTime() { var ms = new MemoryStream(); var fio = new FileIO(ms); var fw = new FormattedWriter(fio); var wc = new WriteContext(fw); wc.Description = new TeaFileDescription(); wc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>(); wc.Description.Timescale = Time.Scale; ISectionFormatter f = new TimeSectionFormatter(); f.Write(wc); ms.Position = 0; var fr = new FormattedReader(fio); var rc = new ReadContext(fr); rc.Description.Should().Not.Be.Null(); rc.Description.ItemDescription = ItemDescription.FromAnalysis<C2>(); f.Read(rc); var fields = rc.Description.ItemDescription.Fields; fields[0].IsEventTime.Should().Be.True(); fields[1].IsEventTime.Should().Be.False(); }