Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | jessamine-french |
View: | 59 times |
Download: | 2 times |
17Copyright © 2004, Oracle. All rights reserved.
Automating Tasks with the Scheduler
Copyright © 2004, Oracle. All rights reserved.17-2
Objectives
After completing this lesson, you should be able to:• Simplify management tasks by using the
Scheduler• Create a job, program, schedule, and window• Reuse Scheduler components for similar tasks• View information about job executions and job
instances
Copyright © 2004, Oracle. All rights reserved.17-3
Scheduling Needs
Create month-end report on the last day of each month
Compute table andindex statistics
twice a day
Replicate table datavia materializedview refreshes
Generate daily report on invalid server access attempts
Start the batch load at 4:00 a.m.
Check for queued events every 10 minutes
Run daily job to backup
database
Copyright © 2004, Oracle. All rights reserved.17-4
Scheduler Concepts
JobProgram
Arguments Arguments
Schedule
Job class Window
Resource plan
Window group
ResourceConsumer Group
Copyright © 2004, Oracle. All rights reserved.17-6
Privileges for Scheduler Components
CREATE [ANY] JOBEXECUTE ANY PROGRAMEXECUTE ANY CLASSMANAGE SCHEDULER
EXECUTE ON <program or class>ALTER ON <job, program, or schedule>ALL ON <job, program, schedule, or class>
System and object privileges SCHEDULER_ADMIN role
CREATE JOB
CREATE ANY JOB
EXECUTE ANY PROGRAM
EXECUTE ANY CLASS
MANAGE SCHEDULER
Copyright © 2004, Oracle. All rights reserved.17-8
Creating a Scheduler Job
There are many ways to create a job:
• Specifying the components in-line as part of the CREATE_JOB procedure
• Specifying the task directly and using a saved schedule
• Calling a saved program and a saved schedule
• Calling a saved program and specifying the schedule directly
Copyright © 2004, Oracle. All rights reserved.17-9
Creating a Scheduler Job: Example
Create a job that calls a backup script every night at 23:00 (starting tonight).
BEGIN DBMS_SCHEDULER.CREATE_JOB( job_name=>'HR.DO_BACKUP', job_type => 'EXECUTABLE', job_action =>'/home/usr/dba/rman/nightly_incr.sh', start_date=>TRUNC(SYSDATE)+23/24, repeat_interval=>'TRUNC(SYSDATE+1)+23/24', /* next night at 11:00 PM */ comments => 'Nightly incremental backups');END;/
Copyright © 2004, Oracle. All rights reserved.17-10
Setting the Repeat Interval for a Job
• Using a calendaring expression:
• Using a datetime expression:
repeat_interval=> 'FREQ=HOURLY; INTERVAL=4'repeat_interval=> 'FREQ=DAILY'repeat_interval=> 'FREQ=MINUTELY; INTERVAL=15'repeat_interval=> 'FREQ=YEARLY; BYMONTH=MAR,JUN,SEP,DEC; BYMONTHDAY=15'
repeat_interval=> 'SYSDATE + 36/24'repeat_interval=> 'SYSDATE + 1'repeat_interval=> 'SYSDATE + 15/(24*60)'
Copyright © 2004, Oracle. All rights reserved.17-11
Calendaring Expressions
YEARLY
MONTHLY
WEEKLY
DAILY
HOURLY
MINUTELY
SECONDLY
INTERVAL(1-999)
BYMONTH
BYWEEKNO
BYYEARDAY
BYMONTHDAY
BYDAY
BYHOUR
BYMINUTE
BYSECOND
Frequency Interval Specifiers
Copyright © 2004, Oracle. All rights reserved.17-12
Using Scheduler Programs
BEGIN DBMS_SCHEDULER.CREATE_PROGRAM( program_name => 'CALC_STATS2', program_action => 'HR.UPDATE_SCHEMA_STATS', program_type => 'STORED_PROCEDURE', enabled => TRUE);
DBMS_SCHEDULER.CREATE_JOB( job_name=>'HR.GET_STATS2', program_name=>'HR.CALC_STATS2', start_date=>'20-DEC-04 07.00.00 AM Greenwich', repeat_interval=> 'FREQ=HOURLY;INTERVAL=2', end_date => '20-DEC-05 07.00.00 AM Greenwich', comments => 'Explicitly scheduled job');END;/
Copyright © 2004, Oracle. All rights reserved.17-13
Creating a Program Using EM
Copyright © 2004, Oracle. All rights reserved.17-14
Specifying Schedules for a Job
Example schedules:
• Wednesday, December 26, 2002, at 2:00 p.m.
• Every fourth Thursday
• Every Monday at 8:00 a.m., starting on January 29, 2004, and ending on March 31, 2004
ScheduleStarttime
End timeor limit
One Timejob
Repeatingjob
Copyright © 2004, Oracle. All rights reserved.17-15
Creating and Using Schedules
BEGIN DBMS_SCHEDULER.CREATE_SCHEDULE( schedule_name => 'stats_schedule', start_date => SYSTIMESTAMP, end_date => SYSTIMESTAMP + 30, repeat_interval => 'FREQ=HOURLY;INTERVAL=4', comments => 'Every 4 hours');
DBMS_SCHEDULER.CREATE_JOB( job_name => 'HR.GET_STATS', program_name => 'HR.CALC_STATS2', schedule_name => 'STATS_SCHEDULE');END;/
Copyright © 2004, Oracle. All rights reserved.17-16
Using EM to Create Schedules
Copyright © 2004, Oracle. All rights reserved.17-17
Advanced Scheduler Concepts
JobProgram
Arguments Arguments
Schedule
Job class Window
Resource plan
Window group
ResourceConsumer Group
Copyright © 2004, Oracle. All rights reserved.17-18
Creating a Job Class
Copyright © 2004, Oracle. All rights reserved.17-19
Creating a Job Class
EXECUTE DBMS_SCHEDULER.CREATE_JOB_CLASS( - job_class_name => 'ADMIN_JOBS', - resource_consumer_group => 'DAYTIME_JOBS', - logging_level => DBMS_SCHEDULER.LOGGING_OFF);
Job Class
Resource Consumer
GroupService
Log History
Logging Level
Copyright © 2004, Oracle. All rights reserved.17-20
Job Logging
• By default, all job runs are logged.
• The amount of information logged can be controlled at the job class level.
EXECUTE DBMS_SCHEDULER.CREATE_JOB_CLASS( - job_class_name => 'ADMIN_JOBS', - resource_consumer_group => 'DAYTIME_JOBS', - logging_level => DBMS_SCHEDULER.LOGGING_RUNS, - log_history => 30);
Copyright © 2004, Oracle. All rights reserved.17-21
Creating a Window
Create a window for the month of December that uses the END_OF_YEAR resource plan and is active every night from 6:00 p.m. to 6:00 a.m. Eastern Standard Time (EST).
BEGIN DBMS_SCHEDULER.CREATE_WINDOW( window_name => 'DEC_NIGHTS', resource_plan => 'END_OF_YEAR', start_date => '01-DEC-04 06.00.00 PM EST', repeat_interval => 'FREQ=DAILY; BYHOUR=18', duration => '0 12:00:00', end_date => '31-DEC-04 06.00.00 AM EST', comments => 'Every day at 6:00 PM');END;/
Copyright © 2004, Oracle. All rights reserved.17-23
Prioritizing Jobs Within a Window
Daytime Window
APPL_JOBS
Job1 Job2
ADMIN_JOBS
Job4 Job5
OTHER
Job3
Job Priority
Job1 1
Job2 5
Job3 3
Job4 4
Job5 2
Copyright © 2004, Oracle. All rights reserved.17-25
Enabling and Disabling Scheduler Components
• Enable the CALC_STATS2 program:
• Disable the GET_STATS job:
EXEC DBMS_SCHEDULER.ENABLE('HR.CALC_STATS2');
EXEC DBMS_SCHEDULER.DISABLE('HR.GET_STATS');
Copyright © 2004, Oracle. All rights reserved.17-26
Managing Jobs
• Run a job:
• Stop a job:
• Drop a job, even if it is currently running:
DBMS_SCHEDULER.RUN_JOB('HR.JOB1');
DBMS_SCHEDULER.STOP_JOB('HR.JOB2');
DBMS_SCHEDULER.DROP_JOB('HR.JOB1,HR.JOB2, SYS.JOBCLASS1');
Copyright © 2004, Oracle. All rights reserved.17-27
Managing Programs
• Enabling a program:
• Disabling a program:
• Dropping a program:
EXECUTE DBMS_SCHEDULER.DROP_PROGRAM( - program_name => 'HR.PROG1', - force => FALSE );
EXECUTE DBMS_SCHEDULER.ENABLE( - 'HR.PROG1,HR.PROG2');
EXECUTE DBMS_SCHEDULER.DISABLE( - 'HR.PROG1,HR.PROG2');
Copyright © 2004, Oracle. All rights reserved.17-28
Managing Programs with EM
Copyright © 2004, Oracle. All rights reserved.17-29
Managing Schedules
• CREATE_SCHEDULE• SET_ATTRIBUTE (to alter a schedule)
• DROP_SCHEDULE
EXEC DBMS_SCHEDULER.DROP_SCHEDULE( - schedule_name => 'HR.STATS_SCHEDULE');
EXEC DBMS_SCHEDULER.SET_ATTRIBUTE( - name => 'HR.STATS_SCHEDULE', - attribute => 'START_DATE', - value => '01-JAN-2004 9:00:00 US/Pacific');
Copyright © 2004, Oracle. All rights reserved.17-30
Managing Windows
DBMS_SCHEDULER.OPEN_WINDOW( window_name =>'DEC_NIGHTS', duration => '1 0:00:00');
DBMS_SCHEDULER.CLOSE_WINDOW('DEC_NIGHTS');
DBMS_SCHEDULER.DROP_WINDOW( window_name =>'DEC_NIGHTS, DEC_DAYS', force => TRUE);
DBMS_SCHEDULER.DISABLE( name => 'SYS.DEC_NIGHTS', force=>TRUE);
Copyright © 2004, Oracle. All rights reserved.17-32
Window Priority
An order of precedence applies for when windows overlap:
• The window with the highest priority opens first
• If windows have same priority, then the window that is active remains open
• If at the end of a window there are multiple windows defined, the window that has the highest percentage of time remaining, opens
• An open window that is dropped is automatically closed
8 a.m6 a.m 10 a.m 12 p.m. 2 p.m
W1 W2W3
Copyright © 2004, Oracle. All rights reserved.17-33
Managing Attributes of Scheduler Components
• Change an attribute for a job:
• Remove a program comment by setting it to NULL:
BEGIN DBMS_SCHEDULER.SET_ATTRIBUTE( name => 'HR.GET_STATS', attribute => 'MAX_FAILURES', value => 3);END;/
EXEC DBMS_SCHEDULER.SET_ATTRIBUTE_NULL( - 'HR.CALC_STATS2','COMMENTS');
Copyright © 2004, Oracle. All rights reserved.17-34
Managing Attributes of Scheduler Components
Retrieve attribute values for the GET_STATS job created by the HR user:
SELECT max_failures MAX_FAIL, job_priority, schedule_limit SCHED_LIMIT, logging_levelFROM user_scheduler_jobsWHERE job_name = 'GET_STATS'AND job_creator = 'HR';
MAX_FAIL JOB_PRIOR SCHED_LIMIT LOGGING_LEVEL-------- --------- ------------ -------------- 3 3 LOW
Copyright © 2004, Oracle. All rights reserved.17-35
Managing Attributes of the Scheduler
You can set three attribute values at the global level, affecting all Scheduler components:
• default_timezone• max_job_slave_processes• log_history
EXEC DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE( - attribute => 'log_history', - value => '14');
Copyright © 2004, Oracle. All rights reserved.17-36
Viewing Job Execution Details
• The DBA_SCHEDULER_JOB_RUN_DETAILS view has a row for each job instance.
• Each row contains information about the job execution for that instance.
SELECT job_name, status, error#, run_durationFROM USER_SCHEDULER_JOB_RUN_DETAILS;
JOB_NAME STATUS ERROR# RUN_DURATION ---------------- ------ ------ ------------GATHER_STATS_JOB SUCCESS 0 +000 00:08:20 PART_EXCHANGE_JOB FAILURE 6576 +000 00:00:00
Copyright © 2004, Oracle. All rights reserved.17-37
Viewing Job Logs
The DBA_SCHEDULER_JOB_LOG view has a row for each job operation or modification.
SELECT job_name, operation, ownerFROM ALL_SCHEDULER_JOB_LOG;
JOB_NAME OPERATION OWNER ----------- --------- ------ GET_STATS2 ALTER HRTESTJOB RUN HRTESTJOB RETRY_RUN HRTESTJOB FAILED HRTESTJOB DROP HRCOMPUTE_STATS CREATE HR
Copyright © 2004, Oracle. All rights reserved.17-38
Purging Job Logs
You can purge the job logs:
• Automatically, through the PURGE_LOG job using default values
• Using the PURGE_LOG job, but modifying the conditions that determine when entries are purged
• On demand, by calling the DBMS_SCHEDULER.PURGE_LOG procedure
EXECUTE DBMS_SCHEDULER.PURGE_LOG( - log_history => 1, - job_name => 'TESTJOB');
Copyright © 2004, Oracle. All rights reserved.17-40
Data Dictionary Views
• [DBA | ALL | USER]_SCHEDULER_JOBS• [DBA | ALL | USER]_SCHEDULER_JOB_ARGS• [DBA | ALL | USER]_SCHEDULER_RUNNING_JOBS• [DBA | ALL | USER]_SCHEDULER_JOB_LOG• [DBA | ALL | USER]_SCHEDULER_JOB_RUN_DETAILS• [DBA | ALL | USER]_SCHEDULER_PROGRAMS• [DBA | ALL | USER]_SCHEDULER_PROGRAM_ARGS• [DBA | ALL | USER]_SCHEDULER_SCHEDULES• [DBA | ALL]_SCHEDULER_JOB_CLASSES• [DBA | ALL ]_SCHEDULER_WINDOWS• [DBA | ALL ]_SCHEDULER_WINDOW_DETAILS• [DBA | ALL ]_SCHEDULER_WINDOW_LOG
Copyright © 2004, Oracle. All rights reserved.17-41
Summary
In this lesson, you should have learned how to:• Simplify management tasks by using the
Scheduler• Create a job, program, schedule, and window• Reuse Scheduler components for similar tasks• View information about job executions and job
instances
Copyright © 2004, Oracle. All rights reserved.17-42
Practice 17 Overview: Automating Tasks with the Scheduler
This practice covers the following topics:
• Creating a job that runs a program outside of the database
• Creating a program and a schedule
• Creating a job that uses a program and a schedule
• Altering the program and schedule for the job and observing the behavior change of the job
• Monitoring job runs