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 !

ABOUT NAV 2016 NAS AND REPORTS SCHEDULING


ABOUT NAV 2016 NAS AND REPORTS SCHEDULING

A little “how-to” post about NAV NAS Configuration and Report Scheduling, below some info, links and some screens for simple configuration.

NAV 2016 NAS

– “NAS services are a middle-tier server component that executes business logic without a user interface or user interaction. NAS services in Microsoft Dynamics NAV Server support applications such as Microsoft Office Outlook Integration and the NAV Job Queue.

– “NAS” stands for NAV Application Server, which was a component in earlier versions of Microsoft Dynamics NAV. NAS services replace the NAS component, and this requires you to modify any use of server-side COM.”

…nice posts\pages about NAS below:

NAV Help on MSDN Online

“Configuring NAS Services”

https://msdn.microsoft.com/en-us/library/dd355183(v=nav.90).aspx

About Job Queues

https://msdn.microsoft.com/en-us/library/hh879489(v=nav.90).aspx

How to: Set Up Job Queues

https://msdn.microsoft.com/en-us/library/hh879053(v=nav.90).aspx

How to: Create Job Queue Categories

https://msdn.microsoft.com/en-us/library/hh179528(v=nav.90).aspx

Video: How Do I: Implement the Job Queue Entry Pattern

https://msdn.microsoft.com/de-de/dynamics/nav/dn864863

“Background Posting and Job Queue in Microsoft Dynamics NAV 2013” by NAV Team Blog, old but nice post about background posting.

http://blogs.msdn.com/b/nav/archive/2012/08/31/background-posting-and-job-queue-in-microsoft-dynamics-nav-2013.aspx

…some screens about “NAS CONFIGURATION AND SETUP”

NAS SERVER SETUP

RETRY ATTEMPS PER DAY

RUN NAS WITH ADMIN RIGHTS

“Specifies whether NAS services run operations with administrator rights instead of the rights granted to the Microsoft Dynamics NAV Server service account.”

  • If you select this setting, NAS services will have full permissions in Microsoft Dynamics NAV, similar to the permissions that are granted by the SUPER permission set. The Microsoft Dynamics NAV Server service account is not required to be set up as a user in Microsoft Dynamics NAV.
  • If you clear this setting, the Microsoft Dynamics NAV Server service account must be added as a user in Microsoft Dynamics NAV and assigned the permissions that are required to perform the operations.

STARTUP ARGUMENT

“Specifies a string argument that will be used when NAS services start. The argument typically specifies an application type, sometimes with additional configuration information.” Example values: "OSYNCH""JOBQUEUE"

STARTUP CODEUNIT

Specifies the codeunit that contains the method that will be called by the NASStartupMethod setting.

Example values:

0  >  When NASStartupCodeunit is set to 0, NAS Services do not start. This is the default value.

1When NAS services start, they run the trigger specified by the NAS Startup Method in codeunit 1

450  >  When NAS services start, they run codeunit 450 (Job Queue – NAS Start Up), and call the method specified by NASStartupMethod.

STARTUP METHOD

Specifies the method that will be called in the NASStartupCodeunit.

Example values:

""  >  If no start method is specified (null string), the OnRun trigger is called.

StartNAS  >  NAS services runs the StartNAS method in the NAS Startup Codeunit.

NB: NAV Help is incorrect for options STARTUP CODEUNIT\STARTUP METHOD

Microsoft: “The reason being change in NAV 2016, in the Codeunit 1 there no NAShandler function anymore, it has shifted to Codeunit 450. If you still define Codeunit 1, then there has to be code defined in Codeunit 1, or if you define any other codeunit then you provide it here as you know this already. This is BUG in the documentation about configuration parameters, will be fix soon.”

NAV NAS CONFIGURATION

LISTA CATEGORIE CODE PROCESSI

CODE PROCESSI

MOVIMENTI CODA PROCESSI

MOVIMENTI LOG CODA PROCESSI

DEFINE A NAS PROCESS QUEUE : “EX NOTIFICA”

notidoca

EX: NOTIFICATION ENTRY DISPATCHER CARD

notify 2

ABOUT REPORT SCHEDULING

  • You can send a report in process scheduling using “ScheduleProgrammazione” option
  • Schedule option is available both for RDLC and Word Layout Reports

EX: LISTA CLIENTI TOP 10

 

REPORTS ELABORATI IN “REPORT ELABORATI”

… to open a report > “Click on link” !

…my old post about NAV 2013 NAS configuration here:

http://blogs.dotnethell.it/dynamicsnav/Configuring-NAS-to-use-Job-Queue__19619.aspx

“Check NAS” Process execution and Send Mail (Batch control)


“Check NAS” Process execution & Send Mail (Batch control)

How System Works

> System every “X minutes” check Status of Scheduled Processes and send a mail with Status (batch control)

Scheduling

> You can schedule this codeunit in some or in another Process Category Queue of your NAS; you can use also another separate NAS Server if you need parallelasynchronous check.

 

Codeunit “Check NAS” C/AL Code

Documentation

RS, NAS01, 20150710, “Check NAS” Codeunit

*** Function “CreaFileErrori

*** Function “ErrorSendMail

*** Function “JobStatus

OnRun()

//RS, NAS01, .sn

//Call Create Errors File

CreaFileErrori;

//RS, NAS01, .en

JobStatus()

//RS, NAS01, .sn

// Check “Job Log Entry” Process Status & Execution Time

IF recJobQEntry.FINDSET THEN

REPEAT //controllo se è in esecuzione da troppo tempo (check for LONG running time execution)

recJobQEntry.”Send error email” := FALSE;

IF (recJobQEntry.Status = recJobQEntry.Status::”In Process”) THEN

BEGIN

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

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

IF recJobQEntry.”Duration Process Max” > 0 THEN

IF recJobQLogEntry.FINDLAST THEN

IF CURRENTDATETIME – recJobQLogEntry.”Start Date/Time” > recJobQEntry.”Duration Process Max” THEN

recJobQEntry.”Send error email” := TRUE;

END

ELSE //se è in errore , IF process Is in “Error” State

IF (recJobQEntry.Status = recJobQEntry.Status::Error) THEN

recJobQEntry.”Send error email” := TRUE;

recJobQEntry.MODIFY;

UNTIL recJobQEntry.NEXT = 0;

//RS, NAS01, .en

ErrorSendEmail(txtFile01 : Text[100])

//RS, NAS01, .sn

//————— Build & Send Mail ———————-//

recSMTP.GET;

cuSendEmail.CreateMessage(recSMTP.”User ID”,

recSMTP.”Sender Address”,

recSMTP.”Receiver Address Log Interface”,

‘Errori Job Schedulate’,’In allegato i job in errore’,

FALSE);

//Add attachment

cuSendEmail.AddAttachment(txtFile01);

//Send attachment

cuSendEmail.Send;

//RS, NAS01, .en

CreaFileErrori()

//RS, NAS01 .sn

// Registrazione Errori NAS, Wrinting NAS Errors in Outpout File

GLOBALLANGUAGE(1040);

CLEAR(OutFile);

OutFile.TEXTMODE := TRUE;

OutFile.WRITEMODE := TRUE;

recSMTP.GET;

txtFileName_00 := recSMTP.”Error Folder Log” + recSMTP.”Error File Log”

+ DELCHR((FORMAT(TODAY,0,'<Year4><Month,2><Day,2>’) +’_’+ FORMAT(TIME)), ‘=’,’/ : .’) + ‘.csv’;

IF OutFile.OPEN(txtFileName_00) THEN

BEGIN

OutFile.CLOSE;

EXIT;

END;

OutFile.CREATE(txtFileName_00);

txtFineRiga:='<CRLF>’;

cr:=13;

lf:=10;

recJobQEntry.SETRANGE(“Send error email”,TRUE);

IF  recJobQEntry.FINDSET THEN

BEGIN

REPEAT

txtOutText:= ‘TIPO;’ + ‘NR JOB;’ + ‘JOB;’;

OutFile.WRITE(txtOutText); //scrivo riga

txtOutText:=FORMAT(FORMAT(recJobQEntry.”Object Type to Run”)+ ‘;’ +

FORMAT(recJobQEntry.”Object ID to Run”)+ ‘;’ +

recJobQEntry.Description, -250);

OutFile.WRITE(txtOutText); //scrivo riga , Write Line

txtOutText := ‘MESSAGGIO;’;

OutFile.WRITE(txtOutText); //scrivo riga, Write Line

OutFile.WRITE(FORMAT(recJobQEntry.”Error Message”,-250)); //scrivo riga

OutFile.WRITE(FORMAT(recJobQEntry.”Error Message 2″,-250)); //scrivo riga

OutFile.WRITE(FORMAT(recJobQEntry.”Error Message 3″,-250)); //scrivo riga

OutFile.WRITE(FORMAT(recJobQEntry.”Error Message 4″,-250)); //scrivo riga

CLEAR(txtOutText);

UNTIL  recJobQEntry.NEXT = 0;

END

ELSE

BEGIN

txtOutText:=’Nessun Errore’;

OutFile.WRITE(txtOutText);

END;

OutFile.CLOSE;  //chiudofile

// INVIO EMAIL CON ALLEGATI, SEND MAIL WITH ATTACHMENTS

ErrorSendEmail(txtFileName_00);

//RS, NAS01 .en

CODEUNIT SCHEDULING SAMPLE

Schedule Codeunit on NAS (example every 60 minutes)

nav 1

“SMTP MAIL Setup” Table with New Fields

smtp

New Fields to insert:

  • Field: File Attachment
  • Field: Patch Attachment
  • Field: Receiver Address
  • Field: VBScript Folder
  • Field: Receiver Address Log Interface
  • Field: Error Folder Log
  • Field: Receiver Warning
  • Field: Error File Log
  • Field: Sender Name
  • Field: Sender Address

RUN TABLE AND ENTER DATA IN NEW FIELDS (add fields on your Standard NAV Page)

nas 4

File Directory

  • Files are stored in “\NavisionShared\Log” > you can change by Setup

nas 6

Error File Sample NavJob20150706_170544”  FILE DETAILS

nas 7