示例#1
0
        public virtual async Task <TableResult> RemoveAsync(TEntity entity)
        {
            var result = await this._tableClient.ExecuteAsync(TableOperation.Delete(entity));

            return(result);
        }
示例#2
0
        public static async Task <string> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string code     = req.Query["code"];
            string username = req.Query["user"];

            string grant_type = "authorization_code";


            string url = @"https://login.microsoftonline.com/{teant}/oauth2/v2.0/token";

            HttpClient client = new HttpClient();


            CloudStorageAccount storageAccount =
                CloudStorageAccount.Parse("XXXX");

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference("appUsers");

            TableOperation retrieveOperation = TableOperation.Retrieve <User>(username, username);

            TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);

            User deleteEntity = (User)retrievedResult.Result;

            if (deleteEntity != null)
            {
                TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
                await table.ExecuteAsync(deleteOperation);
            }

            var values = new Dictionary <string, string>
            {
                { "grant_type", "authorization_code" },
                { "client_id", "a02f6ab7-1acd-4bfc-97d5-992acc1301b1" },
                { "scope", "https://graph.microsoft.com/User.Read offline_access" },
                { "redirect_uri", "https://graphsharepoint.azurewebsites.net/api/GetToken" },
                { "client_secret", "XXX" },
                { "code", code }
            };



            var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync(url, content);

            dynamic responseString = await response.Content.ReadAsAsync <object>();

            await table.CreateIfNotExistsAsync();


            User user = new User
            {
                RowKey       = username,
                name         = username,
                accessToken  = responseString.access_token,
                refreshToken = responseString.refresh_token,
                PartitionKey = username
            };

            TableOperation insertOperation = TableOperation.Insert(user);
            await table.ExecuteAsync(insertOperation);


            return(responseString.access_token);
        }
 private async Task UndoInsertOperationAsync(CloudTable table, ITableEntity entity)
 {
     var deleteOperation = TableOperation.Delete(entity);
     await table.ExecuteAsync(deleteOperation);
 }
 public static async System.Threading.Tasks.Task DeleteAsync <T>(this CloudTable table, T data) where T : TableEntity
 {
     await table.ExecuteAsync(TableOperation.Delete(data));
 }
示例#5
0
        public void DeleteById(string goodId)
        {
            var good = GetById(goodId);

            Table.Execute(TableOperation.Delete(good));
        }
示例#6
0
        private void ProcessQueueMessage(CloudQueueMessage msg)
        {
            Stopwatch requestTimer = Stopwatch.StartNew();
            var       request      = RequestTelemetryHelper.StartNewRequest("ProcessEmailQueueMessage", DateTimeOffset.UtcNow);

            CallContext.LogicalSetData(CORRELATION_SLOT, request.Id);
            //Thread.SetData(Thread.GetNamedDataSlot(CORRELATION_SLOT), request.Id);
            try
            {
                // Log and delete if this is a "poison" queue message (repeatedly processed
                // and always causes an error that prevents processing from completing).
                // Production applications should move the "poison" message to a "dead message"
                // queue for analysis rather than deleting the message.
                if (msg.DequeueCount > 5)
                {
                    Trace.TraceError("Deleting poison message:    message {0} Role Instance {1}.",
                                     msg.ToString(), GetRoleInstance());
                    sendEmailQueue.DeleteMessage(msg);
                    request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "PoisonMessage"));
                    request.Properties.Add(new KeyValuePair <string, string>("DequeueCount", msg.DequeueCount.ToString()));
                    if (msg.InsertionTime != null)
                    {
                        request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                    }
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                    return;
                }
                // Parse message retrieved from queue.
                // Example:  2012-01-01,[email protected],0
                var messageParts = msg.AsString.Split(new char[] { ',' });
                var partitionKey = messageParts[0];
                var rowKey       = messageParts[1];
                var restartFlag  = messageParts[2];
                Trace.TraceInformation("ProcessQueueMessage start:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                // If this is a restart, verify that the email hasn't already been sent.
                if (restartFlag == "1")
                {
                    var retrieveOperationForRestart = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
                    var retrievedResultForRestart   = messagearchiveTable.Execute(retrieveOperationForRestart);
                    var messagearchiveRow           = retrievedResultForRestart.Result as SendEmail;
                    if (messagearchiveRow != null)
                    {
                        // SendEmail row is in archive, so email is already sent.
                        // If there's a SendEmail Row in message table, delete it,
                        // and delete the queue message.
                        Trace.TraceInformation("Email already sent: partitionKey=" + partitionKey + " rowKey= " + rowKey);
                        var deleteOperation = TableOperation.Delete(new SendEmail {
                            PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                        });
                        try
                        {
                            messageTable.Execute(deleteOperation);
                        }
                        catch (Exception ex)
                        {
                            aiClient.TrackException(ex);
                        }
                        sendEmailQueue.DeleteMessage(msg);
                        request.Properties.Add(new KeyValuePair <string, string>("SuccessCode", "NoOp-MessageAlreadySent"));
                        if (msg.InsertionTime != null)
                        {
                            request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                        }
                        RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, true);
                        return;
                    }
                }
                // Get the row in the Message table that has data we need to send the email.
                var retrieveOperation      = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
                var retrievedResult        = messageTable.Execute(retrieveOperation);
                var emailRowInMessageTable = retrievedResult.Result as SendEmail;
                if (emailRowInMessageTable == null)
                {
                    Trace.TraceError("SendEmail row not found:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                    request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "SendEmailRowNotFound"));
                    if (msg.InsertionTime != null)
                    {
                        request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                    }
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                    return;
                }
                // Derive blob names from the MessageRef.
                var htmlMessageBodyRef = emailRowInMessageTable.MessageRef + ".htm";
                var textMessageBodyRef = emailRowInMessageTable.MessageRef + ".txt";
                // If the email hasn't already been sent, send email and archive the table row.
                if (emailRowInMessageTable.EmailSent != true)
                {
                    SendEmailToList(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);

                    var emailRowToDelete = new SendEmail {
                        PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                    };
                    emailRowInMessageTable.EmailSent = true;

                    var upsertOperation = TableOperation.InsertOrReplace(emailRowInMessageTable);
                    messagearchiveTable.Execute(upsertOperation);
                    var deleteOperation = TableOperation.Delete(emailRowToDelete);
                    messageTable.Execute(deleteOperation);
                }

                // Delete the queue message.
                sendEmailQueue.DeleteMessage(msg);
                Trace.TraceInformation("ProcessQueueMessage complete:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                request.Properties.Add(new KeyValuePair <string, string>("SuccessCode", "EmailSent"));
                if (msg.InsertionTime != null)
                {
                    request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                }
                RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, true);
            }
            catch (Exception ex)
            {
                request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "Exception"));
                if (msg.InsertionTime != null)
                {
                    request.Metrics.Add(new KeyValuePair <string, double>("FailedEmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                }
                RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                throw ex;
            }
        }
示例#7
0
        public async Task Delete(ITableEntity entity)
        {
            var op = TableOperation.Delete(entity);

            await this.ExecuteAsync(op);
        }
示例#8
0
 protected override TableOperation AsTableOperation()
 {
     return(TableOperation.Delete(Entity));
 }
示例#9
0
        /// <inheritdoc />
        public async Task <long> ExecuteDeleteAllAsync(CloudTable table, string partitionKey, string filter)
        {
            if (table == null)
            {
                throw new ArgumentNullException(nameof(table));
            }
            if (partitionKey == null)
            {
                throw new ArgumentNullException(nameof(partitionKey));
            }

            // Build query for retrieving exiting entries. We only ask for PK and RK.
            var query = new TableQuery()
            {
                FilterString  = filter,
                SelectColumns = new List <string> {
                    PartitionKey, RowKey
                },
            };

            AddPartitionKeyConstraint(query, partitionKey);

            try
            {
                long totalCount = 0;
                TableContinuationToken continuationToken = null;
                do
                {
                    var webHooks = (await ExecuteQueryAsync(table, query)).ToArray();
                    if (webHooks.Length == 0)
                    {
                        break;
                    }

                    // Delete query results in max of 100-count batches
                    var totalSegmentCount = webHooks.Length;
                    var segmentCount      = 0;
                    do
                    {
                        var batch      = new TableBatchOperation();
                        var batchCount = Math.Min(totalSegmentCount - segmentCount, MaxBatchSize);
                        for (var i = 0; i < batchCount; i++)
                        {
                            var entity = webHooks[segmentCount + i];
                            entity.ETag = "*";
                            var operation = TableOperation.Delete(entity);
                            batch.Add(operation);
                        }

                        await ExecuteBatchAsync(table, batch);

                        segmentCount += batchCount;
                    }while (segmentCount < totalSegmentCount);
                    totalCount += segmentCount;
                }while (continuationToken != null);
                return(totalCount);
            }
            catch (Exception ex)
            {
                var errorMessage = GetStorageErrorMessage(ex);
                var statusCode   = GetStorageStatusCode(ex);
                var message      = string.Format(CultureInfo.CurrentCulture, AzureStorageResources.StorageManager_OperationFailed, statusCode, errorMessage);
                _logger.Error(message, ex);
                throw new InvalidOperationException(message, ex);
            }
        }
示例#10
0
        async Task CleanupAsync(TableRequestOptions requestOptions, OperationContext operationContext)
        {
            // Clean up MTable-specific data from new table.
            // Future: Query only ones with properties we need to clean up.
            IQueryStream <MTableEntity> newTableStream = await newTable.ExecuteQueryStreamedAsync(
                new TableQuery <MTableEntity>(), requestOptions, operationContext);

            MTableEntity newEntity;

            while ((newEntity = await newTableStream.ReadRowAsync()) != null)
            {
                // XXX: Consider factoring out this "query and retry" pattern
                // into a separate method.
Attempt:
                TableOperation cleanupOp = newEntity.deleted ? TableOperation.Delete(newEntity)
                    : TableOperation.Replace(newEntity.Export <DynamicTableEntity>());
                TableResult cleanupResult;
                try
                {
                    cleanupResult = await newTable.ExecuteAsync(cleanupOp, requestOptions, operationContext);
                }
                catch (StorageException ex)
                {
                    if (ex.GetHttpStatusCode() == HttpStatusCode.NotFound)
                    {
                        // Someone else deleted it concurrently.  Nothing to do.
                        await monitor.AnnotateLastBackendCallAsync();

                        continue;
                    }
                    else if (ex.GetHttpStatusCode() == HttpStatusCode.PreconditionFailed)
                    {
                        await monitor.AnnotateLastBackendCallAsync();

                        // Unfortunately we can't assume that anyone who concurrently modifies
                        // the row while the table is in state USE_NEW_HIDE_METADATA will
                        // clean it up, because of InsertOrMerge.  (Consider redesign?)

                        // Re-retrieve row.
                        TableResult retrieveResult = await newTable.ExecuteAsync(
                            TableOperation.Retrieve <MTableEntity>(newEntity.PartitionKey, newEntity.RowKey),
                            requestOptions, operationContext);

                        await monitor.AnnotateLastBackendCallAsync();

                        if ((HttpStatusCode)retrieveResult.HttpStatusCode == HttpStatusCode.NotFound)
                        {
                            continue;
                        }
                        else
                        {
                            newEntity = (MTableEntity)retrieveResult.Result;
                            goto Attempt;
                        }
                    }
                    else
                    {
                        throw ChainTableUtils.GenerateInternalException(ex);
                    }
                }
                await monitor.AnnotateLastBackendCallAsync(
                    spuriousETagChanges :
                    cleanupResult.Etag == null?null : new List <SpuriousETagChange> {
                    new SpuriousETagChange(newEntity.PartitionKey, newEntity.RowKey, cleanupResult.Etag)
                });
            }

            // Delete everything from old table!  No one should be modifying it concurrently.
            IQueryStream <MTableEntity> oldTableStream = await oldTable.ExecuteQueryStreamedAsync(
                new TableQuery <MTableEntity>(), requestOptions, operationContext);

            MTableEntity oldEntity;

            while ((oldEntity = await oldTableStream.ReadRowAsync()) != null)
            {
                await oldTable.ExecuteAsync(TableOperation.Delete(oldEntity), requestOptions, operationContext);

                await monitor.AnnotateLastBackendCallAsync();
            }
        }
        public async Task <bool> DeleteEntityAsync(MetadataModel obj)
        {
            var deleteOperation = TableOperation.Delete(obj);

            return((await TABLE.ExecuteAsync(deleteOperation)).HttpStatusCode == 204);
        }
示例#12
0
        /// <summary>
        /// Deletes the specified azure shardlet.
        /// </summary>
        /// <param name="azureShardlet">The azure shardlet.</param>
        public void Delete(AzureShardlet azureShardlet)
        {
            _table.Execute(TableOperation.Delete(azureShardlet));

            AzureCache.Remove(GetCacheKey(_cacheType, azureShardlet));
        }
示例#13
0
 public async Task DeleteAsync(TaskEntity myTableOperation)
 {
     await Task.Run(() => _myTable.ExecuteAsync(TableOperation.Delete(myTableOperation)));
 }
示例#14
0
 private async Task UndoInsertOperationAsync(CloudTable cloudTable, ITableEntity tableEntity)
 => await cloudTable.ExecuteAsync(TableOperation.Delete(tableEntity));
示例#15
0
        // Based on: http://blogs.msdn.com/b/cesardelatorre/archive/2011/03/12/typical-issue-one-of-the-request-inputs-is-not-valid-when-working-with-the-wa-development-storage.aspx
        private async Task InitializeTableSchemaFromEntity(CloudTableClient tableClient)
        {
            const string operation = "InitializeTableSchemaFromEntity";
            var          startTime = DateTime.UtcNow;

            ITableEntity entity = new T();

            entity.PartitionKey = Guid.NewGuid().ToString();
            entity.RowKey       = Guid.NewGuid().ToString();
            Array.ForEach(
                entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance),
                p =>
            {
                if ((p.Name == "PartitionKey") || (p.Name == "RowKey") || (p.Name == "Timestamp"))
                {
                    return;
                }

                if (p.PropertyType == typeof(string))
                {
                    p.SetValue(entity, Guid.NewGuid().ToString(),
                               null);
                }
                else if (p.PropertyType == typeof(DateTime))
                {
                    p.SetValue(entity, startTime, null);
                }
            });

            try
            {
                // WAS:
                // svc.AddObject(TableName, entity);
                // SaveChangesOptions.None,

                CloudTable tableReference = tableClient.GetTableReference(TableName);
                try
                {
                    await Task <TableResult> .Factory.FromAsync(
                        tableReference.BeginExecute,
                        tableReference.EndExecute,
                        TableOperation.Insert(entity),
                        null);
                }
                catch (Exception exc)
                {
                    CheckAlertWriteError(operation + "-Create", entity, null, exc);
                    throw;
                }

                try
                {
                    // WAS:
                    // svc.DeleteObject(entity);
                    // SaveChangesOptions.None,

                    await Task <TableResult> .Factory.FromAsync(
                        tableReference.BeginExecute,
                        tableReference.EndExecute,
                        TableOperation.Delete(entity),
                        null);
                }
                catch (Exception exc)
                {
                    CheckAlertWriteError(operation + "-Delete", entity, null, exc);
                    throw;
                }
            }
            finally
            {
                CheckAlertSlowAccess(startTime, operation);
            }
        }
示例#16
0
        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(TElement entity)
        {
            var dynamicEntity = this.convertToTableEntity(entity);

            this.TableOperations.Add(TableOperation.Delete(dynamicEntity));
        }
示例#17
0
        private async Task ExecuteAtsOperatioons(List <AtsOperation> operations)
        {
            if (operations.Count == 1)
            {
                var            ats       = operations[0];
                TableOperation operation = null;
                switch (ats.OperationType)
                {
                case AtsOperationType.Insert:
                    operation = TableOperation.InsertOrReplace(ats.Row);
                    break;

                case AtsOperationType.Replace:
                    operation = TableOperation.InsertOrReplace(ats.Row);
                    break;

                case AtsOperationType.Delete:
                    operation = TableOperation.Delete(ats.Row);
                    break;

                default:
                    break;
                }

                if (operation != null)
                {
                    await ExecuteOperation(operation).ConfigureAwait(false);
                }
            }
            else
            {
                List <Task> tasks = new List <Task>();

                foreach (var partitionOperations in operations.GroupBy(x => x.Row.PartitionKey))
                {
                    var batch = new TableBatchOperation();
                    foreach (var operation in partitionOperations)
                    {
                        switch (operation.OperationType)
                        {
                        case AtsOperationType.Insert:
                            batch.InsertOrReplace(operation.Row);
                            break;

                        case AtsOperationType.Replace:
                            batch.InsertOrReplace(operation.Row);
                            break;

                        case AtsOperationType.Delete:
                            batch.Delete(operation.Row);
                            break;

                        default:
                            break;
                        }

                        // only 40, because the request itself can actually become too big... :)
                        if (batch.Count == 40)
                        {
                            tasks.Add(ExecuteBatchOperation(batch));

                            batch = new TableBatchOperation();
                        }
                    }
                    if (batch.Count != 0)
                    {
                        tasks.Add(ExecuteBatchOperation(batch));
                    }
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
        }
示例#18
0
        /// <summary>
        /// Queue a new operation
        /// </summary>
        /// <typeparam name="T">Type of businessObject</typeparam>
        /// <param name="listOfObjects">Business object instances - cannot be NULL.</param>
        /// <param name="type">Type of TableBatchOperation to generate</param>
        public void Add <T>(IEnumerable <T> listOfObjects, TableOperationType type) where T : class
        {
            if (_isDraining)
            {
                // no items can be added during a drain
                throw new Exception("Cannot queue items during a drain.");
            }

            if (listOfObjects == null)
            {
                throw new ArgumentNullException(nameof(listOfObjects));
            }

            int t = (int)type;

            // these are the int values of the range of operation types supported
            if ((t < 0) || (t > 5))
            {
                throw new ArgumentOutOfRangeException("Unsupported operation for queue!");
            }

            string currentPartitionKey = Guid.NewGuid().ToString(); // nobody's partition key will ever be the same as this!
            string tableName           = AzureTablesDataSource.GetTableName <T>();
            string operationName       = Enum.GetName(typeof(TableOperationType), type) ?? "Unknown";

            TableBatchOperation batch = new TableBatchOperation();

            foreach (T obj in listOfObjects)
            {
                TableOperation tableOperation = type switch
                {
                    TableOperationType.Delete => TableOperation.Delete(AzureTableEntity.From(obj, forDelete: true)),
                    TableOperationType.Insert => TableOperation.Insert(AzureTableEntity.From(obj)),
                    TableOperationType.InsertOrMerge => TableOperation.InsertOrMerge(AzureTableEntity.From(obj)),
                    TableOperationType.InsertOrReplace => TableOperation.InsertOrReplace(AzureTableEntity.From(obj)),
                    TableOperationType.Merge => TableOperation.Merge(AzureTableEntity.From(obj)),
                    TableOperationType.Replace => TableOperation.Replace(AzureTableEntity.From(obj))

                    // Actually redundant, since this is already checked at the top of the function.
                    ,
                    _ => throw new ArgumentOutOfRangeException($"Unsupported operation '{operationName}'")
                };

                // all items in a batch must be the same partition key
                // so if we hit a different one, we jump to a new batch
                if ((batch.Count > 0) && (tableOperation.Entity.PartitionKey != currentPartitionKey))
                {
                    _queue.Enqueue(new TableBatchOperationWrapper(batch, tableName));

                    batch = new TableBatchOperation();
                    currentPartitionKey = tableOperation.Entity.PartitionKey;
                }
                else if (batch.Count == 0)
                {
                    currentPartitionKey = tableOperation.Entity.PartitionKey;
                }

                batch.Add(tableOperation);

                if (batch.Count == __MAX_ITEMS_PER_BATCH)
                {
                    _queue.Enqueue(new TableBatchOperationWrapper(batch, tableName));

                    batch = new TableBatchOperation();
                }

                ItemAdded?.Invoke(tableName, operationName, currentPartitionKey, tableOperation.Entity.RowKey);
            }

            // flush remaining entities to the queue
            if (batch.Count > 0)
            {
                _queue.Enqueue(new TableBatchOperationWrapper(batch, tableName));
            }
        }
        public async Task Delete(TModel model)
        {
            TableOperation deleteOperation = TableOperation.Delete(model);

            await this.PrepareTable().ExecuteAsync(deleteOperation);
        }
示例#20
0
 public async Task Delete(OnboardingState state)
 {
     var entity          = ToEntity(state);
     var deleteOperation = TableOperation.Delete(entity);
     await table.ExecuteAsync(deleteOperation);
 }
示例#21
0
        public async Task DeleteAsync(T t)
        {
            var table = await GetTable();

            await table.ExecuteAsync(TableOperation.Delete(GetEntity(t)));
        }
示例#22
0
        public void Dispose()
        {
            TableOperation deleteOperation = TableOperation.Delete(workerRoleInstance);

            workerRoleTable.Execute(deleteOperation);
        }
示例#23
0
        public static async Task Run([TimerTrigger("0 0 07 * * *")] TimerInfo myTimer, TraceWriter log)
        {
            NewsBankClient newsBankClient = new NewsBankClient(
                new EnvironmentVariableEZProxySignInUriProvider(),
                _credProvider,
                new EnvironmentVariableProductBaseUriProvider(),
                new BasicCanLog(log)
                );


            CloudTable articleTable = CloudStorageAccount
                                      .Parse(_storage)
                                      .CreateCloudTableClient()
                                      .GetTableReference("article");

            if (checkTableExists)
            {
                articleTable.CreateIfNotExists();
            }

            DateTime today = new DateTime(DateTime.Today.Ticks, DateTimeKind.Utc);
            TableQuery <ArticlePost> articlesPublishedBeforeToday = articleTable.CreateQuery <ArticlePost>().Where(x => x.ArticleDate < today).AsTableQuery();



            CloudTable oauthTable = CloudStorageAccount
                                    .Parse(_storage)
                                    .CreateCloudTableClient()
                                    .GetTableReference("oauth");

            if (checkTableExists)
            {
                checkTableExists = false;
                oauthTable.CreateIfNotExists();
            }



            RedditOAuth result = (RedditOAuth)oauthTable
                                 .Execute(
                TableOperation.Retrieve <RedditOAuth>("reddit", _user)
                ).Result;

            //https://blog.maartenballiauw.be/post/2012/10/08/what-partitionkey-and-rowkey-are-for-in-windows-azure-table-storage.html
            //https://www.red-gate.com/simple-talk/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/

            if (result?.GetNewToken < DateTimeOffset.Now)
            {
                result = null;
                log.Info("need a new token");
            }

            Reddit      r                = null;
            BotWebAgent agent            = null;
            bool        saveToken        = false;
            bool        tryLogin         = false;
            int         tryLoginAttempts = 2;

            do
            {
                tryLoginAttempts--;
                tryLogin = false;
                if (result == null)
                {
                    agent  = new BotWebAgent(_user, _pass, _clientId, _secret, "https://www.reddit.com/user/somekindofbot0000/");
                    result = new RedditOAuth()
                    {
                        Token = agent.AccessToken, GetNewToken = DateTimeOffset.Now.AddMinutes(57), PartitionKey = "reddit", RowKey = _user
                    };
                    r         = new Reddit(agent, true);
                    saveToken = true;
                }
                else
                {
                    try
                    {
                        r = new Reddit(result.Token);
                    }
                    catch (AuthenticationException a)
                    {
                        result   = null;
                        tryLogin = true;
                    }
                    catch (WebException w)
                    {
                        if (w.Status == WebExceptionStatus.ProtocolError &&
                            (w.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            result   = null;
                            tryLogin = true;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } while (tryLogin && tryLoginAttempts > 0);

            if (r == null)
            {
                throw new Exception("couldn't get logged in");
            }

            if (saveToken)
            {
                oauthTable
                .Execute(
                    TableOperation.InsertOrReplace(result));
                log.Info("saving token");
            }

            List <Task> updateCommentTasks = new List <Task>();

            foreach (ArticlePost ap in articlesPublishedBeforeToday)
            {
                Task updateComment = GetCommentLine(ap, log, newsBankClient)
                                     .ContinueWith((commentLine) =>
                {
                    Comment c = r.GetComment(new Uri("https://www.reddit.com" + ap.CommentUri));
                    if (!String.IsNullOrEmpty(commentLine.Result))
                    {
                        EditComment(commentLine.Result, c);
                        articleTable.Execute(TableOperation.Delete(ap));
                    }
                    else
                    {
                        log.Info("Empty CommentLine update content");
                    }
                }
                                                   , TaskContinuationOptions.OnlyOnRanToCompletion);

                updateCommentTasks.Add(updateComment);
            }

            await Task.WhenAll(updateCommentTasks.ToArray());

            log.Info("AwaitWhenALL " + updateCommentTasks.Count.ToString());
        }
 public static async Task DeleteAsync(CloudTable table, ReportInProgressTableEntity entity)
 {
     var operation = TableOperation.Delete(entity);
     await table.ExecuteAsync(operation);
 }
        /// <summary>
        /// Delete an entity in the table storage.
        /// </summary>
        /// <param name="entity">Entity to be deleted.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        public async Task DeleteAsync(T entity)
        {
            var operation = TableOperation.Delete(entity);

            await this.Table.ExecuteAsync(operation);
        }
示例#26
0
 private void DeleteBroadcasted(DynamicTableEntity entity, uint256 hash)
 {
     try
     {
         BroadcastedTransaction unused;
         _BroadcastedTransactions.TryRemove(hash, out unused);
         entity.ETag = "*";
         Configuration.GetBroadcastedTransactionsListenable().CloudTable.Execute(TableOperation.Delete(entity));
     }
     catch (Exception ex)
     {
         StorageException storageEx = ex as StorageException;
         if (storageEx == null || storageEx.RequestInformation == null || storageEx.RequestInformation.HttpStatusCode != 404)
         {
             ListenerTrace.Error("Error while cleaning up broadcasted transaction " + hash, ex);
         }
     }
 }
        public async Task DeleteAsync(string id)
        {
            var item = await GetAsync(id);

            await Table.ExecuteAsync(TableOperation.Delete(item));
        }
示例#28
0
 void _SenderNode_MessageReceived(Node node, IncomingMessage message)
 {
     if (message.Message.Payload is GetDataPayload)
     {
         var getData = (GetDataPayload)message.Message.Payload;
         foreach (var data in getData.Inventory)
         {
             if (data.Type == InventoryType.MSG_TX && _BroadcastedTransactions.ContainsKey(data.Hash))
             {
                 var result = _BroadcastedTransactions[data.Hash];
                 var tx     = new TxPayload(result.Transaction);
                 node.SendMessage(tx);
                 ListenerTrace.Info("Broadcasted " + data.Hash);
                 try
                 {
                     Configuration.GetBroadcastedTransactionsListenable().CloudTable.Execute(TableOperation.Delete(result.ToEntity()));
                 }
                 catch (StorageException)
                 {
                 }
             }
         }
     }
     if (message.Message.Payload is RejectPayload)
     {
         var     reject = (RejectPayload)message.Message.Payload;
         uint256 txId   = reject.Hash;
         if (txId != null && _BroadcastedTransactions.ContainsKey(txId))
         {
             ListenerTrace.Info("Broadcasted transaction rejected (" + reject.Code + ") " + txId);
             DeleteBroadcasted(txId);
             if (reject.Code != RejectCode.DUPLICATE)
             {
                 UpdateBroadcastState(txId, reject.Code.ToString());
             }
         }
     }
     if (message.Message.Payload is PongPayload)
     {
         ListenerTrace.Verbose("Pong");
     }
 }
示例#29
0
        public async Task <string> Store(string expectedETag, string metadata, List <PendingTransactionState <TState> > statesToPrepare, long?commitUpTo, long?abortAfter)
        {
            if (this.key.ETag != expectedETag)
            {
                throw new ArgumentException(nameof(expectedETag), "Etag does not match");
            }

            // assemble all storage operations into a single batch
            // these operations must commit in sequence, but not necessarily atomically
            // so we can split this up if needed
            var batchOperation = new BatchOperation(logger, key, table);

            // first, clean up aborted records
            if (abortAfter.HasValue && states.Count != 0)
            {
                while (states.Count > 0 && states[states.Count - 1].Key > abortAfter)
                {
                    var entity = states[states.Count - 1].Value;
                    await batchOperation.Add(TableOperation.Delete(entity)).ConfigureAwait(false);

                    states.RemoveAt(states.Count - 1);

                    if (logger.IsEnabled(LogLevel.Trace))
                    {
                        logger.LogTrace($"{partition}.{states[states.Count - 1].Key:x16} Delete {entity.TransactionId}");
                    }
                }
            }

            // second, persist non-obsolete prepare records
            var obsoleteBefore = commitUpTo.HasValue ? commitUpTo.Value : key.CommittedSequenceId;

            if (statesToPrepare != null)
            {
                foreach (var s in statesToPrepare)
                {
                    if (s.SequenceId >= obsoleteBefore)
                    {
                        if (FindState(s.SequenceId, out var pos))
                        {
                            // overwrite with new pending state
                            var existing = states[pos].Value;
                            existing.TransactionId        = s.TransactionId;
                            existing.TransactionTimestamp = s.TimeStamp;
                            existing.TransactionManager   = s.TransactionManager;
                            existing.SetState(s.State, this.jsonSettings);
                            await batchOperation.Add(TableOperation.Replace(existing)).ConfigureAwait(false);

                            states.RemoveAt(pos);

                            if (logger.IsEnabled(LogLevel.Trace))
                            {
                                logger.LogTrace($"{partition}.{existing.SequenceId:x16} Update {existing.TransactionId}");
                            }
                        }
                        else
                        {
                            var entity = StateEntity.Create(this.jsonSettings, this.partition, s);
                            await batchOperation.Add(TableOperation.Insert(entity)).ConfigureAwait(false);

                            states.Insert(pos, new KeyValuePair <long, StateEntity>(s.SequenceId, entity));

                            if (logger.IsEnabled(LogLevel.Trace))
                            {
                                logger.LogTrace($"{partition}.{s.SequenceId:x16} Insert {entity.TransactionId}");
                            }
                        }
                    }
                }
            }

            // third, persist metadata and commit position
            key.Metadata = metadata;
            if (commitUpTo.HasValue && commitUpTo.Value > key.CommittedSequenceId)
            {
                key.CommittedSequenceId = commitUpTo.Value;
            }
            if (string.IsNullOrEmpty(this.key.ETag))
            {
                await batchOperation.Add(TableOperation.Insert(this.key)).ConfigureAwait(false);

                if (logger.IsEnabled(LogLevel.Trace))
                {
                    logger.LogTrace($"{partition}.k Insert");
                }
            }
            else
            {
                await batchOperation.Add(TableOperation.Replace(this.key)).ConfigureAwait(false);

                if (logger.IsEnabled(LogLevel.Trace))
                {
                    logger.LogTrace($"{partition}.k Update");
                }
            }

            // fourth, remove obsolete records
            if (states.Count > 0 && states[0].Key < obsoleteBefore)
            {
                FindState(obsoleteBefore, out var pos);
                for (int i = 0; i < pos; i++)
                {
                    await batchOperation.Add(TableOperation.Delete(states[i].Value)).ConfigureAwait(false);

                    if (logger.IsEnabled(LogLevel.Trace))
                    {
                        logger.LogTrace($"{partition}.{states[i].Key:x16} Delete {states[i].Value.TransactionId}");
                    }
                }
                states.RemoveRange(0, pos);
            }

            await batchOperation.Flush().ConfigureAwait(false);

            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug($"{partition} Stored v{this.key.CommittedSequenceId} eTag={key.ETag}");
            }

            return(key.ETag);
        }
示例#30
0
        public static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("ConnectionString"));

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();//storage table section
            CloudTable       table       = tableClient.GetTableReference("class");

            #region

            //table.Delete();
            //table.DeleteIfExists();

            Console.WriteLine("Table has been deleted or removed.All info has been deleted.");

            #endregion

            #region Info of table

            table.CreateIfNotExists();

            var tableNames = tableClient.ListTables();// all tables name

            foreach (CloudTable item in tableNames)
            {
                Console.WriteLine(item.Name);
            }

            #endregion

            #region Insert data

            Teacher teacher = new Teacher("001", "Teachers")
            {
                Name       = "Ricardo Celis",
                Assignment = "Programmer"
            };

            Teacher teacher2 = new Teacher("002", "Teachers")
            {
                Name       = "Carlos Paredes",
                Assignment = "Tester"
            };

            TableOperation insertData  = TableOperation.Insert(teacher);
            TableOperation insertData2 = TableOperation.Insert(teacher2);

            //TODO: Check batch operation
            table.Execute(insertData);
            table.Execute(insertData2);

            Console.WriteLine("Isnert has been process succesfully");

            #endregion

            #region Read data

            TableQuery <Teacher> query = new TableQuery <Teacher>().Where(TableQuery.GenerateFilterCondition("Partition Key", QueryComparisons.GreaterThan, "000"));

            foreach (Teacher item in table.ExecuteQuery(query))
            {
                Console.WriteLine("{0},  {1}\t{2}\t{3}", item.PartitionKey, item.RowKey, item.Name, item.Assignment);
            }

            Console.WriteLine("Excecution succesfully");

            #endregion

            #region Update data

            TableOperation updateData = TableOperation.Retrieve <Teacher>("002[Partition Key]", "Teachers");

            TableResult result = table.Execute(updateData);

            Teacher updateEntity = (Teacher)result.Result;

            if (updateEntity != null)
            {
                updateEntity.Name = "Diseño audiovisual";
                TableOperation updateOperation = TableOperation.Replace(updateEntity);
                table.Execute(updateOperation);
                Console.WriteLine("Entity updated");
            }
            else
            {
                Console.WriteLine("Entity not found");
            }

            #endregion

            #region Delete data

            TableOperation deleteData = TableOperation.Retrieve <Teacher>("002[Partition Key]", "Teachers");

            TableResult resultDelete = table.Execute(updateData);

            Teacher deleteEntity = (Teacher)result.Result;

            if (updateEntity != null)
            {
                TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
                table.Execute(deleteOperation);
                Console.WriteLine("Entity deleted");
            }
            else
            {
                Console.WriteLine("Entity not found");
            }

            #endregion

            Console.ReadLine();
        }