diff --git a/product/en/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md b/product/en/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md new file mode 100644 index 0000000000000000000000000000000000000000..997802a3be3cc0caedf3a750b226dafa19dd6bce --- /dev/null +++ b/product/en/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md @@ -0,0 +1,367 @@ +--- +title: PKG_SERVICE API +summary: PKG_SERVICE API +author: Guo Huan +date: 2022-03-18 +--- + +# PKG_SERVICE API + +MogDB 2.1.0 provides the following PKG_SERVICE API to manage scheduled jobs. + +
+ +Table1 API Descriptions + +| API | Descriptions | +| ---------------------- | ------------------------------------------------------------ | +| PKG_SERVICE.JOB_CANCEL | Delete the scheduled job by job ID. | +| PKG_SERVICE.JOB_FINISH | Disable or enable scheduled job. | +| PKG_SERVICE.JOB_SUBMIT | Submit a scheduled job. The job ID is automatically generated by the system or specified by the user. | +| PKG_SERVICE.JOB_UPDATE | Update the properties of the scheduled job, including job content, next execution time, and execution interval. | + +
+ +## API Definition and Usage Examples + +- PKG_SERVICE.JOB_CANCEL + + The **CANCEL** procedure deletes the specified scheduled job. + + PKG_SERVICE.JOB_CANCEL function prototype is: + + ``` + PKG_SERVICE.JOB_CANCEL( job IN INTEGER); + ``` + + Table 2 PKG_SERVICE.JOB_CANCEL API parameters description + + | Parameter | Type | In/Out | Whether it can be empty | Definition | + | --------- | ------- | ------ | ----------------------- | ----------------- | + | id | integer | IN | No | Specified job ID. | + + **Example:** + + ```sql + CALL PKG_SERVICE.JOB_CANCEL(101); + ``` + +- PKG_SERVICE.JOB_FINISH + + The **FINISH** procedure disables or enables scheduled job. + + PKG_SERVICE.JOB_FINISH function prototype is: + + ``` + PKG_SERVICE.JOB_FINISH( + id IN INTEGER, + broken IN BOOLEAN, + next_time IN TIMESTAMP DEFAULT sysdate); + ``` + + Table 3 PKG_SERVICE.JOB_FINISH API parameters description + + | Parameter | Type | In/Out | Whether it can be empty | Definition | + | --------- | --------- | ------ | ----------------------- | ------------------------------------------------------------ | + | id | integer | IN | No | Specified job ID. | + | broken | Boolean | IN | No | Status flag bit, **true** means disabled, **false** means enabled. The current job is updated according to the **true** or **false** value; if it is **null**, the status of the original job is not changed. | + | next_time | timestamp | IN | Yes | The next run time, the default value is the current system time. If the parameter **broken** state is **true**, the parameter is updated to **4000-1-1**; if the parameter **broken** state is **false**, and if the parameter next_time is not null, the **next_time** value of the specified job is updated, if **next_time** is **null**, the **next_time** value is not updated. This parameter can be omitted and is the default value. | + +- PKG_SERVICE.JOB_SUBMIT + + The **JOB_SUBMIT** procedure submits a system-provided scheduled job. + + PKG_SERVICE.JOB_SUBMIT function prototype is: + + ``` + PKG_SERVICE.JOB_SUBMIT( + id IN BIGINT DEFAULT, + content IN TEXT, + next_date IN TIMESTAMP DEFAULT sysdate, + interval_time IN TEXT DEFAULT 'null', + job OUT INTEGER); + ``` + + > Note: When a scheduled job is created, the system binds the current database and user name to the currently created timed task by default. This API function can be called by **call** or **select** statement, if called by **select**, you can leave out the parameters. If in a stored procedure, the API function needs to be called via **perform** statement. If the submitted SQL statement job uses a non-public schema, you should specify the schema of the table or function, or add the `set current_schema = xxx;` statement before the SQL statement. + > + + Table 4 PKG_SERVICE.JOB_SUBMIT API parameters description + + | Parameter | Type | In/Out | Whether it can be empty | Definition | + | ------------- | --------- | ------ | ----------------------- | ------------------------------------------------------------ | + | id | bigint | IN | No | Job ID. If the passed id is **NULL**, the job ID will be generated internally. | + | context | text | IN | No | The SQL statement to be executed. Support one or more scenarios of 'DML', 'anonymous block', 'statement calling a stored procedure' or a mix of all 3. | + | next_time | timestamp | IN | No | The next job run time. The default value is the current system time (sysdate). If it is a past time, it indicates immediate execution when the job is submitted. | + | interval_time | text | IN | Yes | The time expression used to calculate the next job run time, either as an interval expression or as sysdate plus a numeric value (e.g. sysdate+1.0/24). If the value is null or the string "null", it means that the job will be executed only once, and the job status will become 'd' after execution. | + | job | integer | OUT | No | Job ID. The range is 1-32767. This parameter can be omitted when pkg_service.job_submit is called with **select** statement. | + + **Example:** + + ```sql + SELECT PKG_SERVICE.JOB_SUBMIT(NULL, 'call pro_xxx();', to_date('20180101','yyyymmdd'),'sysdate+1'); + + SELECT PKG_SERVICE.JOB_SUBMIT(NULL, 'call pro_xxx();', to_date('20180101','yyyymmdd'),'sysdate+1.0/24'); + + CALL PKG_SERVICE.JOB_SUBMIT(NULL, 'INSERT INTO T_JOB VALUES(1); call pro_1(); call pro_2();', add_months(to_date('201701','yyyymm'),1), 'date_trunc(''day'',SYSDATE) + 1 +(8*60+30.0)/(24*60)' ,:jobid); + + SELECT PKG_SERVICE.JOB_SUBMIT (101, 'insert_msg_statistic1;', sysdate, 'sysdate+3.0/24'); + ``` + +- PKG_SERVICE.JOB_UPDATE + + The **UPDATE** procedure modifies the properties of a scheduled job, including the job content, next execution time, and execution interval. + + PKG_SERVICE.JOB_UPDATE function prototype is: + + ``` + PKG_SERVICE.JOB_UPDATE( + id IN BIGINT, + next_time IN TIMESTAMP, + interval_time IN TEXT, + content IN TEXT); + ``` + + Table 5 PKG_SERVICE.JOB_UPDATE API parameters description + + | Parameter | Type | In/Out | Whether it can be empty | Definition | + | ------------- | --------- | ------ | ----------------------- | ------------------------------------------------------------ | + | id | integer | IN | No | Specified job ID. | + | next_time | timestamp | IN | Yes | The next job run time. If this parameter is null, the next_time value of the specified job is not updated, otherwise the next_time value of the specified job is updated. | + | interval_time | text | IN | Yes | The time expression used to calculate the next job run time. If the parameter is null, the interval_time value of the specified job will not be updated; if the parameter is not null, it will check whether the interval_time is a valid time type or interval type, then the interval_time value of the specified job will be updated. If it is the string "null", it means it will be executed only once, and the job status will become 'd' after execution. | + | content | text | IN | Yes | The name of the stored procedure or SQL statement block to execute. If this parameter is null, the content value of the specified job is not updated, otherwise the content value of the specified job is updated. | + + **Example:** + + ```sql + CALL PKG_SERVICE.JOB_UPDATE(101, 'call userproc();', sysdate, 'sysdate + 1.0/1440'); + + CALL PKG_SERVICE.JOB_UPDATE(101, 'insert into tbl_a values(sysdate);', sysdate, 'sysdate + 1.0/1440'); + ``` + +
+ +## Examples of Using Scheduled Job + +This example describes how to implement job management through the above APIs. + +- Create a test table + + ``` + mogdb=# create table t_job (value TIMESTAMP); + CREATE TABLE + + mogdb=# insert into t_job values(sysdate); + INSERT 0 1 + + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + +---------------------+ + SELECT 1 + ``` + +- Create a job to insert a record every minute + + ``` + mogdb=# select pkg_service.job_submit(null, 'insert into t_job values(sysdate);',sysdate,'sysdate + 1/1440'); + +--------------+ + | job_submit | + |--------------| + | 15566 | + +--------------+ + SELECT 1 + ``` + +- Check the results of the job runs + + ``` + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + | 2021-10-09 04:40:54 | + | 2021-10-09 04:41:54 | + | 2021-10-09 04:42:54 | + +---------------------+ + SELECT 4 + ``` + +- Check job operation from system view + + ``` + mogdb=# select job_id,dbname,start_date,next_run_date,interval,failure_count from pg_job; + +----------+----------+----------------------------+---------------------+------------------+-----------------+ + | job_id | dbname | start_date | next_run_date | interval | failure_count | + |----------+----------+----------------------------+---------------------+------------------+-----------------| + | 15566 | postgres | 2021-10-09 04:40:54.072363 | 2021-10-09 04:56:54 | sysdate + 1/1440 | 0 | + +----------+----------+----------------------------+---------------------+------------------+-----------------+ + SELECT 1 + Time: 0.089s + mogdb=# select * from pg_catalog.pg_job_proc pjp where job_id=15566; + +----------+------------------------------------+ + | job_id | what | + |----------+------------------------------------| + | 15566 | insert into t_job values(sysdate); | + +----------+------------------------------------+ + SELECT 1 + Time: 0.089s + ``` + +- Modified to execute once every 2 minutes + + ``` + mogdb=# select pkg_service.job_update(15566,null,'sysdate + 2/1440',null); + +--------------+ + | job_update | + |--------------| + | | + +--------------+ + SELECT 1 + ``` + +- Check modifications and run results + + ``` + mogdb=# select job_id,interval from pg_job where job_id=15566; + +----------+------------------+ + | job_id | interval | + |----------+------------------| + | 15566 | sysdate + 2/1440 | + +----------+------------------+ + SELECT 1 + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + | 2021-10-09 04:40:54 | + | 2021-10-09 04:41:54 | + | 2021-10-09 04:42:54 | + | 2021-10-09 04:43:54 | + | 2021-10-09 04:44:54 | + | 2021-10-09 04:45:54 | + | 2021-10-09 04:46:54 | + | 2021-10-09 04:47:54 | + | 2021-10-09 04:48:54 | + | 2021-10-09 04:49:54 | + | 2021-10-09 04:50:54 | + | 2021-10-09 04:51:54 | + | 2021-10-09 04:52:54 | + | 2021-10-09 04:53:54 | + | 2021-10-09 04:54:54 | + | 2021-10-09 04:55:54 | + | 2021-10-09 04:56:54 | + | 2021-10-09 04:57:54 | + | 2021-10-09 04:58:54 | + | 2021-10-09 04:59:54 | + | 2021-10-09 05:00:55 | + | 2021-10-09 05:01:56 | <--- + | 2021-10-09 05:03:57 | <--- Start interval 2 minutes + +---------------------+ + SELECT 24 + Time: 0.088s + mogdb=# select job_id,interval,next_run_date from pg_job where job_id=15566; + +----------+------------------+---------------------+ + | job_id | interval | next_run_date | + |----------+------------------+---------------------| + | 15566 | sysdate + 2/1440 | 2021-10-09 05:05:57 | + +----------+------------------+---------------------+ + SELECT 1 + Time: 0.078s> + ``` + +- Finish and start jobs + + Both finish and start are the same function **pkg_service.job_finish**, and different parameters are passed in to indicate whether to finish or start. + + ``` + mogdb=# select pkg_service.job_finish(15566,true,null); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.089s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 4000-01-01 00:00:00 | d | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.075s + mogdb=# select pkg_service.job_finish(15566,false,null); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.091s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 4000-01-01 00:00:00 | s | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.080s + ``` + + It can be seen that if the next run time is not specified when the job is re-start, the next run time will always remain at 4000, which means that it will still not start, so if the job is finished and then restarted, you need to manually specify the next run time. + + ``` + mogdb=# select pkg_service.job_finish(15566,false,sysdate); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.088s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 2021-10-09 05:16:22 | s | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.086s + ``` + +- Cancel job + + ``` + mogdb=# select pkg_service.job_cancel(15566); + +--------------+ + | job_cancel | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.082s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+-----------------+--------------+ + | job_id | next_run_date | job_status | + |----------+-----------------+--------------| + +----------+-----------------+--------------+ + SELECT 0 + Time: 0.086s + mogdb=# select * from pg_catalog.pg_job_proc pjp where job_id=15566; + +----------+--------+ + | job_id | what | + |----------+--------| + +----------+--------+ + SELECT 0 + Time: 0.087s + ``` + +
+ +## Related Pages + +[PG_JOB](PG_JOB), [Scheduled Job](31-scheduled-task) \ No newline at end of file diff --git a/product/en/docs-mogdb/v2.1/source-code-parsing.md b/product/en/docs-mogdb/v2.1/source-code-parsing.md index a8002a05a81bce2aa00c320ed9a69730f8248936..634eeb4c852698d95c2e657b4050341b66bfea23 100644 --- a/product/en/docs-mogdb/v2.1/source-code-parsing.md +++ b/product/en/docs-mogdb/v2.1/source-code-parsing.md @@ -83,4 +83,6 @@ You can click the links below to view this series of articles. [Parsing of Security Management Source Code (Part 1)](https://www.modb.pro/db/335331) -[Parsing of Security Management Source Code (Part 2)](https://www.modb.pro/db/337160) \ No newline at end of file +[Parsing of Security Management Source Code (Part 2)](https://www.modb.pro/db/337160) + +[Parsing of Security Management Source Code (Part 3)](https://www.modb.pro/db/378249) \ No newline at end of file diff --git a/product/en/docs-mogdb/v2.1/toc.md b/product/en/docs-mogdb/v2.1/toc.md index 4529c24b3245b9c076a5f142fc893cbe16392d17..ea7afec843255ce4b5fa352f4ded814bb499fc34 100644 --- a/product/en/docs-mogdb/v2.1/toc.md +++ b/product/en/docs-mogdb/v2.1/toc.md @@ -332,8 +332,11 @@ + [Transaction Management](/developer-guide/plpgsql/1-9-transaction-management.md) + [Other Statements](/developer-guide/plpgsql/1-10-other-statements.md) + [Cursors](/developer-guide/plpgsql/1-11-cursors.md) + + [PKG_SERVICE API](/developer-guide/plpgsql/1-14-pkg-service.md) + [Retry Management](/developer-guide/plpgsql/1-12-retry-management.md) + [Debugging](/developer-guide/plpgsql/1-13-debugging.md) + + Scheduled Jobs + + [PKG_SERVICE API](/developer-guide/scheduled-jobs/pkg-service.md) + Autonomous Transaction + [Introduction](/developer-guide/autonomous-transaction/1-introduction-to-autonomous-transaction.md) + [Stored Procedure Supporting Autonomous Transaction](/developer-guide/autonomous-transaction/3-stored-procedure-supporting-autonomous-transaction.md) diff --git a/product/en/docs-mogdb/v2.1/toc_dev.md b/product/en/docs-mogdb/v2.1/toc_dev.md index dcb7e6a2c1d0ab1154b4290862008467b24218b8..177b8ccbdb7722fcc0210c1cb1b93bbb170fda86 100644 --- a/product/en/docs-mogdb/v2.1/toc_dev.md +++ b/product/en/docs-mogdb/v2.1/toc_dev.md @@ -160,8 +160,11 @@ + [Transaction Management](/developer-guide/plpgsql/1-9-transaction-management.md) + [Other Statements](/developer-guide/plpgsql/1-10-other-statements.md) + [Cursors](/developer-guide/plpgsql/1-11-cursors.md) + + [PKG_SERVICE API](/developer-guide/plpgsql/1-14-pkg-service.md) + [Retry Management](/developer-guide/plpgsql/1-12-retry-management.md) + [Debugging](/developer-guide/plpgsql/1-13-debugging.md) ++ Scheduled Jobs + + [PKG_SERVICE API](/developer-guide/scheduled-jobs/pkg-service.md) + Autonomous Transaction + [Introduction](/developer-guide/autonomous-transaction/1-introduction-to-autonomous-transaction.md) + [Stored Procedure Supporting Autonomous Transaction](/developer-guide/autonomous-transaction/3-stored-procedure-supporting-autonomous-transaction.md) diff --git a/product/zh/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md b/product/zh/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md new file mode 100644 index 0000000000000000000000000000000000000000..e9ee88ad27f3c33bafecc38f718f3c2ba7b6dc87 --- /dev/null +++ b/product/zh/docs-mogdb/v2.1/developer-guide/scheduled-jobs/pkg-service.md @@ -0,0 +1,366 @@ +--- +title: PKG_SERVICE接口 +summary: PKG_SERVICE接口 +author: Guo Huan +date: 2022-03-18 +--- + +# PKG_SERVICE接口 + +MogDB 2.1.0版本提供了以下PKG_SERVICE接口来实现定时任务管理。 + +
+ +表1 接口描述 + +| 接口名称 | 描述 | +| ---------------------- | ---------------------------------------------------------- | +| PKG_SERVICE.JOB_CANCEL | 通过任务ID来删除定时任务。 | +| PKG_SERVICE.JOB_FINISH | 禁用或者启用定时任务。 | +| PKG_SERVICE.JOB_SUBMIT | 提交一个定时任务。作业号由系统自动生成或由用户指定。 | +| PKG_SERVICE.JOB_UPDATE | 修改定时任务的属性,包括任务内容、下次执行时间、执行间隔。 | + +
+ +## 接口定义和使用示例 + +- PKG_SERVICE.JOB_CANCEL + + 存储过程CANCEL删除指定的定时任务。 + + PKG_SERVICE.JOB_CANCEL函数原型为: + + ``` + PKG_SERVICE.JOB_CANCEL( job IN INTEGER); + ``` + + 表2 PKG_SERVICE.JOB_CANCEL接口参数说明 + + | 参数 | 类型 | 入参/出参 | 是否可以为空 | 描述 | + | ---- | ------- | --------- | ------------ | -------------- | + | id | integer | IN | 否 | 指定的作业号。 | + + 示例: + + ```sql + CALL PKG_SERVICE.JOB_CANCEL(101); + ``` + +- PKG_SERVICE.JOB_FINISH + + 存储过程FINISH禁用或者启用定时任务。 + + PKG_SERVICE.JOB_FINISH函数原型为: + + ``` + PKG_SERVICE.JOB_FINISH( + id IN INTEGER, + broken IN BOOLEAN, + next_time IN TIMESTAMP DEFAULT sysdate); + ``` + + 表3 PKG_SERVICE.JOB_FINISH接口参数说明 + + | 参数 | 类型 | 入参/出参 | 是否可以为空 | 描述 | + | --------- | --------- | --------- | ------------ | ------------------------------------------------------------ | + | id | integer | IN | 否 | 指定的作业号。 | + | broken | Boolean | IN | 否 | 状态标志位,true代表禁用,false代表启用。根据true或false值更新当前job;如果为空值,则不改变原有job的状态。 | + | next_time | timestamp | IN | 是 | 下次运行时间,默认为当前系统时间。如果参数broken状态为true,则更新该参数为'4000-1-1';如果参数broken状态为false,且如果参数next_time不为空值,则更新指定job的next_time值,如果next_time为空值,则不更新next_time值。该参数可以省略,为默认值。 | + +- PKG_SERVICE.JOB_SUBMIT + + 存储过程JOB_SUBMIT提交一个系统提供的定时任务。 + + PKG_SERVICE.JOB_SUBMIT函数原型为: + + ``` + PKG_SERVICE.JOB_SUBMIT( + id IN BIGINT DEFAULT, + content IN TEXT, + next_date IN TIMESTAMP DEFAULT sysdate, + interval_time IN TEXT DEFAULT 'null', + job OUT INTEGER); + ``` + + > 说明:当创建一个定时任务(JOB)时,系统默认将当前数据库和用户名与当前创建的定时任务绑定起来。该接口函数可以通过call或select调用,如果通过select调用,可以不填写出参。如果在存储过程中,则需要通过perform调用该接口函数。如果提交的sql语句任务使用到非public的schema,应该指定表或者函数的schema,或者在sql语句前添加`set current_schema = xxx;`语句。 + + 表4 PKG_SERVICE.JOB_SUBMIT接口参数说明 + + | 参数 | 类型 | 入参/出参 | 是否可以为空 | 描述 | + | ------------- | --------- | --------- | ------------ | ------------------------------------------------------------ | + | id | bigint | IN | 否 | 作业号。如果传入id为NULL,则内部会生成作业ID。 | + | context | text | IN | 否 | 要执行的SQL语句。支持一个或多个‘DML’,‘匿名块’,‘调用存储过程的语句’或3种混合的场景。 | + | next_time | timestamp | IN | 否 | 下次作业运行时间。默认值为当前系统时间(sysdate)。如果是过去时间,在提交作业时表示立即执行。 | + | interval_time | text | IN | 是 | 用来计算下次作业运行时间的时间表达式,可以是interval表达式,也可以是sysdate加上一个numeric值(例如:sysdate+1.0/24)。如果为空值或字符串"null"表示只执行一次,执行后JOB状态STATUS变成'd' 不再执行。 | + | job | integer | OUT | 否 | 作业号。范围为1~32767。当使用select调用pkg_service.job_submit时,该参数可以省略。 | + + 示例: + + ```sql + SELECT PKG_SERVICE.JOB_SUBMIT(NULL, 'call pro_xxx();', to_date('20180101','yyyymmdd'),'sysdate+1'); + + SELECT PKG_SERVICE.JOB_SUBMIT(NULL, 'call pro_xxx();', to_date('20180101','yyyymmdd'),'sysdate+1.0/24'); + + CALL PKG_SERVICE.JOB_SUBMIT(NULL, 'INSERT INTO T_JOB VALUES(1); call pro_1(); call pro_2();', add_months(to_date('201701','yyyymm'),1), 'date_trunc(''day'',SYSDATE) + 1 +(8*60+30.0)/(24*60)' ,:jobid); + + SELECT PKG_SERVICE.JOB_SUBMIT (101, 'insert_msg_statistic1;', sysdate, 'sysdate+3.0/24'); + ``` + +- PKG_SERVICE.JOB_UPDATE + + 存储过程UPDATE修改定时任务的属性,包括任务内容、下次执行时间、执行间隔。 + + PKG_SERVICE.JOB_UPDATE函数原型为: + + ``` + PKG_SERVICE.JOB_UPDATE( + id IN BIGINT, + next_time IN TIMESTAMP, + interval_time IN TEXT, + content IN TEXT); + ``` + + 表5 PKG_SERVICE.JOB_UPDATE接口参数说明 + + | 参数 | 类型 | 入参/出参 | 是否可以为空 | 描述 | + | ------------- | --------- | --------- | ------------ | ------------------------------------------------------------ | + | id | integer | IN | 否 | 指定的作业号。 | + | next_time | timestamp | IN | 是 | 下次运行时间。如果该参数为空值,则不更新指定job的next_time值,否则更新指定job的next_time值。 | + | interval_time | text | IN | 是 | 用来计算下次作业运行时间的时间表达式。如果该参数为空值,则不更新指定job的interval_time值;如果该参数不为空值,会校验interval_time是否为有效的时间类型或interval类型,则更新指定job的interval_time值。如果为字符串"null"表示只执行一次,执行后JOB状态STATUS变成'd' 不再执行。 | + | content | text | IN | 是 | 执行的存储过程名或者sql语句块。如果该参数为空值,则不更新指定job的content值,否则更新指定job的content值。 | + + 示例: + + ```sql + CALL PKG_SERVICE.JOB_UPDATE(101, 'call userproc();', sysdate, 'sysdate + 1.0/1440'); + + CALL PKG_SERVICE.JOB_UPDATE(101, 'insert into tbl_a values(sysdate);', sysdate, 'sysdate + 1.0/1440'); + ``` + +
+ +## 定时任务(job)使用示例 + +本示例介绍如何通过上述接口实现定时任务(job)管理。 + +- 创建测试表 + + ``` + mogdb=# create table t_job (value TIMESTAMP); + CREATE TABLE + + mogdb=# insert into t_job values(sysdate); + INSERT 0 1 + + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + +---------------------+ + SELECT 1 + ``` + +- 创建任务,每一分钟插入一条记录 + + ``` + mogdb=# select pkg_service.job_submit(null, 'insert into t_job values(sysdate);',sysdate,'sysdate + 1/1440'); + +--------------+ + | job_submit | + |--------------| + | 15566 | + +--------------+ + SELECT 1 + ``` + +- 检查JOB运行结果 + + ``` + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + | 2021-10-09 04:40:54 | + | 2021-10-09 04:41:54 | + | 2021-10-09 04:42:54 | + +---------------------+ + SELECT 4 + ``` + +- 从系统视图中检查JOB运行情况 + + ``` + mogdb=# select job_id,dbname,start_date,next_run_date,interval,failure_count from pg_job; + +----------+----------+----------------------------+---------------------+------------------+-----------------+ + | job_id | dbname | start_date | next_run_date | interval | failure_count | + |----------+----------+----------------------------+---------------------+------------------+-----------------| + | 15566 | postgres | 2021-10-09 04:40:54.072363 | 2021-10-09 04:56:54 | sysdate + 1/1440 | 0 | + +----------+----------+----------------------------+---------------------+------------------+-----------------+ + SELECT 1 + Time: 0.089s + mogdb=# select * from pg_catalog.pg_job_proc pjp where job_id=15566; + +----------+------------------------------------+ + | job_id | what | + |----------+------------------------------------| + | 15566 | insert into t_job values(sysdate); | + +----------+------------------------------------+ + SELECT 1 + Time: 0.089s + ``` + +- 修改为2分钟执行一次 + + ``` + mogdb=# select pkg_service.job_update(15566,null,'sysdate + 2/1440',null); + +--------------+ + | job_update | + |--------------| + | | + +--------------+ + SELECT 1 + ``` + +- 检查修改情况和运行结果 + + ``` + mogdb=# select job_id,interval from pg_job where job_id=15566; + +----------+------------------+ + | job_id | interval | + |----------+------------------| + | 15566 | sysdate + 2/1440 | + +----------+------------------+ + SELECT 1 + mogdb=# select * from t_job; + +---------------------+ + | value | + |---------------------| + | 2021-10-09 04:36:20 | + | 2021-10-09 04:40:54 | + | 2021-10-09 04:41:54 | + | 2021-10-09 04:42:54 | + | 2021-10-09 04:43:54 | + | 2021-10-09 04:44:54 | + | 2021-10-09 04:45:54 | + | 2021-10-09 04:46:54 | + | 2021-10-09 04:47:54 | + | 2021-10-09 04:48:54 | + | 2021-10-09 04:49:54 | + | 2021-10-09 04:50:54 | + | 2021-10-09 04:51:54 | + | 2021-10-09 04:52:54 | + | 2021-10-09 04:53:54 | + | 2021-10-09 04:54:54 | + | 2021-10-09 04:55:54 | + | 2021-10-09 04:56:54 | + | 2021-10-09 04:57:54 | + | 2021-10-09 04:58:54 | + | 2021-10-09 04:59:54 | + | 2021-10-09 05:00:55 | + | 2021-10-09 05:01:56 | <--- + | 2021-10-09 05:03:57 | <--- 开始间隔2分钟 + +---------------------+ + SELECT 24 + Time: 0.088s + mogdb=# select job_id,interval,next_run_date from pg_job where job_id=15566; + +----------+------------------+---------------------+ + | job_id | interval | next_run_date | + |----------+------------------+---------------------| + | 15566 | sysdate + 2/1440 | 2021-10-09 05:05:57 | + +----------+------------------+---------------------+ + SELECT 1 + Time: 0.078s> + ``` + +- 禁用和启用任务 + + 禁用和启用都是同样的函数pkg_service.job_finish,传入不同的参数表示是禁用还是启用。 + + ``` + mogdb=# select pkg_service.job_finish(15566,true,null); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.089s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 4000-01-01 00:00:00 | d | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.075s + mogdb=# select pkg_service.job_finish(15566,false,null); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.091s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 4000-01-01 00:00:00 | s | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.080s + ``` + + 可以看到如果重新启用任务的时候,没有指定下次运行时间,那么下次运行时间会始终保持在4000年,意味着仍然不会启动,所以如果禁用任务之后再重新启动,需要手动显式指定下次运行时间。 + + ``` + mogdb=# select pkg_service.job_finish(15566,false,sysdate); + +--------------+ + | job_finish | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.088s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+---------------------+--------------+ + | job_id | next_run_date | job_status | + |----------+---------------------+--------------| + | 15566 | 2021-10-09 05:16:22 | s | + +----------+---------------------+--------------+ + SELECT 1 + Time: 0.086s + ``` + +- 删除任务 + + ``` + mogdb=# select pkg_service.job_cancel(15566); + +--------------+ + | job_cancel | + |--------------| + | | + +--------------+ + SELECT 1 + Time: 0.082s + mogdb=# select job_id,next_run_date,job_status from pg_job where job_id=15566; + +----------+-----------------+--------------+ + | job_id | next_run_date | job_status | + |----------+-----------------+--------------| + +----------+-----------------+--------------+ + SELECT 0 + Time: 0.086s + mogdb=# select * from pg_catalog.pg_job_proc pjp where job_id=15566; + +----------+--------+ + | job_id | what | + |----------+--------| + +----------+--------+ + SELECT 0 + Time: 0.087s + ``` + +
+ +## 相关页面 + +[PG_JOB](PG_JOB)、[定时任务](31-scheduled-task) \ No newline at end of file diff --git a/product/zh/docs-mogdb/v2.1/source-code-parsing.md b/product/zh/docs-mogdb/v2.1/source-code-parsing.md index 51cbbeeaa8ce320279e7dc9d03e58accef6b8219..56f4d9c7535b890f9f7a6ad15026fe30cd6d3dd0 100644 --- a/product/zh/docs-mogdb/v2.1/source-code-parsing.md +++ b/product/zh/docs-mogdb/v2.1/source-code-parsing.md @@ -83,4 +83,6 @@ date: 2022-02-07 [安全管理源码解析(一)](https://www.modb.pro/db/335331) -[安全管理源码解析(二)](https://www.modb.pro/db/337160) \ No newline at end of file +[安全管理源码解析(二)](https://www.modb.pro/db/337160) + +[安全管理源码解析(三)](https://www.modb.pro/db/378249) \ No newline at end of file diff --git a/product/zh/docs-mogdb/v2.1/toc.md b/product/zh/docs-mogdb/v2.1/toc.md index bd12419dbfcb6600ba9a226969f484682c74ee5a..c10e4dbb33c03f69803931f55f62e7c120cdafb5 100644 --- a/product/zh/docs-mogdb/v2.1/toc.md +++ b/product/zh/docs-mogdb/v2.1/toc.md @@ -336,6 +336,8 @@ + [游标](/developer-guide/plpgsql/1-11-cursors.md) + [Retry管理](/developer-guide/plpgsql/1-12-retry-management.md) + [调试](/developer-guide/plpgsql/1-13-debugging.md) + + 定时任务 + + [PKG_SERVICE接口](/developer-guide/scheduled-jobs/pkg-service.md) + 自治事务 + [介绍](/developer-guide/autonomous-transaction/1-introduction-to-autonomous-transaction.md) + [存储过程支持自治事务](/developer-guide/autonomous-transaction/3-stored-procedure-supporting-autonomous-transaction.md) diff --git a/product/zh/docs-mogdb/v2.1/toc_dev.md b/product/zh/docs-mogdb/v2.1/toc_dev.md index 2db89ed3028813b3a03f19f6d5224af16e7c0be2..14e3613ab5e54a7d5ff35f280eba096dd0daabb5 100644 --- a/product/zh/docs-mogdb/v2.1/toc_dev.md +++ b/product/zh/docs-mogdb/v2.1/toc_dev.md @@ -160,8 +160,11 @@ + [事务管理](/developer-guide/plpgsql/1-9-transaction-management.md) + [其他语句](/developer-guide/plpgsql/1-10-other-statements.md) + [游标](/developer-guide/plpgsql/1-11-cursors.md) + + [PKG_SERVICE接口](/developer-guide/plpgsql/1-14-pkg-service.md) + [Retry管理](/developer-guide/plpgsql/1-12-retry-management.md) + [调试](/developer-guide/plpgsql/1-13-debugging.md) ++ 定时任务 + + [PKG_SERVICE接口](/developer-guide/scheduled-jobs/pkg-service.md) + 自治事务 + [介绍](/developer-guide/autonomous-transaction/1-introduction-to-autonomous-transaction.md) + [存储过程支持自治事务](/developer-guide/autonomous-transaction/3-stored-procedure-supporting-autonomous-transaction.md)