首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用FluentScheduler库和Web调度作业?

如何使用FluentScheduler库和Web调度作业?
EN

Stack Overflow用户
提问于 2017-04-02 00:53:42
回答 1查看 4.8K关注 0票数 0

我无法让FluentScheduler在.Net Framework4.5.2WebAPI中工作。几天前,我问了一个关于通过控制台应用程序调度的类似问题,可以帮助它工作,但不幸的是,现在面对Web的问题。下面是密码。

代码语言:javascript
复制
    [HttpPost]
    [Route("Schedule")]
    public IHttpActionResult Schedule([FromBody] SchedulerModel schedulerModel)
    {
        var registry = new Registry();
        registry.Schedule<MyJob>().ToRunNow();
        JobManager.Initialize(registry);
        JobManager.StopAndBlock();
        return Json(new { success = true, message = "Scheduled!" });
    }

下面是我想安排的工作,现在只是把文字写到一个文件中。

代码语言:javascript
复制
public class SampleJob: IJob, IRegisteredObject
{
    private readonly object _lock = new object();
    private bool _shuttingDown;

    public SampleJob()
    {
        HostingEnvironment.RegisterObject(this);
    }

    public void Execute()
    {
        lock (_lock)
        {
            if (_shuttingDown)
                return;
          //Schedule writing to a text file
           WriteToFile();
        }
    }

    public void WriteToFile()
    {
        string text = "Random text";
        File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
    }

    public void Stop(bool immediate)
    {
        lock (_lock)
        {
            _shuttingDown = true;
        }            
        HostingEnvironment.UnregisterObject(this);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-02 21:15:00

这件事终于解决了。结果是我的注册表课出了问题。我不得不把它修改如下。

代码语言:javascript
复制
public class ScheduledJobRegistry: Registry
{
    public ScheduledJobRegistry(DateTime appointment)
    {
      //Removed the following line and replaced with next two lines
      //Schedule<SampleJob>().ToRunOnceIn(5).Seconds();
      IJob job = new SampleJob();
      JobManager.AddJob(job, s => s.ToRunOnceIn(5).Seconds());
    }

}

    [HttpPost]
    [Route("Schedule")]
    public IHttpActionResult Schedule([FromBody] SchedulerModel schedulerModel)
    {
        JobManager.Initialize(new ScheduledJobRegistry());                       
        JobManager.StopAndBlock();
        return Json(new { success = true, message = "Scheduled!" });
    }

还有一点要注意:我可以让它正常工作,但是在IIS中托管Api会使它变得很棘手,因为我们必须处理App回收、空闲时间等问题,但这看起来是一个好的开始。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43163544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档