コード例 #1
0
ファイル: MessageDispatcher.cs プロジェクト: xpan/fastdeploy
        public static void DoWork(Func<Guid, IEnumerable<MessageDescriptor>> func)
        {
            var id = Guid.NewGuid();
            var messages = func(id).ToArray();

            var account = CloudStorageAccount.FromConfigurationSetting("mystorage");
            var client = account.CreateCloudQueueClient();
            var queue = client.GetQueueReference("fastdeployqueue");
            queue.CreateIfNotExist();

            foreach (var message in messages) {
                byte[] buffer;
                long length;
                BinaryFormatter.Serialize(message, out buffer, out length);
                queue.AddMessage(new CloudQueueMessage(buffer));
            }

            var task = new RetryTask(() => {
                using (var context = new DataContext(RoleEnvironment.GetConfigurationSettingValue("mydb"))) {
                    if (context.JobRun.Count(o => o.JobId == id && o.Status == 1) < messages.Length) {
                        throw new ApplicationException();
                    }
                }
            }).WithMaxElapsedTime(300).WithRecoverAction(() => Thread.Sleep(2000));
            RetryHelper.Invoke(task);
        }
コード例 #2
0
ファイル: DbHelper.cs プロジェクト: xpan/fastdeploy
        public static void PopulateTable(string connectionString, string tableName, Func<IEnumerable<DataColumn>, object[][]> func)
        {
            IEnumerable<DataColumn> columns = null;
            using (var connection = new SqlConnection(connectionString)) {
                connection.Open();
                columns = GetColumnInfo(tableName, connection).ToArray();
            }

            var dataTable = new DataTable();
            foreach (var column in columns) {
                dataTable.Columns.Add(column);
            }

            var rows = func(columns);

            foreach (var row in rows) {
                dataTable.Rows.Add(row);
            }

            var task = new RetryTask(() => {
                using (var connection = new SqlConnection(connectionString)) {
                    connection.Open();
                    using (var transaction = connection.BeginTransaction())
                    using (var bcp = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) {
                        bcp.BulkCopyTimeout = 300;
                        bcp.DestinationTableName = tableName;
                        try {
                            bcp.WriteToServer(dataTable);
                            transaction.Commit();
                        } catch (Exception) {
                            try {
                                transaction.Rollback();
                            } catch (Exception) {
                                //
                            }
                            throw;
                        }
                    }
                }
            }).WithMaxRetryTimes(2).WithRecoverAction(() => Thread.Sleep(5000));
            RetryHelper.Invoke(task);
        }