/// <summary> /// Returns true if something was added to the database. /// </summary> /// <param name="db"></param> /// <param name="lane"></param> /// <param name="host"></param> /// <returns></returns> public static bool AddRevisionWork(DB db, List <DBLane> lanes, List <DBHostLane> hostlanes) { var stopwatch = new Stopwatch(); stopwatch.Start(); int line_count = 0; try { using (var cmd = db.CreateCommand(@" INSERT INTO RevisionWork (lane_id, host_id, revision_id, state) SELECT Lane.id, Host.id, Revision.id, 10 FROM HostLane INNER JOIN Host ON HostLane.host_id = Host.id INNER JOIN Lane ON HostLane.lane_id = Lane.id INNER JOIN Revision ON Revision.lane_id = lane.id WHERE HostLane.enabled = true AND NOT EXISTS ( SELECT 1 FROM RevisionWork WHERE RevisionWork.lane_id = Lane.id AND RevisionWork.host_id = Host.id AND RevisionWork.revision_id = Revision.id ) RETURNING lane_id, host_id, revision_id " )) using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int lane_id = reader.GetInt32(0); int host_id = reader.GetInt32(1); int revision_id = reader.GetInt32(2); var info = new GenericNotificationInfo(); info.laneID = lane_id; info.hostID = host_id; info.revisionID = revision_id; info.message = "Commit received."; info.state = DBState.Executing; Notifications.NotifyGeneric(info); line_count++; } } log.DebugFormat("AddRevisionWork: Added {0} records.", line_count); return(line_count > 0); } catch (Exception ex) { log.ErrorFormat("AddRevisionWork got an exception (will try a slower method): {0}", ex); return(AddRevisionWorkSlow(db, lanes, hostlanes)); } finally { stopwatch.Stop(); log.InfoFormat("AddRevisionWork [Done in {0} seconds]", stopwatch.Elapsed.TotalSeconds); } }
/// <summary> /// Returns true if something was added to the database. /// </summary> /// <param name="db"></param> /// <param name="lanes"></param> /// <param name="hostlanes"></param> /// <returns></returns> public static bool AddRevisionWorkSlow(DB db, List <DBLane> lanes, List <DBHostLane> hostlanes) { var stopwatch = new Stopwatch(); stopwatch.Start(); int line_count = 0; try { var selected_lanes = new Dictionary <int, DBLane> (); foreach (var hl in hostlanes) { if (!hl.enabled) { continue; } if (!selected_lanes.ContainsKey(hl.lane_id)) { selected_lanes [hl.lane_id] = lanes.Find((v) => v.id == hl.lane_id); } } foreach (var l in lanes) { if (l.enabled) { continue; } if (selected_lanes.ContainsKey(l.id)) { selected_lanes.Remove(l.id); } } foreach (var id in selected_lanes.Keys) { using (var cmd = db.CreateCommand(string.Format(@" INSERT INTO RevisionWork (lane_id, host_id, revision_id, state) SELECT Lane.id, Host.id, Revision.id, 10 FROM HostLane INNER JOIN Host ON HostLane.host_id = Host.id INNER JOIN Lane ON HostLane.lane_id = Lane.id INNER JOIN Revision ON Revision.lane_id = lane.id WHERE HostLane.enabled = true AND Lane.id = {0} AND NOT EXISTS ( SELECT 1 FROM RevisionWork WHERE RevisionWork.lane_id = Lane.id AND RevisionWork.host_id = Host.id AND RevisionWork.revision_id = Revision.id ) RETURNING lane_id, host_id, revision_id " , id))) using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int lane_id = reader.GetInt32(0); int host_id = reader.GetInt32(1); int revision_id = reader.GetInt32(2); var info = new GenericNotificationInfo(); info.laneID = lane_id; info.hostID = host_id; info.revisionID = revision_id; info.message = "Commit received."; info.state = DBState.Executing; Notifications.NotifyGeneric(info); line_count++; } } log.DebugFormat("AddRevisionWorkSlow: Added {0} records for lane {1}.", line_count, selected_lanes [id].lane); } return(line_count > 0); } catch (Exception ex) { log.ErrorFormat("AddRevisionWorkSlow got an exception: {0}", ex); return(false); } finally { stopwatch.Stop(); log.InfoFormat("AddRevisionWorkSlow [Done in {0} seconds]", stopwatch.Elapsed.TotalSeconds); } }