title | description | ms.date | ms.topic | author |
---|---|---|---|---|
Job queue |
Learn about how the job queue works |
12/18/2023 |
conceptual |
jswymer |
This article describes how the job queue works in [!INCLUDEprod_short]. A job queue is basically an abstraction that uses the task scheduler from the platform to enable end users to view, create, or modify jobs that are set to run in the background. These jobs can be set to run on a recurring schedule.
For information about how users work with the job queue in the client, see Use job queues to schedule tasks.
[!INCLUDEasync_note]
The following diagram illustrates the flow of the job queue:
This section describes the flow that a job queue goes through.
A job is run when the task scheduler's task is run. For more information, see Task scheduler - detailed flow.
Here's a general overview of the process:
- When a job queue is created and set to ready state, a scheduled task is created to run, but not before the Earliest Start Date/Time.
- When the scheduled task is picked up by the task scheduler to run, a new background session is started.
- The background session is run by the Job Queue Dispatcher codeunit:
-
It first checks whether the job should be run or rescheduled.
If the job shouldn't run, it's deleted. Or, in the case of an already running "Category Code", it's rescheduled.
-
The job queue entry is updated to the In-Progress state, and a job queue log entry is created.
-
The specified Object ID to Run is then started.
If an exception occurs, none of the subsequent steps in the Job Queue Dispatcher path will be run, but instead, the failure codeunit path will be run.
-
- The failure codeunit path runs as follows:
- An exception is raised and surfaced.
- The Job Queue Error Handler codeunit is run in a new background session.
- The job queue entry is updated to the Error state.
- The errors are saved by using Error Message Management.
- The job queue entry and job queue log entry are updated with the error.
- The job queue then either stays in the Error state or is rescheduled:
- It stays in Error state if the Maximum No. of Attempts value has been exceeded and it's not a recurring job.
- It's rescheduled if the Maximum No. of Attempts value hasn't been exceeded and it's a recurring job.
When running a codeunit with the job queue, you must declare the OnRun()
trigger, as this defines the code, which the underlying task scheduler will run. The same applies for the failure codeunit.
codeunit 50142 MyCodeunitToBeRunFromTheJobQueue
{
trigger OnRun()
var
// declare your variables
begin
// code to be run. Probably calling other methods
end;
}
[!INCLUDEgui_allowed]
If you want to do multiple things with a single codeunit using the job queue, consider using a parameter. To achieve this, define the TableNo
property and set it to "Job Queue Entry" on the codeunit, then you'll be able to access the parameter string defined on the job queue entry from the codeunit.
codeunit 50143 MyCodeunitToBeRunFromTheJobQueueNowWithParameters
{
TableNo = "Job Queue Entry";
trigger OnRun()
var
// declare your variables
begin
if Rec."Parameter String" <> '' then
begin
// code to be run. Probably calling other methods
end
end;
}
When running codeunits in the job queue, you must make sure that the AL code doesn't assume the ability to interact with a user through the UI. You can use the GuiAllowed Method to suppress UI interactions.
[!INCLUDEcallback_exception_no_ui_note]
[!INCLUDEcallback_exceptions_no_ui]
The session runs using the same user/credentials that are used when calling AL code. The user that's used is the user that sets the job to ready state. The user must have appropriate permissions to run the job queue and any other objects that are associated with the operation of the specified object.
For more information about assigning permissions, see Assign Permissions to Users and Groups in the business functionality help.
[!INCLUDEjob_queue_performance]
[!INCLUDEprod_short] offers two ways to monitor the flow of job queues: Azure Application Insights and the Session Event table. These tools let you follow the execution of a job and investigate errors in failure codeunits.
You can set up [!INCLUDEprod_short] to send telemetry traces to an [!INCLUDEazure-appinsights-name] resource in Azure. Once set up, telemetry data will be sent to the resource as job queue moves through the flow. For more information, see:
Enable Sending Telemetry to Application Insights
Note
Job queue telemetry in Azure Application Insights also works for [!INCLUDEprod_short] on-premises
[!INCLUDEjob_queue_telemetry]
[!INCLUDEjob_queue_telemetry_events]
For more information about job queue telemetry, see Analyzing Job Queue Telemetry
[!INCLUDEtask_scheduler_telemetry]
For more information about task scheduler telemetry, see Analyzing Task Scheduler Telemetry
From the [!INCLUDEprod_short] web client, you can open the Session Events table by adding table=2000000111
to the URL. For example: https://businesscentral.dynamics.com/?table=2000000111.
[!INCLUDEjobqueue-and-task-scheduler-characteristics]
Use Job Queues to Schedule Tasks
Analyzing Job Queue Telemetry
Task Scheduler
Async processing overview
Performance Articles for Developers
Developing Extensions
Get Started with AL