How to get TaskScheduler to do something?

  • How to get TaskScheduler to do something?

    Posted by DSC Communities on April 12, 2020 at 12:40 pm
    • Wolter Kaper

      Member

      April 12, 2020 at 12:40 PM

      #Technical #BC/NAVPlus

      Dear Business Central / AL developer experts,

      I am new to TaskScheduler.
      I am trying to get the TaskScheduler to work from my AL code. I can schedule a task, it is visible in the “Scheduled Tasks” table. At the scheduled time, a row in the “Session Events” table appears. But the task does not do it’s usual work. What can be the reason for this?

      I wrote a test CodeUnit, if I run it by hand it writes a message to a custom log table. If I schedule it, the message does not get written… even though the task appears in the session events table.

      Please see the code below.
      Also added: a zip of the AL test project (with the MS apps removed to make it uploadable).
      ==================

      codeunitĀ 60001Ā TaskSchedulerTest
      {
      Ā Ā Ā Ā //ThisĀ isĀ theĀ TaskĀ toĀ perform.
      Ā Ā Ā Ā triggerĀ OnRun()
      Ā Ā Ā Ā var
      Ā Ā Ā Ā Ā Ā Ā Ā Log:Ā RecordĀ MessageLog;
      Ā Ā Ā Ā begin
      Ā Ā Ā Ā Ā Ā Ā Ā Log.Log(3,Ā ‘TheĀ testĀ CodeUnitĀ 60001Ā hasĀ run!’);
      Ā Ā Ā Ā end;

      Ā Ā Ā Ā //ThisĀ shouldĀ scheduleĀ theĀ Task,Ā aĀ chosenĀ numberĀ ofĀ minutesĀ afterĀ NOW.
      Ā Ā Ā Ā procedureĀ ScheduleMe()
      Ā Ā Ā Ā var
      Ā Ā Ā Ā Ā Ā Ā Ā MinutesAfterNow:Ā Integer;
      Ā Ā Ā Ā Ā Ā Ā Ā TargetCompany:Ā Text;
      Ā Ā Ā Ā Ā Ā Ā Ā NotBefore:Ā DateTime;
      Ā Ā Ā Ā Ā Ā Ā Ā TaskGuid:Ā Guid;
      Ā Ā Ā Ā begin
      Ā Ā Ā Ā Ā Ā Ā Ā //ConfigureĀ theĀ scheduling
      Ā Ā Ā Ā Ā Ā Ā Ā MinutesAfterNowĀ :=Ā 5;
      Ā Ā Ā Ā Ā Ā Ā Ā TargetCompanyĀ :=Ā ‘EntocareĀ CV’;Ā //changeĀ it

      Ā Ā Ā Ā Ā Ā Ā Ā //ScheduleĀ thisĀ CodeUnit
      Ā Ā Ā Ā Ā Ā Ā Ā NotBeforeĀ :=Ā CreateDateTime(Today(),Ā Time())Ā +Ā MinutesAfterNowĀ *Ā 60Ā *Ā 1000;
      Ā Ā Ā Ā Ā Ā Ā Ā ifĀ notĀ TaskScheduler.CanCreateTask()Ā then
      Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Error(‘CanCreateTaskĀ isĀ false.’);
      Ā Ā Ā Ā Ā Ā Ā Ā TaskGuidĀ :=Ā TaskScheduler.CreateTask(70001,Ā 0,Ā true,Ā TargetCompany,Ā NotBefore);
      Ā Ā Ā Ā Ā Ā Ā Ā Message(‘Scheduled:Ā ‘Ā +Ā TaskGuid);
      Ā Ā Ā Ā end;
      }

      ??

      ——————————
      Wolter Kaper
      Entocare C.V.
      Wageningen
      ——————————

    • Wolter Kaper

      Member

      April 13, 2020 at 6:25 AM

      OOPS…! Fellow programmers, sorry, that was not a good test! Contained 1 typing error, hope you have not spotted it. Here’s the corrected test code (the line that does “.CreateTask” has changed):

      codeunitĀ 60001Ā TaskSchedulerTest
      {
      Ā Ā Ā Ā //ThisĀ isĀ theĀ TaskĀ toĀ perform.
      Ā Ā Ā Ā triggerĀ OnRun()
      Ā Ā Ā Ā var
      Ā Ā Ā Ā Ā Ā Ā Ā Log:Ā RecordĀ MessageLog;
      Ā Ā Ā Ā begin
      Ā Ā Ā Ā Ā Ā Ā Ā Log.Log(3,Ā ‘TheĀ testĀ CodeUnitĀ 60001Ā hasĀ run!’);
      Ā Ā Ā Ā end;

      Ā Ā Ā Ā //ThisĀ shouldĀ scheduleĀ theĀ Task,Ā aĀ chosenĀ numberĀ ofĀ minutesĀ afterĀ NOW.
      Ā Ā Ā Ā procedureĀ ScheduleMe()
      Ā Ā Ā Ā var
      Ā Ā Ā Ā Ā Ā Ā Ā MinutesAfterNow:Ā Integer;
      Ā Ā Ā Ā Ā Ā Ā Ā TargetCompany:Ā Text;
      Ā Ā Ā Ā Ā Ā Ā Ā NotBefore:Ā DateTime;
      Ā Ā Ā Ā Ā Ā Ā Ā TaskGuid:Ā Guid;
      Ā Ā Ā Ā begin
      Ā Ā Ā Ā Ā Ā Ā Ā //ConfigureĀ theĀ scheduling
      Ā Ā Ā Ā Ā Ā Ā Ā MinutesAfterNowĀ :=Ā 5;
      Ā Ā Ā Ā Ā Ā Ā Ā TargetCompanyĀ :=Ā ‘EntocareĀ CV’;Ā //changeĀ it

      Ā Ā Ā Ā Ā Ā Ā Ā //ScheduleĀ thisĀ CodeUnit
      Ā Ā Ā Ā Ā Ā Ā Ā NotBeforeĀ :=Ā CreateDateTime(Today(),Ā Time())Ā +Ā MinutesAfterNowĀ *Ā 60Ā *Ā 1000;
      Ā Ā Ā Ā Ā Ā Ā Ā ifĀ notĀ TaskScheduler.CanCreateTask()Ā then
      Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Error(‘CanCreateTaskĀ isĀ false.’);
      Ā Ā Ā Ā Ā Ā Ā Ā TaskGuidĀ :=Ā TaskScheduler.CreateTask(60001,Ā 0,Ā true,Ā TargetCompany,Ā NotBefore);
      Ā Ā Ā Ā Ā Ā Ā Ā Message(‘Scheduled:Ā ‘Ā +Ā TaskGuid);
      Ā Ā Ā Ā end;
      }

      Results are still as stated: At the scheduled time, a row in the “Session Events” table appears. But the task does not get done. While it gets done if run by hand. Question: why?
      (Attached a corrected zip.)

      ——————————
      Wolter Kaper
      Entocare C.V.
      Wageningen
      ——————————
      ——————————————-

    • Kyle Hardin

      Member

      April 13, 2020 at 8:03 AM

      So you’ve eliminated code as the problem. Good first step. But this means that (probably) task scheduler isn’t running correctly, or at all.

      I recommend you run Task Scheduler in its own Service Tier. This lets you restart it as necessary without affecting users.

      You also need to choose an account for the service to use that is an actual BC user with the appropriate roles and permissions. This is an important step.

      In the BC Admin tool, make sure everything on the NAS tab is blank. And also make sure Task Scheduler is enabled. See the image I attached.

      Once all of this is done, restart the service and then check the event log for any errors.

      ——————————
      Kyle Hardin
      NAV Developer
      ArcherPoint Inc.
      Duluth GA
      ——————————
      ——————————————-

    • Wolter Kaper

      Member

      April 13, 2020 at 9:57 AM

      Dear Kyle,

      That sounds like good advice, thanks. But… we’re in the cloud! (BC365)
      I probably should have said that, right?
      We have an “admin center” but it looks quite different and has no section labeld “NAS” or “Task Scheduler”.
      Could it be I need to ask our Business Central service provider? (or “partner”)

      We are writing a custom extension for our own tenant, but we are not our own “provider”, is it the right word?

      Many regards and thanks,
      Wolter

      ——————————
      Wolter Kaper
      Entocare C.V.
      Wageningen
      ——————————
      ——————————————-

    • Kyle Hardin

      Member

      April 13, 2020 at 10:29 AM

      Shame on me for assuming onprem. And I will confess to not having much SaaS experience with BC.

      Does Last Known Error in page inspection when looking at the task scheduler entries show anything helpful?

      https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/manage-technical-support

      ——————————
      Kyle Hardin
      NAV Developer
      ArcherPoint Inc.
      Duluth GA
      ——————————
      ——————————————-

    • Kevin Fons

      Member

      April 14, 2020 at 9:17 AM

      Have you tried to create a job queue entry to run the codeunit on a schedule?Ā  That is what the job queue entries do in BC.

      ——————————
      Kevin Fons
      Senior Application Consultant
      Innovia Consulting
      Windsor WI
      ——————————
      ——————————————-

    • Wolter Kaper

      Member

      April 14, 2020 at 1:02 PM

      Dear Sir,

      I am a bit of a newbie, sorry for that. I just did what this manual page explains:
      https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-task-scheduler
      But it’s not doing what it promises…! (Or, more likely, I overlooked something)

      Is the “job queue” you’re referring to different from the “task schedule” that this page tells me about?
      If different: can you point me to a manual page about the Job Queue? I might like to try it.

      With best regards,
      Wolter

      ——————————
      Wolter Kaper
      Entocare C.V.
      Wageningen
      ——————————
      ——————————————-

    • Kevin Fons

      Member

      April 14, 2020 at 2:05 PM

      Take a look at this –Ā 

      https://docs.microsoft.com/en-us/dynamics365/business-central/admin-job-queues-schedule-tasks

      ——————————
      Kevin Fons
      Senior Application Consultant
      Innovia Consulting
      Windsor WI
      ——————————
      ——————————————-

    • Wolter Kaper

      Member

      April 15, 2020 at 2:51 PM

      Ah… it’s in the end-users manual. And I was thinking I had a programming problem.
      Thanks!

      ——————————
      Wolter Kaper
      Entocare C.V.
      Wageningen
      ——————————
      ——————————————-

    DSC Communities replied 5 years, 4 months ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

The discussion ‘How to get TaskScheduler to do something?’ is closed to new replies.

Start of Discussion
0 of 0 replies June 2018
Now

Welcome to our new site!

Here you will find a wealth of information created for peopleĀ  that are on a mission to redefine business models with cloud techinologies, AI, automation, low code / no code applications, data, security & more to compete in the Acceleration Economy!