Example #1
0
        public JsonResult SendMessages(List <Message> toSend)
        {
            if (toSend == null)
            {
                return(Json(new SMSResponse()
                {
                    Status = 404, Message = "Nema poruka za poslati!"
                }));
            }
            SMSResponse response = new SMSResponse();
            Dictionary <Message, bool> sendStatus = new Dictionary <Message, bool>();

            try
            {
                foreach (Message msg in toSend)
                {
                    sendStatus.Add(msg, msg.SendMessage());
                }

                BulkInsertStatus   insStatus   = this.msgRep.InsertMessage(toSend.Select(x => x.MapToMessageDB()).ToList());
                BulkDeliveryStatus delivStatus = Message.DetermineMessageDeliveryStatus(sendStatus);
                response = Message.MessageToDeliver(delivStatus, insStatus);
            }
            catch (Exception ex)
            {
                ///TODO log exception
                return(Json(new SMSResponse(500, "Error in saving messages")));
            }

            return(Json(response, JsonRequestBehavior.AllowGet));
        }
Example #2
0
		public override void Respond(IHttpContext context)
		{
			if (string.IsNullOrEmpty(context.Request.QueryString["no-op"]) == false)
			{
				// this is a no-op request which is there just to force the client HTTP layer to handle the authentication
				// only used for legacy clients
				return; 
			}
			if("generate-single-use-auth-token".Equals(context.Request.QueryString["op"],StringComparison.InvariantCultureIgnoreCase))
			{
				// using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
				// instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
				// we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating 
				// a single use token for them.
				var token = server.RequestAuthorizer.GenerateSingleUseAuthToken(Database, context.User);
				context.WriteJson(new
				{
					Token = token
				});
				return;
			}

			if (HttpContext.Current != null)
			{
				HttpContext.Current.Server.ScriptTimeout = 60*60*6; // six hours should do it, I think.
			}
			var options = new BulkInsertOptions
			{
				CheckForUpdates = context.GetCheckForUpdates(),
				CheckReferencesInIndexes = context.GetCheckReferencesInIndexes()
			};

			var operationId = ExtractOperationId(context);
			var sp = Stopwatch.StartNew();

			var status = new BulkInsertStatus();

			int documents = 0;
			var mre = new ManualResetEventSlim(false);

			var currentDatbase = Database;
			var task = Task.Factory.StartNew(() =>
			{
				currentDatbase.BulkInsert(options, YieldBatches(context, mre, batchSize => documents += batchSize), operationId);
			    status.Documents = documents;
			    status.Completed = true;
			});

			long id;
			Database.AddTask(task, status, out id);

			mre.Wait(Database.WorkContext.CancellationToken);

			context.Log(log => log.Debug("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

			context.WriteJson(new
			{
				OperationId = id
			});
		}
Example #3
0
        public static SMSResponse MessageToDeliver(BulkDeliveryStatus dlvStat, BulkInsertStatus insStat)
        {
            SMSResponse resp = new SMSResponse();
            string      msg  = string.Empty;

            if (dlvStat == BulkDeliveryStatus.SUCCESS && insStat == BulkInsertStatus.SUCCESS)
            {
                resp.Message = "Poruke poslane i zabilježene!";
                resp.Status  = 200;
            }
            else if (dlvStat == BulkDeliveryStatus.SUCCESS && insStat != BulkInsertStatus.SUCCESS)
            {
                resp.Message = "Poruke poslane ali njihovo slanje nije zabilježeno!";
                resp.Status  = 202;
            }
            else if (dlvStat == BulkDeliveryStatus.PARTIAL)
            {
                resp.Message = "Nisu sve poruke poslane!";
                resp.Status  = 500;
            }
            else
            {
                resp.Message = "Poruke nisu poslane!";
                resp.Status  = 500;
            }
            return(resp);
        }
Example #4
0
        public override void Respond(IHttpContext context)
        {
            if (string.IsNullOrEmpty(context.Request.QueryString["no-op"]) == false)
            {
                // this is a no-op request which is there just to force the client HTTP layer to handle the authentication
                // only used for legacy clients
                return;
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.Server.ScriptTimeout = 60 * 60 * 6;             // six hours should do it, I think.
            }
            var options = new BulkInsertOptions
            {
                CheckForUpdates          = context.GetCheckForUpdates(),
                CheckReferencesInIndexes = context.GetCheckReferencesInIndexes()
            };

            var operationId = ExtractOperationId(context);
            var sp          = Stopwatch.StartNew();

            var status = new BulkInsertStatus();

            int documents = 0;
            var mre       = new ManualResetEventSlim(false);

            var currentDatbase = Database;
            var task           = Task.Factory.StartNew(() =>
            {
                currentDatbase.BulkInsert(options, YieldBatches(context, mre, batchSize => documents += batchSize), operationId);
                status.Documents = documents;
                status.Completed = true;
            });

            long id;

            Database.AddTask(task, status, out id);

            mre.Wait(Database.WorkContext.CancellationToken);

            context.Log(log => log.Debug("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

            context.WriteJson(new
            {
                OperationId = id
            });
        }
Example #5
0
		public override void Respond(IHttpContext context)
		{
			if (string.IsNullOrEmpty(context.Request.QueryString["no-op"]) == false)
			{
				// this is a no-op request which is there just to force the client HTTP layer to handle the authentication
				// only used for legacy clients
				return; 
			}

			if (HttpContext.Current != null)
			{
				HttpContext.Current.Server.ScriptTimeout = 60*60*6; // six hours should do it, I think.
			}
			var options = new BulkInsertOptions
			{
				CheckForUpdates = context.GetCheckForUpdates(),
				CheckReferencesInIndexes = context.GetCheckReferencesInIndexes()
			};

			var operationId = ExtractOperationId(context);
			var sp = Stopwatch.StartNew();

			var status = new BulkInsertStatus();

			int documents = 0;
			var mre = new ManualResetEventSlim(false);

			var currentDatbase = Database;
			var task = Task.Factory.StartNew(() =>
			{
				currentDatbase.BulkInsert(options, YieldBatches(context, mre, batchSize => documents += batchSize), operationId);
			    status.Documents = documents;
			    status.Completed = true;
			});

			long id;
			Database.AddTask(task, status, out id);

			mre.Wait(Database.WorkContext.CancellationToken);

			context.Log(log => log.Debug("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

			context.WriteJson(new
			{
				OperationId = id
			});
		}
        public async Task<HttpResponseMessage> BulkInsertPost()
        {
            if (string.IsNullOrEmpty(GetQueryStringValue("no-op")) == false)
            {
                // this is a no-op request which is there just to force the client HTTP layer to handle the authentication
                // only used for legacy clients
                return GetEmptyMessage();
            }
            if ("generate-single-use-auth-token".Equals(GetQueryStringValue("op"), StringComparison.InvariantCultureIgnoreCase))
            {
                // using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
                // instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
                // we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating 
                // a single use token for them.

                var authorizer = (MixedModeRequestAuthorizer)Configuration.Properties[typeof(MixedModeRequestAuthorizer)];

                var token = authorizer.GenerateSingleUseAuthToken(DatabaseName, User);
                return GetMessageWithObject(new
                {
                    Token = token
                });
            }

            if (HttpContext.Current != null)
                HttpContext.Current.Server.ScriptTimeout = 60 * 60 * 6; // six hours should do it, I think.

            var options = new BulkInsertOptions
            {
                OverwriteExisting = GetOverwriteExisting(),
                CheckReferencesInIndexes = GetCheckReferencesInIndexes(),
				SkipOverwriteIfUnchanged = GetSkipOverwriteIfUnchanged()
            };

            var operationId = ExtractOperationId();
            var sp = Stopwatch.StartNew();

            var status = new BulkInsertStatus();
            status.IsTimedOut = false;

            var documents = 0;
            var mre = new ManualResetEventSlim(false);
            var tre = new CancellationTokenSource();
            
            var inputStream = await InnerRequest.Content.ReadAsStreamAsync().ConfigureAwait(false);
            var currentDatabase = Database;
            var timeout = tre.TimeoutAfter(currentDatabase.Configuration.BulkImportBatchTimeout);
            var user = CurrentOperationContext.User.Value;
            var headers = CurrentOperationContext.Headers.Value;
            Exception error = null;
            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    CurrentOperationContext.User.Value = user;
                    CurrentOperationContext.Headers.Value = headers;
                    currentDatabase.Documents.BulkInsert(options, YieldBatches(timeout, inputStream, mre, batchSize => documents += batchSize), operationId, tre.Token, timeout);
                }
				catch (InvalidDataException e)
				{
					status.Faulted = true;
					status.State = RavenJObject.FromObject(new { Error = "Could not understand json.", InnerError = e.SimplifyException().Message });
					status.IsSerializationError = true;
					error = e;
				}
				catch (OperationCanceledException)
                {
                    // happens on timeout
                    currentDatabase.Notifications.RaiseNotifications(new BulkInsertChangeNotification { OperationId = operationId, Message = "Operation cancelled, likely because of a batch timeout", Type = DocumentChangeTypes.BulkInsertError });
                    status.IsTimedOut = true;
                    status.Faulted = true;
                }
                catch (Exception e)
                {
                    status.Faulted = true;
                    status.State = RavenJObject.FromObject(new { Error = e.SimplifyException().Message });
                    error = e;
                }
                finally
                {
                    status.Completed = true;
                    status.Documents = documents;
	                CurrentOperationContext.User.Value = null;
	                CurrentOperationContext.Headers.Value = null;

					timeout.Dispose();
                }
			}, tre.Token);

            long id;
            Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription
                                                 {
                                                     StartTime = SystemTime.UtcNow,
                                                     TaskType = TaskActions.PendingTaskType.BulkInsert,
                                                     Payload = operationId.ToString()
                                                 }, out id, tre);

            await task;

            if (error != null)
            {
				var httpStatusCode = status.IsSerializationError ? (HttpStatusCode)422 : HttpStatusCode.InternalServerError;
	            return GetMessageWithObject(new
                {
                    error.Message,
                    Error = error.ToString()
				}, httpStatusCode);
            }
	        if (status.IsTimedOut)
				throw new TimeoutException("Bulk insert operation did not receive new data longer than configured threshold");

            sp.Stop();

            AddRequestTraceInfo(log => log.AppendFormat("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

            return GetMessageWithObject(new
            {
                OperationId = id
            });
        }
Example #7
0
        public override void Respond(IHttpContext context)
        {
            if (string.IsNullOrEmpty(context.Request.QueryString["no-op"]) == false)
            {
                // this is a no-op request which is there just to force the client HTTP layer to handle the authentication
                // only used for legacy clients
                return;
            }
            if ("generate-single-use-auth-token".Equals(context.Request.QueryString["op"], StringComparison.InvariantCultureIgnoreCase))
            {
                // using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
                // instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
                // we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating
                // a single use token for them.
                var token = server.RequestAuthorizer.GenerateSingleUseAuthToken(Database, context.User);
                context.WriteJson(new
                {
                    Token = token
                });
                return;
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.Server.ScriptTimeout = 60 * 60 * 6;             // six hours should do it, I think.
            }
            var options = new BulkInsertOptions
            {
                CheckForUpdates          = context.GetCheckForUpdates(),
                CheckReferencesInIndexes = context.GetCheckReferencesInIndexes()
            };

            var operationId = ExtractOperationId(context);
            var sp          = Stopwatch.StartNew();

            var status = new BulkInsertStatus();

            int documents = 0;
            var mre       = new ManualResetEventSlim(false);

            var currentDatbase = Database;
            var task           = Task.Factory.StartNew(() =>
            {
                currentDatbase.BulkInsert(options, YieldBatches(context, mre, batchSize => documents += batchSize), operationId);
                status.Documents = documents;
                status.Completed = true;
            });

            long id;

            Database.AddTask(task, status, out id);

            mre.Wait(Database.WorkContext.CancellationToken);

            context.Log(log => log.Debug("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

            context.WriteJson(new
            {
                OperationId = id
            });
        }
Example #8
0
        public async Task <HttpResponseMessage> BulkInsertPost()
        {
            if (string.IsNullOrEmpty(GetQueryStringValue("no-op")) == false)
            {
                // this is a no-op request which is there just to force the client HTTP layer to handle the authentication
                // only used for legacy clients
                return(GetEmptyMessage());
            }
            if ("generate-single-use-auth-token".Equals(GetQueryStringValue("op"), StringComparison.InvariantCultureIgnoreCase))
            {
                // using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
                // instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
                // we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating
                // a single use token for them.

                var authorizer = (MixedModeRequestAuthorizer)Configuration.Properties[typeof(MixedModeRequestAuthorizer)];

                var token = authorizer.GenerateSingleUseAuthToken(DatabaseName, User);
                return(GetMessageWithObject(new
                {
                    Token = token
                }));
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.Server.ScriptTimeout = 60 * 60 * 6; // six hours should do it, I think.
            }
            var options = new BulkInsertOptions
            {
                OverwriteExisting        = GetOverwriteExisting(),
                CheckReferencesInIndexes = GetCheckReferencesInIndexes(),
                SkipOverwriteIfUnchanged = GetSkipOverwriteIfUnchanged()
            };

            var operationId = ExtractOperationId();
            var sp          = Stopwatch.StartNew();

            var status = new BulkInsertStatus();

            status.IsTimedOut = false;

            var documents = 0;
            var mre       = new ManualResetEventSlim(false);
            var tre       = new CancellationTokenSource();

            var inputStream = await InnerRequest.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var currentDatabase = Database;
            var timeout         = tre.TimeoutAfter(currentDatabase.Configuration.BulkImportBatchTimeout);
            var task            = Task.Factory.StartNew(() =>
            {
                try
                {
                    currentDatabase.Documents.BulkInsert(options, YieldBatches(timeout, inputStream, mre, batchSize => documents += batchSize), operationId, tre.Token);
                }
                catch (OperationCanceledException)
                {
                    // happens on timeout
                    currentDatabase.Notifications.RaiseNotifications(new BulkInsertChangeNotification {
                        OperationId = operationId, Message = "Operation cancelled, likely because of a batch timeout", Type = DocumentChangeTypes.BulkInsertError
                    });
                    status.Completed  = true;
                    status.IsTimedOut = true;
                    throw;
                }
                status.Completed = true;
                status.Documents = documents;
            });

            long id;

            Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription
            {
                StartTime = SystemTime.UtcNow,
                TaskType  = TaskActions.PendingTaskType.BulkInsert,
                Payload   = operationId.ToString()
            }, out id, tre);

            task.Wait(Database.WorkContext.CancellationToken);
            if (status.IsTimedOut)
            {
                throw new TimeoutException("Bulk insert operation did not receive new data longer than configured treshold");
            }

            sp.Stop();

            AddRequestTraceInfo(log => log.AppendFormat("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

            return(GetMessageWithObject(new
            {
                OperationId = id
            }));
        }
Example #9
0
		public async Task<HttpResponseMessage> BulkInsertPost()
		{
			if (string.IsNullOrEmpty(GetQueryStringValue("no-op")) == false)
			{
				// this is a no-op request which is there just to force the client HTTP layer to handle the authentication
				// only used for legacy clients
				return GetEmptyMessage();
			}
			if ("generate-single-use-auth-token".Equals(GetQueryStringValue("op"), StringComparison.InvariantCultureIgnoreCase))
			{
				// using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
				// instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
				// we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating 
				// a single use token for them.

				var authorizer = (MixedModeRequestAuthorizer)Configuration.Properties[typeof(MixedModeRequestAuthorizer)];

				var token = authorizer.GenerateSingleUseAuthToken(Database, User, this);
				return GetMessageWithObject(new
				{
					Token = token
				});
			}

			if (HttpContext.Current != null)
				HttpContext.Current.Server.ScriptTimeout = 60*60*6; // six hours should do it, I think.

			var options = new BulkInsertOptions
			{
				OverwriteExisting = GetOverwriteExisting(),
				CheckReferencesInIndexes = GetCheckReferencesInIndexes()
			};

			var operationId = ExtractOperationId();
			var sp = Stopwatch.StartNew();

			var status = new BulkInsertStatus();

			var documents = 0;
			var mre = new ManualResetEventSlim(false);

			var inputStream = await InnerRequest.Content.ReadAsStreamAsync();
			var currentDatabase = Database;
			var task = Task.Factory.StartNew(() =>
			{
				currentDatabase.Documents.BulkInsert(options, YieldBatches(inputStream , mre, batchSize => documents += batchSize), operationId);
				status.Documents = documents;
				status.Completed = true;
			});

			long id;
			Database.Tasks.AddTask(task, status, out id);

			mre.Wait(Database.WorkContext.CancellationToken);

			//TODO: log
			//context.Log(log => log.Debug("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

			return GetMessageWithObject(new
			{
				OperationId = id
			});
		}
Example #10
0
        public BulkInsertStatus CreateInventory(Inventory inventory)
        {
            string CreateInventoryItemQuery(long transactionId, InventoryItem inventoryItem)
            {
                var query = $@"
                    INSERT INTO INVTRS
                    (
                        TrsNo,
                        ItemNo, 
                        Pkuda,
                        Store, 
                        SerNo, 
                        AccNo, 
                        Agent, 
                        Subject, 
                        AuxQty, 
                        BatchNo,                         
                        Details, 
                        DocCode, 
                        ExpireDate, 
                        FcSum, 
                        InOut,                         
                        Qty,
                        Color,                         
                        Size,                                                 
                        ""Sum"", 
                        Voucher,                        
                        date                        
                    )                        
                    (
                        SELECT   
                        {transactionId},
                        '{SafeConvert.ToPervasiveString(inventoryItem.ItemId)}', 
                        {inventoryItem.CommandNo},
                        {inventoryItem.StoreNo}, 
                        '{inventoryItem.SerialNo}', 
                        {inventoryItem.AccountId}, 
                        {inventoryItem.AgentId}, 
                        {inventoryItem.SubjectNo}, 
                        {inventoryItem.AuxQty}, 
                        '{inventoryItem.BatchNo}',                         
                        '{SafeConvert.ToPervasiveString(inventoryItem.Details)}', 
                        '{inventoryItem.DocumentCode}', 
                        {PervasiveDBHelper.ToPervasiveDate(inventoryItem.ExpireDate)}, 
                        {inventoryItem.FcTotal}, 
                        '{inventoryItem.InOut}',                         
                        {inventoryItem.Quantity},
                        '{inventoryItem.Color}',                         
                        '{inventoryItem.Size}',                                                 
                        {inventoryItem.Total}, 
                        {inventoryItem.Voucher},                        
                        {PervasiveDBHelper.ToPervasiveDate(DateTime.Now)}                        
                    )
                ";

                return(query);
            }

            OdbcTransaction transaction = null;
            var             result      = new BulkInsertStatus();

            try
            {
                using (var connection = new OdbcConnection(this.ConnetionString))
                {
                    connection.Open();
                    transaction = connection.BeginTransaction();

                    var command = new OdbcCommand();

                    command.Connection  = connection;
                    command.Transaction = transaction;

                    inventory.Items.ForEach(inventoryItem => {
                        var transactionId   = PervasiveDBHelper.GetColumnMaxValue(command, "INVTRS", "TrsNo");
                        command.CommandText = CreateInventoryItemQuery(transactionId, inventoryItem);
                        command.ExecuteNonQuery();

                        result.NumOfSuccesses++;
                    });

                    transaction.Commit();
                }

                return(result);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                result.NumOfFailures++;
                throw;
            }
        }
        public async Task <HttpResponseMessage> BulkInsertPost()
        {
            if (string.IsNullOrEmpty(GetQueryStringValue("no-op")) == false)
            {
                // this is a no-op request which is there just to force the client HTTP layer to handle the authentication
                // only used for legacy clients
                return(GetEmptyMessage());
            }
            if ("generate-single-use-auth-token".Equals(GetQueryStringValue("op"), StringComparison.InvariantCultureIgnoreCase))
            {
                // using windows auth with anonymous access = none sometimes generate a 401 even though we made two requests
                // instead of relying on windows auth, which require request buffering, we generate a one time token and return it.
                // we KNOW that the user have access to this db for writing, since they got here, so there is no issue in generating
                // a single use token for them.

                var authorizer = (MixedModeRequestAuthorizer)Configuration.Properties[typeof(MixedModeRequestAuthorizer)];

                var token = authorizer.GenerateSingleUseAuthToken(Database, User, this);
                return(GetMessageWithObject(new
                {
                    Token = token
                }));
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.Server.ScriptTimeout = 60 * 60 * 6;             // six hours should do it, I think.
            }
            var options = new BulkInsertOptions
            {
                OverwriteExisting        = GetOverwriteExisting(),
                CheckReferencesInIndexes = GetCheckReferencesInIndexes()
            };

            var operationId = ExtractOperationId();
            var sp          = Stopwatch.StartNew();

            var status = new BulkInsertStatus();

            var documents = 0;
            var mre       = new ManualResetEventSlim(false);

            var inputStream = await InnerRequest.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var currentDatabase = Database;
            var task            = Task.Factory.StartNew(() =>
            {
                currentDatabase.Documents.BulkInsert(options, YieldBatches(inputStream, mre, batchSize => documents += batchSize), operationId);
                status.Documents = documents;
                status.Completed = true;
            });

            long id;

            Database.Tasks.AddTask(task, status, out id);

            mre.Wait(Database.WorkContext.CancellationToken);

            sp.Stop();

            AddRequestTraceInfo(log => log.AppendFormat("\tBulk inserted received {0:#,#;;0} documents in {1}, task #: {2}", documents, sp.Elapsed, id));

            return(GetMessageWithObject(new
            {
                OperationId = id
            }));
        }