Beispiel #1
0
        //================================================================================= Write events

        private static void WriteRegisterTaskEvent(RegisterTaskResult result, string machineName, SqlCommand command)
        {
            var task = result.Task;

            WriteEvent(command, result.NewlyCreated ? TaskEventType.Registered : TaskEventType.Updated, task.Id,
                       null, task.Title, null,
                       task.AppId, machineName, null,
                       task.Tag, task.Type, task.Order, task.Hash, task.TaskData);
        }
Beispiel #2
0
        //================================================================================= Write events

        private Task WriteRegisterTaskEventAsync(RegisterTaskResult result, string machineName, SqlCommand command, CancellationToken cancellationToken)
        {
            var task = result.Task;

            return(WriteEventAsync(command, result.NewlyCreated ? TaskEventType.Registered : TaskEventType.Updated, task.Id,
                                   null, task.Title, null,
                                   task.AppId, machineName, null,
                                   task.Tag, task.Type, task.Order, task.Hash, task.TaskData, cancellationToken));
        }
Beispiel #3
0
        private static RegisterTaskResult RegisterTask(string type, string title, double order, string appId, string tag, string finalizeUrl, long hash, string taskDataSerialized, string machineName)
        {
            using (var cn = new SqlConnection(Configuration.ConnectionString))
            {
                cn.Open();
                var        tran = cn.BeginTransaction();
                var        cm1  = cn.CreateCommand();
                SqlCommand cm2  = null;
                cm1.CommandText = REGISTERTASKSQL;
                cm1.Connection  = cn;
                cm1.Transaction = tran;
                cm1.CommandType = CommandType.Text;

                try
                {
                    cm1.Parameters.Add("@Type", SqlDbType.NVarChar).Value        = type;
                    cm1.Parameters.Add("@Title", SqlDbType.NVarChar).Value       = title;
                    cm1.Parameters.Add("@Order", SqlDbType.Float).Value          = order;
                    cm1.Parameters.Add("@Tag", SqlDbType.NVarChar).Value         = tag;
                    cm1.Parameters.Add("@AppId", SqlDbType.NVarChar).Value       = (object)appId ?? DBNull.Value;
                    cm1.Parameters.Add("@FinalizeUrl", SqlDbType.NVarChar).Value = finalizeUrl;
                    cm1.Parameters.Add("@Hash", SqlDbType.BigInt).Value          = hash;
                    cm1.Parameters.Add("@TaskData", SqlDbType.NVarChar).Value    = taskDataSerialized;

                    var result = new RegisterTaskResult();
                    var id     = 0;
                    using (var reader = cm1.ExecuteReader())
                    {
                        // there must be only one row
                        reader.Read();
                        var o = reader[0];

                        // task registration was not successful
                        if (o == DBNull.Value)
                        {
                            return(null);
                        }

                        id = Convert.ToInt32(o);
                        result.NewlyCreated = reader.GetBoolean(1);
                        result.Updated      = reader.GetBoolean(2);
                    }

                    result.Task = new SnTask
                    {
                        Id           = id,
                        Title        = title,
                        Order        = order,
                        Tag          = tag,
                        TaskData     = taskDataSerialized,
                        RegisteredAt = DateTime.UtcNow,
                        Hash         = hash,
                        Type         = type,
                        AppId        = appId,
                        FinalizeUrl  = finalizeUrl
                    };

                    if (result.NewlyCreated || result.Updated)
                    {
                        cm2             = cn.CreateCommand();
                        cm2.Transaction = tran;
                        WriteRegisterTaskEvent(result, machineName, cm2);
                    }

                    tran.Commit();
                    return(result);
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
                finally
                {
                    cm1.Dispose();
                    if (cm2 != null)
                    {
                        cm2.Dispose();
                    }
                }
            }
        }
Beispiel #4
0
        public RegisterTaskResult RegisterTask(RegisterTaskRequest taskRequest)
        {
            Application app = null;

            try
            {
                // load the corresponding application to make sure the appid is valid
                app = ApplicationHandler.GetApplication(taskRequest.AppId);
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, "Error loading app for id " + taskRequest.AppId, EventId.TaskManagement.General);
            }

            // If we do not know the appid, we must not register the task. Client applications
            // must observe this response and try to re-register the application, before
            // trying to register the task again (this can happen if the TaskManagement Web
            // was unreachable when the client application tried to register the appid before).
            if (app == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, RegisterTaskRequest.ERROR_UNKNOWN_APPID));
            }

            RegisterTaskResult result = null;

            try
            {
                // calculate hash with the default algrithm if not given
                var hash = taskRequest.Hash == 0
                    ? ComputeTaskHash(taskRequest.Type + taskRequest.AppId + taskRequest.Tag + taskRequest.TaskData)
                    : taskRequest.Hash;

                result = TaskDataHandler.RegisterTask(
                    taskRequest.Type,
                    taskRequest.Title,
                    taskRequest.Priority,
                    taskRequest.AppId,
                    taskRequest.Tag,
                    taskRequest.FinalizeUrl,
                    hash,
                    taskRequest.TaskData,
                    taskRequest.MachineName);
            }
            catch (Exception ex)
            {
                // the client app needs to be notified
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            try
            {
                // notify agents
                AgentHub.BroadcastMessage(result.Task);

                // notify monitor clients
                TaskMonitorHub.OnTaskEvent(SnTaskEvent.CreateRegisteredEvent(
                                               result.Task.Id, result.Task.Title, string.Empty, result.Task.AppId,
                                               result.Task.Tag, null, result.Task.Type, result.Task.Order,
                                               result.Task.Hash, result.Task.TaskData));
            }
            catch (Exception ex)
            {
                // The task has been created successfully, this error is only about
                // notification, so client applications should not be notified.
                SnLog.WriteException(ex, "Error during agent or monitor notification after a task was registered.", EventId.TaskManagement.Communication);
            }

            return(result);
        }
Beispiel #5
0
        private async Task <RegisterTaskResult> RegisterTaskAsync(string type, string title, double order, string appId,
                                                                  string tag, string finalizeUrl, long hash, string taskDataSerialized, string machineName, CancellationToken cancellationToken)
        {
            await using var cn = new SqlConnection(_connectionString);
            await cn.OpenAsync(cancellationToken).ConfigureAwait(false);

            var        tran = (SqlTransaction)(await cn.BeginTransactionAsync(cancellationToken).ConfigureAwait(false));
            var        cm1  = cn.CreateCommand();
            SqlCommand cm2  = null;

            cm1.CommandText = REGISTERTASKSQL;
            cm1.Connection  = cn;
            cm1.Transaction = tran;
            cm1.CommandType = CommandType.Text;

            try
            {
                cm1.Parameters.Add("@Type", SqlDbType.NVarChar).Value        = type;
                cm1.Parameters.Add("@Title", SqlDbType.NVarChar).Value       = title;
                cm1.Parameters.Add("@Order", SqlDbType.Float).Value          = order;
                cm1.Parameters.Add("@Tag", SqlDbType.NVarChar).Value         = (object)tag ?? DBNull.Value;
                cm1.Parameters.Add("@AppId", SqlDbType.NVarChar).Value       = (object)appId ?? DBNull.Value;
                cm1.Parameters.Add("@FinalizeUrl", SqlDbType.NVarChar).Value = (object)finalizeUrl ?? DBNull.Value;
                cm1.Parameters.Add("@Hash", SqlDbType.BigInt).Value          = hash;
                cm1.Parameters.Add("@TaskData", SqlDbType.NVarChar).Value    = taskDataSerialized;

                var result = new RegisterTaskResult();
                int id;
                await using (var reader = await cm1.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
                {
                    // there must be only one row
                    await reader.ReadAsync(cancellationToken).ConfigureAwait(false);

                    var o = reader[0];

                    // task registration was not successful
                    if (o == DBNull.Value)
                    {
                        return(null);
                    }

                    id = Convert.ToInt32(o);
                    result.NewlyCreated = reader.GetBoolean(1);
                    result.Updated      = reader.GetBoolean(2);
                }

                result.Task = new SnTask
                {
                    Id           = id,
                    Title        = title,
                    Order        = order,
                    Tag          = tag,
                    TaskData     = taskDataSerialized,
                    RegisteredAt = DateTime.UtcNow,
                    Hash         = hash,
                    Type         = type,
                    AppId        = appId,
                    FinalizeUrl  = finalizeUrl
                };

                if (result.NewlyCreated || result.Updated)
                {
                    cm2             = cn.CreateCommand();
                    cm2.Transaction = tran;
                    await WriteRegisterTaskEventAsync(result, machineName, cm2, cancellationToken).ConfigureAwait(false);
                }

                await tran.CommitAsync(cancellationToken).ConfigureAwait(false);

                return(result);
            }
            catch
            {
                await tran.RollbackAsync(cancellationToken).ConfigureAwait(false);

                throw;
            }
            finally
            {
                await cm1.DisposeAsync().ConfigureAwait(false);

                if (cm2 != null)
                {
                    await cm2.DisposeAsync().ConfigureAwait(false);
                }
            }
        }