18.定时任务

作者: 落地成佛 | 来源:发表于2017-08-30 22:13 被阅读31次

    一、概述

    实现 IBackgroundWorker 接口,继承 BackgroundWorkerBase 或PeriodicBackgroundWorkerBase

    二、使用

    2.1 定义

    public class MakeInactiveUsersPassiveWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
    private readonly IRepository<User, long> _userRepository;
    public MakeInactiveUsersPassiveWorker(AbpTimer timer, IRepository<User, long> userRepository)
    : base(timer)
    {
        _userRepository = userRepository;
        Timer.Period = 5000; //5 seconds (good for tests, but normally will be more)
    }
    [UnitOfWork]
    protected override void DoWork()
    {
        using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
        {
            var oneMonthAgo = Clock.Now.Subtract(TimeSpan.FromDays(30));
            var inactiveUsers = _userRepository.GetAllList(u =>
                                            u.IsActive &&);
            foreach (var inactiveUser in inactiveUsers)
            {
                  inactiveUser.IsActive = false;
            );
        } 
        CurrentUnitOfWork.SaveChanges();
      }
    }
    

    2.2 注册

    public class MyProjectWebModule : AbpModule
    {
    //...
    public override void PostInitialize()
    {
        var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
        workManager.Add(IocManager.Resolve<MakeInactiveUsersPassiveWorker>());
    }
    }
    

    相关文章

      网友评论

        本文标题:18.定时任务

        本文链接:https://www.haomeiwen.com/subject/rkqfjxtx.html