예제 #1
0
        /// <summary>
        /// 临时任务(程序重启会丢失)
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public string AddTempTask(TimeSpan timeout, Action handle)
        {
            var id  = Guid.NewGuid().ToString();
            var bus = new IdleTimeout(() =>
            {
                _ib.TryRemove(id);
                Interlocked.Decrement(ref _quantityTempTask);
                if (handle != null)
                {
                    _wq.Enqueue(handle);
                }
            });

            if (_ib.TryRegister(id, () => bus, timeout))
            {
                _ib.Get(id);
                Interlocked.Increment(ref _quantityTempTask);
            }
            return(id);
        }
예제 #2
0
        void AddTaskPriv(TaskInfo task, bool isSave)
        {
            if (task.Round != -1 && task.CurrentRound >= task.Round)
            {
                return;
            }
            IdleTimeout bus = null;

            bus = new IdleTimeout(() =>
            {
                if (_ib.TryRemove(task.Id) == false)
                {
                    return;
                }
                var currentRound = task.IncrementCurrentRound();
                var round        = task.Round;
                if (round != -1 && currentRound >= round)
                {
                    if (_tasks.TryRemove(task.Id, out var old))
                    {
                        Interlocked.Decrement(ref _quantityTask);
                    }
                }
                _wq.Enqueue(() =>
                {
                    var result = new TaskLog
                    {
                        CreateTime = DateTime.UtcNow,
                        TaskId     = task.Id,
                        Round      = currentRound,
                        Success    = true
                    };
                    var startdt = DateTime.UtcNow;
                    try
                    {
                        _taskHandler.OnExecuting(this, task);
                    }
                    catch (Exception ex)
                    {
                        task.IncrementErrorTimes();
                        result.Exception = ex.InnerException == null ? $"{ex.Message}\r\n{ex.StackTrace}" : $"{ex.Message}\r\n{ex.StackTrace}\r\n\r\nInnerException: {ex.InnerException.Message}\r\n{ex.InnerException.StackTrace}";
                        result.Success   = false;
                    }
                    finally
                    {
                        result.ElapsedMilliseconds = (long)DateTime.UtcNow.Subtract(startdt).TotalMilliseconds;
                        task.LastRunTime           = DateTime.UtcNow;
                        _taskHandler.OnExecuted(this, task, result);
                    }
                    if (round == -1 || currentRound < round)
                    {
                        if (_ib.TryRegister(task.Id, () => bus, task.GetInterval()))
                        {
                            _ib.Get(task.Id);
                        }
                    }
                });
            });
            if (_tasks.TryAdd(task.Id, task))
            {
                if (isSave)
                {
                    try
                    {
                        _taskHandler.OnAdd(task);
                    }
                    catch
                    {
                        _tasks.TryRemove(task.Id, out var old);
                        throw;
                    }
                }
                Interlocked.Increment(ref _quantityTask);
                if (_ib.TryRegister(task.Id, () => bus, task.GetInterval()))
                {
                    _ib.Get(task.Id);
                }
            }
        }