//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ public FabResponse <T> NewFabResp <T>() where T : new() { var fr = new FabResponse <T>(); fr.Data = new List <T>(); fr.Data.Add(new T()); return(fr); }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ private void CheckBatch(Batch pBatch, ParallelLoopState pState, long pIndex) { string msg = "BatchId " + pBatch.Id; if (pBatch.ExportList.Count == 0) { ++vCheckCount; CommIo.Print(msg + "Empty."); return; } Data.Domain.Export e = pBatch.ExportList[0]; //CommIo.Print(" - First Export Id="+e.Id+", ArtifactId="+e.Artifact.Id+ // ", FabricId="+e.FabricId); var f = new FabricClient(); f.AppDataProvSession.RefreshTokenIfNecessary(); f.UseDataProviderPerson = true; if (!f.AppDataProvSession.IsAuthenticated) { throw new Exception("Could not authenticate."); } FabResponse <FabClass> fr = f.Services.Traversal.GetRootStep.ClassId(e.FabricId).Get(); msg += " \t(" + (++vCheckCount) + " \tof " + vTotalCount + "): \t"; if (fr == null) { lock ( vFailList ) { vFailList.Add(pBatch); } CommIo.Print(msg + "Failed. FabResponse was null."); return; } FabClass c = fr.FirstDataItem(); if (c == null) { lock ( vFailList ) { vFailList.Add(pBatch); } CommIo.Print(msg + "Failed. FabClass was null."); return; } CommIo.Print(msg + "ArtifactId=" + c.ArtifactId + "."); }
/*--------------------------------------------------------------------------------------------*/ protected T AssertFabResponseData <T>(BrowserResponse pResp) where T : FabObject { FabResponse <T> fr = AssertFabResponse <T>(pResp); Assert.Null(fr.Error, "FabResponse.Error should be null: " + fr.Error.ToJson()); Assert.NotNull(fr.Data, "FabResponse.Data should be filled."); Assert.AreEqual(1, fr.Data.Count, "Incorrect FabResponse.Data length."); Assert.NotNull(fr.Data[0], "FabResponse.Data[0] should be filled."); return(fr.Data[0]); }
/*--------------------------------------------------------------------------------------------*/ protected FabResponse <T> AssertFabResponse <T>(BrowserResponse pResp) where T : FabObject { AssertBody(pResp); FabResponse <T> fr = JsonSerializer.DeserializeFromString <FabResponse <T> >(pResp.Body.AsString()); Assert.NotNull(fr, "The FabResponse<" + typeof(T).Name + "> object should be filled."); return(fr); }
/*--------------------------------------------------------------------------------------------*/ private Response HandleError(NancyContext pContext, Exception pException) { Log.Fatal("Error at " + pContext.Request.Path, pException); var fr = new FabResponse <FabObject>(); fr.Error = FabError.ForInternalServerError(); var jr = new JsonResponse(fr, new ServiceStackJsonSerializer()); jr.StatusCode = HttpStatusCode.InternalServerError; return(jr); }
/*--------------------------------------------------------------------------------------------*/ public FabResponse <T> NewFabRespList <T>(int pCount) where T : new() { var fr = new FabResponse <T>(); fr.Data = new List <T>(); for (int i = 0; i < pCount; ++i) { fr.Data.Add(new T()); } return(fr); }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ protected void OnUnhandledException(IApiResponse pResp, Exception pEx) { var fr = new FabResponse <FabObject>(); fr.Error = FabError.ForInternalServerError(); fr.TotalMs = pResp.GetTimerMilliseconds(); pResp.SetJsonWith(fr); pResp.Status = HttpStatusCode.InternalServerError; pResp.Unhandled = pEx; Log.Fatal("Unhandled exception: " + pEx.Message, pEx); }
/*--------------------------------------------------------------------------------------------*/ protected virtual FabricResponse <T> GetFabricResponse(IClientContext pContext) { string fullPath = pContext.Config.ApiPath + Path + (Query != null ? "?" + Query : ""); pContext.LogInfo("Request initiated..."); //// try { pContext.LogInfo("Request Path: " + Method + " " + Path); pContext.LogInfo("Request URL: " + fullPath); IFabricHttpResponse wr = GetHttpWebResponse(fullPath); string data = StreamToString(wr.GetResponseStream()); pContext.LogDebug("Request Response: " + data); return(new FabricResponse <T>(JsonSerializer.DeserializeFromString <T>(data))); } catch (WebException we) { if (we.Response == null) { throw new Exception("No Fabric response from " + Method + " " + fullPath); } string data = StreamToString(we.Response.GetResponseStream()); bool isOauthErr = (data.Length > 9 && data.Substring(0, 9) == "{\"error\":"); bool isRespErr = typeof(FabResponse).IsAssignableFrom(typeof(T)); pContext.LogDebug("Request Error: " + data + " (IsError=" + isRespErr + ", IsOauthError=" + isOauthErr + ")"); if (isRespErr) { FabResponse frErr = JsonSerializer.DeserializeFromString <FabResponse>(data); return(new FabricResponse <T>(frErr, we)); } if (isOauthErr) { FabOauthError oerr = JsonSerializer.DeserializeFromString <FabOauthError>(data); return(new FabricResponse <T>(oerr, we)); } throw; } }
/*--------------------------------------------------------------------------------------------*/ public void Handle(HttpStatusCode pCode, NancyContext pContext) { var err = new FabError(); err.Code = (int)FabFault.Code.HttpError; err.Name = FabFault.Code.HttpError + ""; err.Message = pContext.Response.StatusCode + " (" + (int)pContext.Response.StatusCode + ") " + "for API request: " + pContext.Request.Method + " " + pContext.Request.Path; var fr = new FabResponse <FabObject>(); fr.Error = err; var jr = new JsonResponse(fr, new ServiceStackJsonSerializer()); jr.StatusCode = pCode; pContext.Response = jr; }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ private void ThreadAction(IList <T> pBatch, ParallelLoopState pState, long pIndex) { if (pBatch == null || pBatch.Count == 0) { ThreadPrint(pIndex, "Batch is empty, leaving thread"); ++vThreadDoneCount; ++vThreadSkipCount; return; } try { long t = DateTime.UtcNow.Ticks; FabResponse <FabBatchResult> fr = ThreadAddItemsToFabric(pBatch, pIndex); string fabSecs = GetSecs(t); lock ( vThreadResults ) { vThreadResults.Add(fr); } ++vThreadDoneCount; double perc = vThreadDoneCount / (double)vBatchCount; double time = (DateTime.UtcNow.Ticks - vThreadStartTime) / 10000000.0; double perSec = ((vThreadDoneCount - vThreadSkipCount) * vBatchSize) / time; long bar = (DateTime.UtcNow.Ticks - t) / 10000000; //seconds string barStr = new string('#', (int)bar); ThreadPrint(pIndex, (vDebug ? " * ............................................................. " : "") + "Finished batch " + vThreadDoneCount + " of " + vBatchCount + " \t" + fr.DbMs + "/" + fr.TotalMs + "\t" + fabSecs + " fab \t" + GetSecs(vThreadStartTime) + " tot \t" + (perc * 100).ToString("##0.000") + "% \t" + perSec.ToString("#0.000") + " exp/sec | " + barStr); } catch (Exception e) { ThreadPrint(pIndex, " # EXCEPTION: " + e); vFailureCount += vBatchSize; } }
/*--------------------------------------------------------------------------------------------*/ public override IApiResponse Execute() { var resp = new ApiResponse(); resp.Status = HttpStatusCode.OK; resp.StartTimer(); try { ApiReq.OpCtx.Auth.ExecuteOauth(); IList <T> list = vGetResponse(); resp.StopTimer(); FabResponse <T> fr = NewFabResponse(); fr.Data = list; fr.TotalMs = resp.GetTimerMilliseconds(); resp.SetJsonWith(fr); } catch (Exception e) { FabError err = OnException(e); resp.StopTimer(); if (err == null) { OnUnhandledException(resp, e); } else { var fr = new FabResponse <FabObject>(); fr.TotalMs = resp.GetTimerMilliseconds(); fr.Error = err; resp.SetJsonWith(fr); resp.Status = (err.Code == (int)FabFault.Code.AuthorizationRequired ? HttpStatusCode.Unauthorized : HttpStatusCode.BadRequest); } } resp.LogResponse(ApiReq); return(resp); }
public void SendFabricError() { var fe = new FabError(); fe.Name = "Test"; fe.Message = "Desc"; fe.Code = 500; var fr = new FabResponse(); fr.Error = fe; var wr = new TestWebResponse(GetStream(fr.ToJson())); var we = new WebException("Error", null, WebExceptionStatus.UnknownError, wr); vMoqHttpProv .Setup(x => x.GetResponse(vHttpReq)) .Throws(we); var req = NewFabricRequest <FabResponse <FabApp> >(); try { req.Send(vContext, SessionType.Default); Assert.Fail("WebException expected, but not thrown."); } catch (FabricErrorException errEx) { Assert.NotNull(errEx.RespError, "Error should be filled."); Assert.Null(errEx.OauthError, "OauthError should be null."); Assert.AreEqual(fe.Code, errEx.RespError.Error.Code, "Incorrect Error.Code."); Assert.AreEqual(fe.Name, errEx.RespError.Error.Name, "Incorrect Error.Name."); Assert.AreEqual(fe.Message, errEx.RespError.Error.Message, "Incorrect Error.Message."); } catch (Exception e) { Assert.Fail("WebException expected: " + e); } }
/*--------------------------------------------------------------------------------------------*/ public FabricResponse(FabResponse pRespError, Exception pExcep) { RespError = pRespError; Excep = pExcep; }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ /// <summary /> public FabricErrorException(FabResponse pRespError) : base(pRespError.Error.Name + " (" + pRespError.Error.Code + "): " + pRespError.Error.Message) { RespError = pRespError; }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ public override void Start() { try { CommIo.Print("Authenticating Fabric DataProvider..."); var f = new FabricClient(); f.AppDataProvSession.RefreshTokenIfNecessary(); f.UseDataProviderPerson = true; if (!f.AppDataProvSession.IsAuthenticated) { throw new Exception("DataProvider is not authenticated."); } CommIo.Print("DataProvider authenticated."); } catch (Exception e) { CommIo.Print("Authentication exception: " + e); return; } var sp = new SessionProvider(); using (ISession sess = sp.OpenSession()) { if (vJobId == -1) { Job j = sess.QueryOver <Job>() .OrderBy(x => x.Id).Desc .Take(1) .SingleOrDefault(); vJobId = j.Id; } CommIo.Print("Loading Job " + vJobId); IList <Batch> batchList = sess.QueryOver <Batch>() .Where(x => x.Job.Id == vJobId) .List(); var failList = new List <Batch>(); int i = 0; foreach (Batch b in batchList) { CommIo.Print("Confirming Batch " + b.Id + " (" + (++i) + " of " + batchList.Count + ")"); Data.Domain.Export e = b.ExportList[0]; CommIo.Print(" - First Export Id=" + e.Id + ", ArtifactId=" + e.Artifact.Id + ", FabricId=" + e.FabricId); var f = new FabricClient(); f.AppDataProvSession.RefreshTokenIfNecessary(); f.UseDataProviderPerson = true; if (!f.AppDataProvSession.IsAuthenticated) { throw new Exception("Could not authenticate."); } FabResponse <FabClass> fr = f.Services.Traversal.GetRootStep.ClassId(e.FabricId).Get(); if (fr == null) { failList.Add(b); CommIo.Print(" - FabResponse was null."); continue; } FabClass c = fr.FirstDataItem(); if (c == null) { failList.Add(b); CommIo.Print(" - FabClass was null."); continue; } CommIo.Print(" - Found class: " + c.ArtifactId); } CommIo.Print(""); CommIo.Print("Failures: " + failList.Count); foreach (Batch b in failList) { CommIo.Print(" - Batch " + b.Id); } } }
/*--------------------------------------------------------------------------------------------*/ private FabResponse <FabBatchResult> ThreadAddItemsToFabric(IList <T> pBatch, long pIndex) { var f = new FabricClient(); f.AppDataProvSession.RefreshTokenIfNecessary(); f.UseDataProviderPerson = true; if (!f.AppDataProvSession.IsAuthenticated) { throw new Exception("Could not authenticate."); } if (vDebug) { ThreadPrint(pIndex, "Starting batch..."); } TBatchNew[] newBatchItems = GetNewBatchList(pBatch, pIndex); FabResponse <FabBatchResult> fr = AddToFabric(f, newBatchItems); if (fr == null) { vFailureCount += vBatchSize; throw new Exception(" - FabResponse is null."); } if (fr.Error != null) { FabError e = fr.Error; vFailureCount += vBatchSize; throw new Exception(" - FabError " + e.Code + ": " + e.Name + " / " + e.Message); } if (fr.Data == null) { vFailureCount += vBatchSize; throw new Exception(" - FabResponse.Data is null."); } foreach (FabBatchResult br in fr.Data) { if (br.Error == null) { continue; } vFailureCount++; CommIo.Print(" # ERROR: " + br.Error.Name + " (" + br.Error.Code + "): " + br.Error.Message + " [" + br.BatchId + " / " + br.ResultId + "]"); //Enables "repair" mode /*if ( br.Error.Name == "UniqueConstraintViolation" ) { * const string idStr = "ArtifactId="; * string msg = br.Error.Message; * int idIndex = msg.IndexOf(idStr); * * if ( idIndex != -1 ) { * idIndex += idStr.Length; * int dotIndex = msg.IndexOf(".", idIndex); * string classId = msg.Substring(idIndex, dotIndex-idIndex); * ThreadPrint(pIndex, "Repair: "+br.BatchId+", "+b.Id+", "+classId); * * var e2 = new Data.Domain.Export(); * e2.Batch = b; * e2.FabricId = long.Parse(classId); * SetItemTypeId(sess, e2, (int)br.BatchId); * sess.Save(e2); * } * }*/ } if (vDebug) { foreach (FabBatchResult br in fr.Data) { ThreadPrint(pIndex, " * Export success: " + vTypeName + " " + br.BatchId + " == Fab " + br.ResultId); } } return(fr); }