1. 程式人生 > 其它 >KINGBASE Job And Schedule

KINGBASE Job And Schedule

KINGBASE 資料庫提供了kdb_schedule 擴充套件,使得使用者能通過類似oracle job 的方式進行job呼叫。kdb_schedule 提供了三個Schema :dbms_job anddbms_scheduler 分別類似於 oracle 的 dbms_job 和 dbms_schedule 包,資料字典資訊放於kdb_schedule 模式下。

一、通過dbms_job管理Job

1、建立Job

create table d_test(tid varchar2(64), insdate date);

create or replace procedure p_test() as
begin insert into d_test values(to_char(sysdate, 'yyyymmddhh24miss'), sysdate); commit; end; / DECLARE v_jobid NUMBER; BEGIN dbms_job.submit(v_jobid, 'call p_test()', now(), 'Freq=Minutely;Interval=1'); COMMIT; END; /

2、查詢Job

test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from
kdb_job; jobid | jobname | jobenabled | joblastrun | jobnextrun | jobrepeattimes -------+---------------------------------------------+------------+------------+-------------------------------+---------------- 1 | internal_job1:2021-06-22 08
:11:39.797681+08 | f | | 2021-06-22 08:11:39.797681+08 | 0

可以看到,Job 建立後,預設的狀態是 jobenabled=false,也就是實際沒有啟用。

Note:早期版本 kdb_job 表是schemakdb_schedule下,後面移到pg_catalog schema下。

3、啟用Job

job 建立後,預設是 broken(disabled) ,需要enable

test=# call dbms_job.broken(1,false);
CALL
test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from kdb_job;
 jobid |                   jobname                   | jobenabled | joblastrun |          jobnextrun           | jobrepeattimes 
-------+---------------------------------------------+------------+------------+-------------------------------+----------------
     1 | internal_job1:2021-06-22 08:11:39.797681+08 | t          |            | 2021-06-22 08:11:39.797681+08 |              0

enable job 後,等待一會再查,發現實際job 還是沒有執行。這是為什麼?這是因為kdb_schedule 後臺程序沒有啟動。啟動後臺程序後,再查,可以看到job已經運行了。

test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from kdb_job; 
 jobid |                   jobname                   | jobenabled |          joblastrun           |       jobnextrun       | jobrepeattimes 
-------+---------------------------------------------+------------+-------------------------------+------------------------+----------------
     1 | internal_job1:2021-06-22 08:11:39.797681+08 | t          | 2021-06-22 08:22:39.062009+08 | 2021-06-22 08:23:39+08 |              0
(1 row)

4、Job實際上也是Schedule

test=#  select * from kdb_schedule.kdb_schedule;
 scid | scname | scdesc | scenabled |          scstart           | scend |    screpeat_interval     
------+--------+--------+-----------+----------------------------+-------+--------------------------
    2 |        |        | t         | 2021-06-22 10:11:25.586148 |       | Freq=Minutely;Interval=1
(1 row)

test=# select * from kdb_schedule.kdb_schedule_job;
 sjid | sjscid | sjjobid | sjstatus | sjlasttime |          sjnexttime           
------+--------+---------+----------+------------+-------------------------------
    2 |      2 |       1 | s        |            | 2021-06-22 10:11:25.586148+08

可以看到,通過dbms_job建立的job資訊,同樣會在kdb_schedule 和kdb_schedule_job 顯示。

5、其他Job維護操作

--Job刪除
test=# call dbms_job.remove(1); CALL

二、啟動kdb_schedule 程序

使用過Oracle的人都知道,Oracle資料庫啟動時,會執行多個ora_j000_xxx 程序,這些程序實際上就是用於執行Job 的。同樣KINGBASE 也需要後臺程序來執行job

[kingbase@dbhost03 ~]$ kdb_schedule "dbname=template1 user=system"

Note:kdb_schedule只需要執行一個,後面跟的資料庫連線資訊可以是任何一個數據庫,不影響其他庫job的使用。

三、通過dbms_scheduler管理Job

1、建立program

begin
  dbms_scheduler.create_program(program_name        => 'prog_01',
                                program_type        => 'PLSQL_BLOCK',         
                                program_action      => 'call public.p_test()',
                                acdbname            => 'test',
                                number_of_arguments => 0,
                                enabled             => true,
                                comments            => 'test program');
end;

這裡有兩個地方必須注意。'PLSQL_BLOCK' 必須大寫,acdbname 必須指定。

acdbname 指明瞭action 所在的資料庫,如果沒有指定,預設指 kdb_schedule 執行時指定的資料庫。成功建立program後,會有如下一行資訊:

test=# select * from kdb_schedule.kdb_action;
 acid | acname  |    acdesc    | acenabled | ackind |        accode        | acconnstr | acdbname | acnextrun 
------+---------+--------------+-----------+--------+----------------------+-----------+----------+-----------
    7 | prog_01 | test program | t         | s      | call public.p_test() |           | test     | 
(1 row)

2、建立Schedule

begin
  dbms_scheduler.create_schedule(schedule_name   => 'schedule_01',
                                 start_date      => now(),
                                 repeat_interval => 'freq=minutely;interval=1',
                                 end_date        => null,
                                 comments        => 'test schedule');
end;

建立schedule 後,會有如下一行資訊:

test=# select * from kdb_schedule.kdb_schedule;
 scid |   scname    |    scdesc     | scenabled |          scstart           | scend |    screpeat_interval     
------+-------------+---------------+-----------+----------------------------+-------+--------------------------
    7 | schedule_01 | test schedule | t         | 2021-06-22 14:28:38.536823 |       | freq=minutely;interval=1
(1 row)

3、建立Job

begin
  dbms_scheduler.create_job(job_name         => 'job_01',
                            program_name     => 'prog_01',
                            schedule_name    => 'schedule_01',
                            job_class        => 'routine maintenance',
                            enabled          => true,
                            auto_drop        => true,
                            comments         => 'test job',
                            credentail_name  => null,
                            destination_name => null);
end;

建立後,下表會有相關記錄資訊,分別記錄 job 與 schedule , 以及 job 與 action 之間的關係。

test=# select * from kdb_schedule.kdb_schedule_job;
 sjid | sjscid | sjjobid | sjstatus | sjlasttime | sjnexttime 
------+--------+---------+----------+------------+------------
    7 |      7 |       1 | s        |            | 

test=# select * from kdb_schedule.kdb_job_action;
 jaid | jajobid | jaacid | jastatus | jalasttime 
------+---------+--------+----------+------------
    7 |       1 |      7 | s        | 

具體的job資訊,可以看 kdb_job:

test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from kdb_job;
jobid | jobname | jobenabled |          joblastrun           |       jobnextrun       | jobrepeattimes 
-------+---------+------------+-------------------------------+------------------------+----------------
     1 | job_01  | t          | 2021-06-22 14:59:41.997986+08 | 2021-06-22 15:00:38+08 |              0
(1 row)

4、Enable and Disable Job

Job 建立時,可以指定enable or disable ,後續也可以手動修改。

begin
  dbms_scheduler.disable(name => 'job_01');
end;
/

test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from kdb_job;
 jobid | jobname | jobenabled |          joblastrun           |       jobnextrun       | jobrepeattimes 
-------+---------+------------+-------------------------------+------------------------+----------------
     1 | job_01  | f          | 2021-06-22 15:06:42.303189+08 | 2021-06-22 15:07:38+08 |              0
(1 row)

begin
  dbms_scheduler.enable(name => 'job_01');
end;
/

test=# select jobid,jobname,jobenabled,joblastrun,jobnextrun,jobrepeattimes from kdb_job;
 jobid | jobname | jobenabled |          joblastrun           |       jobnextrun       | jobrepeattimes 
-------+---------+------------+-------------------------------+------------------------+----------------
     1 | job_01  | t          | 2021-06-22 15:08:07.365179+08 | 2021-06-22 15:08:38+08 |              0
(1 row)

5、其他操作

begin
  dbms_scheduler.drop_job(job_name         => 'job_01');
end;
/

begin
  dbms_scheduler.drop_schedule(schedule_name   => 'schedule_01');
end;
/

begin
  dbms_scheduler.drop_program(program_name        => 'prog_01');
end;
/

begin
  dbms_scheduler.run_job(job_name => 'job_01');
end;
/

四、故障排查

kdb_schedule.kdb_jobsteplog 記錄了job 呼叫的具體資訊,如果有錯誤的,jsloutput 會顯示具體的錯誤資訊。