How to restart a job in “Error” or “Frozen Completed” status


Sometimes can happen that some recursively scheduled jobs can go wrong or get stuck, you can re-enable them automatically and after consult the log to see errors; it is not a perfect solution but sometimes the error can be a simple error ex: “another user has modified..” etc. This function could be useful for NAV versions up to 2016, NAV 2017 has an improved management and a robust task scheduling system.

Samples of Jobs in “Error” or “Frozen Completed State”

Before Execution

After Execution

The Simple Solution

The solution is based on a codeunit that takes care of reactivating the jobs in the state “error” or “completed”, it can be changed according to the needs.

It can be scheduled:

  • Directly into the NAS (in the same or other NAS instances)
  • On Windows Task Scheduler with codeunit invoked by Powershell

 

Fields Added on Table “Job Queue Entry”

 

Codeunit 50000 “ChangeJobsStatus”

CODE

OnRun()

ChangeJobStatus;

ChangeJobStatus()

Documentation()

“Restart Job” IF in errorin processfinished state

OnRun()

IF ChangeJobStatus() = TRUE THEN

MESSAGE(‘Jobs Status Changed!’)

ELSE

MESSAGE(‘Nothing Changed!’)

LOCAL ChangeJobStatus() JobStatusChanged : Boolean

//”Restart Job” IF in errorin processfinished state

JobStatusChanged := FALSE;

recJobQEntry.RESET;

IF recJobQEntry.FINDSET THEN

REPEAT

CASE recJobQEntry.Status OF

//*** STATE: IN PROCESS

recJobQEntry.Status::”In Process”:

BEGIN

recJobQLogEntry.RESET;

recJobQLogEntry.SETRANGE(“Object Type to Run”,recJobQEntry.”Object Type to Run”); //Check Run.Time

recJobQLogEntry.SETRANGE(“Object ID to Run”,recJobQEntry.”Object ID to Run”);

//Check Duration Process Max

IF recJobQEntry.”Duration Process Max” > 0 THEN

IF recJobQLogEntry.FINDLAST THEN

IF CURRENTDATETIME – recJobQLogEntry.”Start Date/Time” > recJobQEntry.”Duration Process Max” THEN BEGIN //CHECK MAX DURATION TIME

recJobQEntry.Status := recJobQEntry.Status::Ready; // –> SET STATUS TO READY

recJobQEntry.MODIFY(FALSE);

JobStatusChanged := TRUE;

END

END;

//*** STATE: ERROR OR FINISHED

recJobQEntry.Status::Error, recJobQEntry.Status::Finished: //IF “ERROR” OR “FINISHED”

BEGIN

recJobQEntry.Status := recJobQEntry.Status::Ready; // –> SET STATUS TO READY

recJobQEntry.MODIFY(FALSE);

JobStatusChanged := TRUE;

END;

END; //END CASE

COMMIT;

UNTIL recJobQEntry.NEXT = 0;

EXIT(JobStatusChanged);

Download from MSDN https://gallery.technet.microsoft.com/How-to-restart-a-job-in-8aca5516

 

About Scheduling

1) With NAV Codeunit scheduled by NAS

After Execution

2) With NAV Codeunit invoked by Powershell and scheduled by Windows Task Scheduler

Import-Module ‘C:Program FilesMicrosoft Dynamics NAV100ServiceMicrosoft.Dynamics.Nav.Management.dll’

Invoke-NAVCodeunit dynamicsnav100 -CodeunitId 50000 -CompanyName ‘CRONUS Italia S.p.A.’

To Schedule Powershell Script by Windows Task Scheduler

An easy way to schedule a PowerShell script is, to use Windows Task Scheduler, look at this post:

Coffee Break: Proactive Monitoring with Task Scheduler

https://blogs.msdn.microsoft.com/nav/2016/01/19/coffee-break-proactive-monitoring-with-task-scheduler/

My Old Post about NAV 2017 Task Scheduler

https://robertostefanettinavblog.com/2016/11/04/nav-2017-task-scheduler/

Advertisements

OnDemand NAS Codeunits Scheduling


OnDemand NAS Codeunits Scheduling

With a little modification of standard NAV “Scheduling Report Page”, is possible to schedule also codeunits !

You don’t need IT department to schedule you applications handled by codeunits (ex: interfaces, intercompany entries, copy company functions etc.), user can schedule codeunits in his NAS session without use of Process Queue management console. Some standard codeunits have HideDialog functions defined, you can use these functions to bypass client input parameters (es: confirm messages etc.), you are in NOT GUIALLOWED mode with NAS.

Examples of utilizations:

  • we use OnDemand schedule to launch OnDemand Web Services synchronizations and multicompanies data deployments, we won’t use reports to do this, and we need to handle transactions with great rollback by design (codeunits).

Example

OnDemand schedule of codeunit “99000853 Calc. Low-level code”, that require user input confirmation (Confirm)

Modifications:

Codeunit 99000853 Calc. Low-level code

OnRun()

// Set Hidedialogs TRUE for NAS execution (NAV Service and NAS for user sessions)

IF NOT GUIALLOWED THEN

SetHideDialogs(TRUE);

SetHideDialogs(NewHideDialogs : Boolean)

HideDialogs := NewHideDialogs;

//Standard HideDialogs Function

Custom Page 50001 “Schedule a Codeunit”  (new page from “Schedule a Report” Page 682)

Triggers and functions

OnOpenPage()

IF NOT FINDFIRST THEN BEGIN

INIT;

ReportEditable := TRUE;

OutPutEditable := TRUE;

Status := Status::”On Hold”;

VALIDATE(“Object Type to Run”,”Object Type to Run”::Codeunit);

INSERT(TRUE);

END ;

Object ID to Run – OnLookup(VAR Text : Text) : Boolean // Show Codeunits List

IF LookupObjectID(NewObjectID) THEN BEGIN

Text := FORMAT(NewObjectID);

EXIT(TRUE);

END;

EXIT(FALSE);

ScheduleACodeunit(CodeunitId : Integer;RequestPageXml : Text) : Boolean

ScheduleACodeunit.SetParameters(CodeunitId,RequestPageXml);

EXIT(ScheduleACodeunit.RUNMODAL = ACTION::OK);

SetParameters(ReportId : Integer;RequestPageXml : Text)

INIT;

Status := Status::”On Hold”;

VALIDATE(“Object Type to Run”,”Object Type to Run”::Codeunit);

VALIDATE(“Object ID to Run”,ReportId);

INSERT(TRUE);

Example
OnDemand Schedule for codeunit 99000853 “Low Level Code Calculation”

  • Select codeunit from list

After Selection

  • Validate Codeunit
  • Insert Scheduling Timing

Check in Process Queue entries

  • You can find your OnDemand Schedule

Check NAS Log

  • Completed !

Download Txt Page from here:  Page_50001

You are Welcome !