public void ProcessRequest (HttpContext context) { var lane_id = context.Request ["lane_id"].ToUInt32 (); if (lane_id == null) { context.Response.StatusCode = 400; context.Response.ContentType = "text/plain"; context.Response.Write ("lane_id GET parameter is required."); return; } using (var db = new DB ()) using (var cmd = db.CreateCommand ()) { DB.CreateParameter (cmd, "nlimit", LIMIT); DB.CreateParameter (cmd, "lane_id", (int) lane_id.Value); cmd.CommandText = @"SELECT 1 FROM lane WHERE id = @lane_id"; if (cmd.ExecuteScalar () == null) { context.Response.StatusCode = 404; context.Response.ContentType = "text/plain"; context.Response.Write ("No such lane."); return; } cmd.CommandText = @" SELECT rw.id, lane, host.host, revision, rw.state, rw.createdtime, rw.assignedtime, rw.startedtime, rw.endtime, workhost.host FROM revisionwork AS rw INNER JOIN lane ON lane.id = rw.lane_id INNER JOIN host ON host.id = rw.host_id LEFT OUTER JOIN host AS workhost ON workhost.id = rw.workhost_id INNER JOIN revision ON revision.id = rw.revision_id WHERE lane.id = @lane_id AND createdtime IS NOT NULL ORDER BY rw.createdtime DESC LIMIT @nlimit; "; using (var reader = cmd.ExecuteReader ()) { var commits = new JArray (); while (reader.Read ()) { var commit = new JObject (); commit ["id"] = reader.GetInt32 (0); commit ["lane"] = reader.GetString (1); commit ["host"] = reader.GetString (2); commit ["revision"] = reader.GetString (3); commit ["status"] = reader.GetInt32 (4); commit ["createdtime"] = dateTimeToMilliseconds(reader.GetDateTimeOrNull(5)); commit ["assignedtime"] = dateTimeToMilliseconds(reader.GetDateTimeOrNull(6)); commit ["startedtime"] = dateTimeToMilliseconds(reader.GetDateTimeOrNull(7)); commit ["endtime"] = dateTimeToMilliseconds(reader.GetDateTimeOrNull(8)); commit ["assignedhost"] = reader.IsDBNull(9) ? null : reader.GetString (9); commits.Add (commit); } context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; context.Response.Write (commits.ToString ()); } } }
public static bool IsSuccess (this DBLaneDependency me, DB db, string revision) { using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @" SELECT RevisionWork.id FROM RevisionWork INNER JOIN Revision ON Revision.id = RevisionWork.revision_id WHERE RevisionWork.lane_id = @lane_id AND Revision.revision = @revision "; // The state of the dependent revision switch (me.Condition) { case DBLaneDependencyCondition.DependentLaneIssuesOrSuccess: cmd.CommandText += " AND (RevisionWork.state = @success OR RevisionWork.state == @issues)"; DB.CreateParameter (cmd, "issues", (int) DBState.Issues); break; default: cmd.CommandText += " AND RevisionWork.state = @success"; break; } DB.CreateParameter (cmd, "success", (int) DBState.Success); if (me.dependent_host_id.HasValue) { cmd.CommandText += " AND RevisionWork.host_id = @host_id"; DB.CreateParameter (cmd, "host_id", me.dependent_host_id.Value); } switch (me.Condition) { case DBLaneDependencyCondition.DependentLaneSuccess: case DBLaneDependencyCondition.DependentLaneIssuesOrSuccess: break; case DBLaneDependencyCondition.DependentLaneSuccessWithFile: cmd.CommandText += " AND WorkFile.filename = @filename"; DB.CreateParameter (cmd, "filename", me.filename); break; default: log.ErrorFormat ("LaneDependency '{0}' contains an unknown dependency condition: {1}", me.id, me.Condition); return false; } cmd.CommandText += " LIMIT 1;"; DB.CreateParameter (cmd, "lane_id", me.dependent_lane_id); DB.CreateParameter (cmd, "revision", revision); // Don't join with id here, if the revision comes from another lane, it might have a different id object obj = cmd.ExecuteScalar (); bool result = obj != null && !(obj is DBNull); log.DebugFormat ("Dependency id {0}: {1} (condition: {2}, revision: {3}, host_id: {4}, filename: {5}, lane: {6})", me.id, result, me.Condition, revision, me.dependent_host_id.HasValue ? me.dependent_host_id.Value.ToString () : "null", me.filename, me.dependent_lane_id); return result; } }
static void CleanupLogins () { int r; try { Stopwatch watch = new Stopwatch (); watch.Start (); using (DB db = new DB ()) { r = db.ExecuteNonQuery ("DELETE FROM login WHERE expires < now();"); } watch.Stop (); Logger.Log ("Maintenance: successfully cleaned up logins ({0} affected records) in {1} seconds", r, watch.Elapsed.TotalSeconds); } catch (Exception ex) { Logger.Log ("Maintenance: failed to cleanup logins: {0}", ex); } }
static void CleanupEmptyRevisionWorks () { int r; try { Stopwatch watch = new Stopwatch (); watch.Start (); using (DB db = new DB ()) { r = db.ExecuteNonQuery ( @"UPDATE revisionwork SET workhost_id = NULL, state = 10 WHERE id IN (SELECT id FROM revisionwork WHERE NOT EXISTS (SELECT work.id FROM work WHERE work.revisionwork_id = revisionwork.id) ) ;"); } watch.Stop (); Logger.Log ("Maintenance: successfully cleaned up empty revision works ({0} affected records) in {1} seconds", r, watch.Elapsed.TotalSeconds); } catch (Exception ex) { Logger.Log ("Maintenance: failed to cleanup empty revision work: {0}", ex); } }
/// <summary> /// Returns a list of all the dependencies for the specified lane. /// Returns null if there are no dependencies for the lane. /// </summary> /// <param name="lane"></param> /// <returns></returns> public static List<DBLaneDependency> GetDependencies (DB db, DBLane lane) { List<DBLaneDependency> result = null; using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "SELECT * FROM LaneDependency"; if (lane != null) { cmd.CommandText += " WHERE lane_id = @lane_id"; DB.CreateParameter (cmd, "lane_id", lane.id); } cmd.CommandText += ";"; using (IDataReader reader = cmd.ExecuteReader ()) { while (reader.Read ()) { if (result == null) result = new List<DBLaneDependency> (); result.Add (new DBLaneDependency (reader)); } } } return result; }
public static DBLaneDependency Create (DB db, int id) { return DBRecord_Extensions.Create (db, new DBLaneDependency (), DBLaneDependency.TableName, id); }
public DBFileStream (DBFile file, DB db) { try { transaction = db.dbcon.BeginTransaction (); obj = db.Manager.Open (file.file_id.Value); } catch { if (transaction != null) { transaction.Rollback (); transaction = null; } } }