SharePoint 中数据表条目的计时器作业计划

发布于 2024-09-09 11:25:17 字数 1065 浏览 2 评论 0原文

我有一个列表,其中有一个 ItemUpdated 处理程序。

当我使用数据表视图进行编辑并修改每个项目时,ItemUpdated 事件显然将为每个项目运行。

在我的 ItemUpdated 事件中,我希望它检查是否有计划运行的计时器作业。如果有,则延长该作业的 SPOneTimeSchedule 计划,使其延迟 5 秒。如果没有,则创建计时器作业并将其安排为从现在开始 5 秒。

我尝试查看处理程序中是否存在作业定义,如果确实存在,则将计划延长 5 秒。如果不存在,则创建作业定义以在几分钟内运行。

MyTimerJob rollupJob = null;
foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions) 
{
    if (job.Name == Constants.JOB_ROLLUP_NAME)
    {
        rollupJob = (MyTimerJob)job;
    }
}
if (rollupJob == null)
{
        rollupJob = new MyTimerJob(Constants.JOB_ROLLUP_NAME, web.Site.WebApplication);
}

SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now.AddSeconds(5));
rollupJob.Schedule = schedule;
rollupJob.Update();

当我在服务器上尝试此操作时,出现很多错误

“发生更新冲突,您必须重试此操作。对象 MyTimerJob Name=MyTimerJobName Parent=SPWebApplication Name=SharePoint -80 正在由 NT 更新” w3wp 进程中的 AUTHORITY\NETWORK SERVICE

我认为该作业可能是第一次运行,一旦运行,其他 ItemUpdated 事件就会进入并找到现有的作业定义,然后它会尝试更新此定义,即使它当前正在运行。我是否应该创建一个新的作业定义名称,以便它不会超越第一个作业定义名称?或者将时间提高到一分钟?

I have a list on which I have an ItemUpdated handler.

When I edit using the datasheet view and modify every item, the ItemUpdated event will obviously run for every single item.

In my ItemUpdated event, I want it to check if there is a Timer Job scheduled to run. If there is, then extend the SPOneTimeSchedule schedule of this job to delay it by 5 seconds. If there isn't, then create the Timer Job and schedule it for 5 seconds from now.

I've tried looking to see if the job definition exists in the handler and if it does exist, then extend the schedule by 5 seconds. If it doesn't exist, then create the job definition to run in a minutes time.

MyTimerJob rollupJob = null;
foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions) 
{
    if (job.Name == Constants.JOB_ROLLUP_NAME)
    {
        rollupJob = (MyTimerJob)job;
    }
}
if (rollupJob == null)
{
        rollupJob = new MyTimerJob(Constants.JOB_ROLLUP_NAME, web.Site.WebApplication);
}

SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now.AddSeconds(5));
rollupJob.Schedule = schedule;
rollupJob.Update();

When I try this out on the server, I get a lot of errors

"An update conflict has occurred, and you must re-try this action. The object MyTimerJob Name=MyTimerJobName Parent=SPWebApplication Name=SharePoint -80 is being updated by NT AUTHORITY\NETWORK SERVICE in the w3wp process

I think the job is probably running for the first time and once running, the other ItemUpdated events are coming in and finding the existing Job definition. It then tries to Update this definition even though it is currently being used. Should I make a new Job Definition name so that it doesn't step on top of the first? Or raise the time to a minute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-09-16 11:25:17

我自己解决了这个问题,只需将延迟设置为从现在开始的一分钟,无论是否找到定义。这样,当它很忙时,它会不断推迟作业的调度,直到处理完成

I solved this myself by just setting the delay to a minutes time from now regardless of whether a definition is found. This way, while it is busy, it will keep pushing back the scheduling of the job until it is done processing

玉环 2024-09-16 11:25:17

这是因为该事件是异步的。您需要重新思考您要使用此代码解决的问题,并可能重新考虑它。

This is because the event is asynchronous. You'll need to rethink exactly what you're trying to solve with this code and potentially re-factor it.

七秒鱼° 2024-09-16 11:25:17

也许您应该尝试在计时器作业对象上使用“锁定”?

Maybe you should try using "lock" on the timer job object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文