User Defined Notification Handler for Scheduler Jobs
Database Scheduler provides an email notification mechanism to track the status of periodically running or automated jobs. In addition to this, the Database Scheduler also supports user-defined PL/SQL Scheduler job notification handler procedure.
Adding a scheduler job notification handler procedure allows you to monitor scheduled or automated jobs running in your Autonomous AI Database.
About User Defined Notification Handler for Scheduler Jobs
The Database Scheduler supports job notification handler procedure that can make use of custom code to call HTTP or REST endpoints for improved monitoring of scheduler jobs in an Autonomous AI Database instance.
The handler procedure receives all pertinent information regarding the job, such as the job owner’s name, class name, event type, and timestamp in JSON format. Based on the information, the handler procedure then takes the required action.
See DBMS_SCHEDULER for more information on Oracle Scheduler.
Configuring user defined notification handler for scheduler jobs consists of these steps:
-
Create a job notification handler procedure as described in:Create a Job Notification Handler Procedure.
-
Register the job notification handler procedure for the database as described in:Register the Job Handler Notification Procedure.
-
Trigger the job notification handler procedure as described in:Trigger the Job Handler Notification Procedure
-
De-Register the job notification handler procedure for the database as described in:De-Register the Job Handler Notification Procedure.
Create a Job Notification Handler Procedure
Provides steps to create a job notification handler.
-
Create a credential object.
See CREATE_CREDENTIAL Procedure for more information.
See Specifying Scheduler Job Credentials for more information.
-
Create a Job notification handler:
Example to create a job notification handler procedure to send a message to a Slack channel:
CREATE OR REPLACE PROCEDURE ADMIN.SEND_NOTIFICATION(data_in CLOB) AS BEGIN DBMS_CLOUD_NOTIFICATION.SEND_MESSAGE ( provider => 'slack', credential_name => 'SLACK_CRED', message => data_in, params => JSON_OBJECT('channel' value 'adw-job-nfy'); end; /This example creates the
SEND_NOTIFICATIONprocedure.This user defined procedure sends the provided input data as a message to the specified Slack channel.
See Send Slack Notifications from Autonomous AI Database for more information.
See SEND_MESSAGE Procedure for more information.
Example to create a job notification handler procedure to insert the message into a table:
CREATE TABLE ADMIN.JOB_STATUS(jnfy_data CLOB); CREATE OR REPLACE PROCEDURE ADMIN.INSERT_JOB_STATUS(data_in CLOB) AS l_sessuser VARCHAR2(128) := SYS_CONTEXT('userenv','session_user'); BEGIN INSERT INTO ADMIN.JOB_STATUS (jnfy_data) VALUES (data_in || TO_CLOB(' : Sent By Session User : ' || l_sessuser)); COMMIT; END; /This example creates the
JOB_STATUStable andINSERT_JOB_STATUSprocedure to insert the session-specific values into the table.You must be logged in as the ADMIN user or have
CREATE ANY PROCEDUREsystem privilege to create a job notification handler procedure.Note: An
ORA-27405is returned when you specify an invalid owner or object name as the job notification handler procedure.Existing
DBMS_SCHEDULERproceduresADD_JOB_EMAIL_NOTIFICATIONandREMOVE_JOB_EMAIL_NOTIFICATIONare enhanced to support the job notification handler procedure.See ADD_JOB_EMAIL_NOTIFICATION Procedure and REMOVE_JOB_EMAIL_NOTIFICATION Procedure for more information.
Use
DBA_SCHEDULER_NOTIFICATIONSdictionary view to query the list of notifications for a scheduler job. See DBA_SCHEDULER_NOTIFICATIONS for more information.
Register the Job Handler Notification Procedure
Use DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE procedure to set JOB_NOTIFICATION_HANDLER attribute value to register the job handler notification procedure.
The JOB_NOTIFICATION_HANDLER attribute specifies the job handler notification procedure that you want to use.
To register the job handler notification procedure you must:
-
Be logged in as the ADMIN user or have
MANAGE SCHEDULERprivilege. -
Have
EXECUTEprivilege on the handler procedure orEXECUTE ANY PROCEDUREsystem privilege.
The JOB_NOTIFICATION_HANDLER attribute and EMAIL_SERVER attribute are mutually exclusive. The ATTRIBUTE parameter of the SET_SCHEDULER_ATTRIBUTE procedure can have either the JOB_NOTIFICATION_HANDLER or the EMAIL_SERVER value at a time. You are allowed to either configure email notifications or create your notification handler for your scheduler jobs.
An ORA-27488 error is raised when you attempt to set both EMAIL_SERVER and JOB_NOTIFICATION_HANDLER global attributes.
Execute DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE procedure to register the job handler notification procedure:
BEGIN
DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE('job_notification_handler','ADMIN.SEND_NOTIFICATION');
END;
/
This example registers the ADMIN.SEND_NOTIFICATION procedure as the job handler notification procedure for your database.
See SET_SCHEDULER_ATTRIBUTE Procedure for more information.
Execute this command to verify the job notification handler:
SELECT value FROM dba_scheduler_global_attribute WHERE attribute_name='JOB_NOTIFICATION_HANDLER';
VALUE
---------------
"ADMIN"."SEND_NOTIFICATION"
See DBA_SCHEDULER_GLOBAL_ATTRIBUTE for more information.
You must assign EXECUTE privilege to allow other users to use the job notification handler. For example:
GRANT EXECUTE ON ADMIN.SEND_NOTIFICATION To DWUSER;
ORA-27476 (""%s"."%s" does not exist") or ORA-27486 ("insufficient privileges") error is thrown if you do not have privilege on the job handler notification procedure.
Trigger the Job Handler Notification Procedure
You must call the DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION procedure to trigger the user defined job notification handler procedure.
The overloaded form of the DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION enables you to trigger the job notification handler procedure and send a notification. However, these notifications are not sent out in the form of an email when you have registered the job notification handler procedure. Hence, the parameters SUBJECT, and BODY are optional. The RECIPIENT parameter is still mandatory. Since this overloaded form of procedure is not sending email notifications so, you can provide any string value for the RECIPIENT parameter.
For example, the following steps create a scheduler job, add notification for the job, enable the job, verify the notification entries, show the data received by the job notification handler procedure, and remove notification for the job.
- Create a scheduler job in the
DWUSERschema:BEGIN DBMS_SCHEDULER.CREATE_JOB( job_name => 'DWUSER.MY_JOB', job_type => 'PLSQL_BLOCK', job_action => 'BEGIN null; END;', enabled => FALSE, auto_drop => FALSE); END; /This creates a job
DWUSER.MY_JOBwith the specified attributes.See CREATE_JOB Procedure for more information.
-
Configure the job to send notifications at specified events.
BEGIN DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION( job_name => 'DWUSER.MY_JOB', recipients => 'PLACEHOLDER_STRING', subject => 'Job Notification-%job_owner%.%job_name%-%event_type%', body => '%event_type% occurred at %event_timestamp%. %error_message%', events => 'job_started, job_succeeded, job_completed'); END; /This procedure adds notifications for the
DWUSER.MY_JOBjob. The notifications are sent whenever any of the specified job state events is raised.See ADD_JOB_EMAIL_NOTIFICATION Procedure for more information.
-
Enable the scheduler job.
EXEC DBMS_SCHEDULER.ENABLE('DWUSER.MY_JOB');See ENABLE Procedure for more information.
When you enable the
DWUSER.MY_JOBjob, theUSER_SCHEDULER_NOTIFICATIONSview is populated with the job notification entries. To verify you can query theUSER_SCHEDULER_NOTIFICATIONSview. For example:SELECT job_name, recipient, event, subject, body FROM user_scheduler_notifications ORDER BY notification_owner, owner, job_name; JOB_NAME RECIPIENT EVENT SUBJECT BODY ------- ------------------ ------------- --------------- --------------- MY_JOB placeholder_string JOB_STARTED Job Notificatio %event_type% oc n-%job_owner%.% curred at %even job_name%-%even t_timestamp%. % t_type% error_message% MY_JOB placeholder_string JOB_SUCCEEDED Job Notificatio %event_type% oc n-%job_owner%.% curred at %even job_name%-%even t_timestamp%. % t_type% error_message% MY_JOB placeholder_string JOB_COMPLETED Job Notificatio %event_type% oc n-%job_owner%.% curred at %even job_name%-%even t_timestamp%. % t_type% error_message%See USER_SCHEDULER_NOTIFICATIONS View for more information.
When the job
DWUSER.MY_JOBis executed and any of the specified job state events is raised, the job notification handler procedure is triggered and receives the specified information as input. For example, theADMIN.SEND_NOTIFICATIONjob notification handler procedure receives the following:{"job_owner":"DWUSER","job_name":"MY_JOB","job_class_name":"DEFAULT_JOB_CLA SS","event_type":"JOB_STARTED","event_timestamp":"12-JAN-23 08.13.46.193306 PM UTC","error_code":0,"error_msg":null,"sender":null,"recipient":"data_lo ad_pipeline","subject":"Job Notification-DWUSER.MY_JOB-JOB_STARTED","msg_te xt":"JOB_STARTED occurred at 12-JAN-23 08.13.46.193306 PM UTC. ","comments" :"User defined job notification handler"} {"job_owner":"DWUSER","job_name":"MY_JOB","job_class_name":"DEFAULT_JOB_CLA SS","event_type":"JOB_SUCCEEDED","event_timestamp":"12-JAN-23 08.13.46.2863 44 PM UTC","error_code":0,"error_msg":null,"sender":null,"recipient":"data_ load_pipeline","subject":"Job Notification-DWUSER.MY_JOB-JOB_SUCCEEDED","ms g_text":"JOB_SUCCEEDED occurred at 12-JAN-23 08.13.46.286344 PM UTC. ","com ments":"User defined job notification handler"} -
Remove the job notification. For example:
EXEC DBMS_SCHEDULER.REMOVE_JOB_EMAIL_NOTIFICATION('DWUSER.MY_JOB');See REMOVE_JOB_EMAIL_NOTIFICATION Procedure for more information.
De-Register the Job Handler Notification Procedure
Use DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE to de-register the job handler notification procedure.
To de-register the job handler notification procedure you must be logged in as the ADMIN user or have MANAGE SCHEDULER privilege.
Example to de-register the job handler notification procedure:
BEGIN
DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE ('job_notification_handler','');
END;
/