Skip to content

lib-job 库参考 ​

原文: https://finproducts.atlassian.net/wiki/spaces/JIDD3/pages/34793685140/FIN+5.3.1+lib-job 中文整理基于 FIN 5.3.1

导读: 作业管理(已弃用)

以下为 API 参考,保留英文以便对照源码;目录已列出(英文标题便于检索);概述已尽量中译。


目录(导读) ​

  • lib
  • meta
  • docs
  • tags
  • funcs
  • views
  • Overview
  • Job Functions
  • Scheduled Jobs
  • Tuning
  • Security
  • jobCancel(handle)
  • jobCurRec(checked: true)
  • jobFolioBackup()
  • jobFolioCompact()
  • jobFolioSnapshot()
  • jobIsRunning()
  • jobLog(handle)
  • jobProgress(percent, msg)
  • jobRefreshUser()
  • jobRun(expr)
  • jobSleep(dur)
  • jobStatus(handle)
  • jobStatusAll()

概述(中译) ​

作业管理(已弃用)


API 正文(英文) ​

docs ​

Overview • Job Functions • Scheduled Jobs • Tuning • Security


tags ​

jobBackground job
jobExprAxon expression to execute for job
jobScheduleSpecifies when a job is scheduled using a string format
jobStatusCurrent status of a job

funcs ​

jobCancelSend the cancel message to the job identified by given handle
jobCurRecIf running in the context of a scheduled job then return the job record Dict
jobFolioBackupRun the folioBackup() function as a background job
jobFolioCompactThis is a no-op
jobFolioSnapshotDeprecated
jobIsRunningIs the current context contained by a running job
jobLogReturn the LogRecs logged by the job identified by handle
jobProgressUpdate the current job's progress with percent complete and display message for current activity
jobRefreshUserRefresh the user account used for jobs
jobRunRun the given expression in the context of a background job
jobSleepSleep for the given duration
jobStatusReturn the status for a single job identified by handle
jobStatusAllReturn the status for all the known jobs

views ​

jobsManage records used to run background jobs

lib job ​

Overview ​

The job extension is used to schedule and manage long running computing tasks in the background. The job extension may be used in two ways:

  1. any axon expression may be scheduled as a background task using the jobRun() function
  2. jobs may be permanently configured and scheduled to run periodically using the job, jobExpr, and jobSchedule tags

NOTE: the job extension is being phased out in favor of the task ext.

Job Functions ​

The following Axon functions are used to create background tasks:

  • jobRun(): kick off an expression in the background
  • jobIsRunning(): check if your code is evaluating in context of job
  • jobProgress(): used to advance job's progress bar and update users what your job is currently doing

When the jobRun function is called it creates a background job and assigns it an opaque handle string. This handle may be used with functions such as jobStatus and jobLog() to monitor the status of the job.

Typically a function is written to be run in the context of a job. Here is a simple example:

() => do
  echo("TestJob start...")
  10.times(i => do
    jobProgress(i*10, echo("TestJob " + i + "..."))
    jobSleep(1sec)
  end)
  echo("TestJob done!")
end

To test it out create this function and give it a name like myTestJob and then run this expression in the Axon tool:

jobRun(myTestJob)

The function will be scheduled to run, and assuming you don't have other jobs running it should immediately execute on a background thread. The job extension tool will show your job and indicate the progress. The example code above will print messages to stdout every second for ten iterations.

Scheduled Jobs ​

You can configure scheduled jobs in the job extension tool. Scheduled jobs are recs with these three tags:

  • job: marker tag
  • jobExpr: the Axon expression to run (typically function name)
  • jobSchedule: when to run the job

The job scheduling tag uses a special string format we call a schedule rule:

  • one-shot jobs are scheduled to run once at a given date time:
    • onetime 2010-07-04T12:00:00-04:00 New_York
  • every jobs are scheduled to run periodically with a given interval:
    • every 15min: run every 15 minutes
    • every 2hr: run every 2 hours
  • at jobs are scheduled to run at one or more times of day:
    • at 00:00: run at midnight every day
    • at 9:00,18:00: run at 9am and 6pm every day
  • on jobs are scheduled to run only on given days at a given time:
    • on sun at 2:00: run every Sunday at 2am
    • on 1,15 at 12:00: run on the 1st and 15th of each month at noon

Jobs report their status from their last run using the following tags:

  • jobLastRuntime: duration of last run
  • jobLastStatus: status last run: doneCancel, doneErr, doneOk
  • jobLastTime: timestamp when job finished (success or failure)

Job scheduling is based off the jobLastTime tag. If this tag cannot be updated properly, then the job cannot be correctly scheduled. For this reason jobs are not scheduled if the database is not in read-write mode.

Jobs which run every hour or more frequently are called high frequency jobs. High frequency jobs only update their status transiently. This avoids writing job status to disk which is a costly operation. However it means that high frequency jobs will be re-run immediately after every reboot.

If the disabled marker tag is applied to a scheduled job, then it will not automatically run. However it can still be run manually using the jobRun() function.

Tuning ​

By default, the job extension will run up to 10 concurrent jobs. You can tune this by editing the job ext record and setting maxRunning the desired value.

// set maxRunning to 5
diff(read(ext=="job"), {maxRunning: 5}).commit

The job extension will keep the logs from the previous 20 jobs available in-memory. For jobs that do extensive logging, this can quickly consume heap. You can tune how many job logs are kept in memory by editing the job ext record and setting maxFinished to the desired value.

// set maxFinished to 10
diff(read(ext=="job"), {maxFinished: 10}).commit

Security ​

Jobs are executed in an axon context using the local user with the username "job". Use the userAllow function to grant specific superuser functions to your jobs. The job user account must not be configured with the "su" role or it will not be used.

If no user is defined by the username "job", then an inferred synthetic user with the "admin" role is used for all evaluations. The synthetic user has its projAccessFilter set to restrict access to the local project only. XQuery to other projects is not available.

Remember that by default all admin users can create/edit jobs. So it is important restrict what can be performed by jobs using the user database.

If testing changes to the job user account, you can call jobRefreshUser() to force an immediate cache refresh.



def ​

func:jobCancel ​

jobCancel(handle) ​

Send the cancel message to the job identified by given handle. Throw Err if job ext not installed.

Side effects:

  • changes job status of given job

meta ​

admin✓
deffunc:jobCancel
isfunc
liblib:job
namejobCancel

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobCurRec ​

jobCurRec(checked: true) ​

If running in the context of a scheduled job then return the job record Dict. Otherwise raise an error or return null based on the checked flag.


meta ​

deffunc:jobCurRec
isfunc
liblib:job
namejobCurRec

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobFolioBackup ​

jobFolioBackup() ​

Run the folioBackup() function as a background job. Unlike the core function, this function runs synchronously and updates the job progress based on backup progress. Raise an exception if this function is not called while running in a job.


meta ​

admin✓
deffunc:jobFolioBackup
isfunc
liblib:job
namejobFolioBackup

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobFolioCompact ​

jobFolioCompact() ​

This is a no-op; compaction is no longer required in 3.0


meta ​

admin✓
deffunc:jobFolioCompact
deprecated✓
isfunc
liblib:job
namejobFolioCompact

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobFolioSnapshot ​

jobFolioSnapshot() ​

Deprecated: use jobFolioBackup()


meta ​

admin✓
deffunc:jobFolioSnapshot
deprecated✓
isfunc
liblib:job
namejobFolioSnapshot

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobIsRunning ​

jobIsRunning() ​

Is the current context contained by a running job.


meta ​

deffunc:jobIsRunning
isfunc
liblib:job
namejobIsRunning

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobLog ​

jobLog(handle) ​

Return the LogRecs logged by the job identified by handle. Throw Err if job ext not installed or handle does map to a valid job. Columns:

  • ts: timestamp of log item
  • level: err, warn, info, debug
  • msg: message of LogRec
  • errTrace: dump if there was an exception

meta ​

deffunc:jobLog
isfunc
liblib:job
namejobLog

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobProgress ​

jobProgress(percent, msg) ​

Update the current job's progress with percent complete and display message for current activity. If the current context is not actively running inside a job, this is a no-op.


meta ​

deffunc:jobProgress
isfunc
liblib:job
namejobProgress

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobRefreshUser ​

jobRefreshUser() ​

Refresh the user account used for jobs


meta ​

admin✓
deffunc:jobRefreshUser
isfunc
liblib:job
namejobRefreshUser

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobRun ​

jobRun(expr) ​

Run the given expression in the context of a background job. If the expression evaluates to a Ref which references a job record, then run that job.

The job is run in the background using admin permissions.

See job.


meta ​

admin✓
deffunc:jobRun
isfunc
liblib:job
namejobRun

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobSleep ​

jobSleep(dur) ​

Sleep for the given duration. If the current context is not actively running inside a job, this is a no-op.

Side effects:

  • pauses the job for the given duration

meta ​

admin✓
deffunc:jobSleep
isfunc
liblib:job
namejobSleep

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobStatus ​

jobStatus(handle) ​

Return the status for a single job identified by handle. Throw Err if job ext not installed or handle does map to a valid job. Columns:

  • handle: unique id for job
  • jobStatus: pending, running, cancelling, doneCancel, doneErr, doneOk
  • dis: summary display of job
  • progress: integer percent complete between 0 and 100
  • progressMsg: current progress activity
  • started: datetime job was started or null if not run yet
  • runtime: duration of job run

meta ​

deffunc:jobStatus
isfunc
liblib:job
namejobStatus

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

func:jobStatusAll ​

jobStatusAll() ​

Return the status for all the known jobs. Throw Err if job ext not installed. Result grid uses same format as jobStatus.


meta ​

deffunc:jobStatusAll
isfunc
liblib:job
namejobStatusAll

supertypes ​

featureFeature namespace of definitions formatted as feature:name
funcAxon function


def ​

job ​

Background job


meta ​

defjob
iconFlagOutlined
isentity
liblib:job
mandatory✓

supertypes ​

markerMarker labels a dict with typing information
entityTop-level dicts with a unique identifier

tags ​

disDisplay name for an entity
disabledIndicates the entity is in a disabled state
idDefines the unique identifier of an entity in system using a ref value type
jobExprAxon expression to execute for job
jobScheduleSpecifies when a job is scheduled using a string format
jobStatusCurrent status of a job


def ​

jobExpr ​

Axon expression to execute for job


meta ​

defjobExpr
isstr
liblib:job
multiLine✓
tagOnjob
uiInputui::StrInput

supertypes ​

valData value type
scalarScalar is an atomic value kind
strUnicode string of characters


def ​

jobSchedule ​

Specifies when a job is scheduled using a string format:

onetime 2010-07-04T12:00:00-04:00 New_York
every 15min
every 2hr
at 00:00
at 6:00,20:00
on tue at 16:00
on sat,sun at 02:00
on 1,15 at 02:00

See job.


meta ​

defjobSchedule
isstr
liblib:job
tagOnjob
uiInputui::JobScheduleInput

supertypes ​

valData value type
scalarScalar is an atomic value kind
strUnicode string of characters


def ​

jobStatus ​

Current status of a job


meta ​

defjobStatus
enumSee below
isstr
liblib:job
tagOnjob
transient✓

enum ​

pending
running
cancelling
doneOk
doneErr
doneCancel

supertypes ​

valData value type
scalarScalar is an atomic value kind
strUnicode string of characters


def ​

view:jobs ​

Manage records used to run background jobs


meta ​

admin✓
appNametools
defview:jobs
disKeyui::job
iconview
isview
liblib:job

supertypes ​

featureFeature namespace of definitions formatted as feature:name
viewFresco UI view

src ​

view:    { inherit:"tile" }
layout:  { var kind:"Str" defVal:"grid 1x2; 0 0 1 1; 0 1 1 1"}
recs: Trio:
  view:    { inherit:"table" defName:"job" }
  new:     { action:"recNew" }
  edit:    { action:"recEdit" }
  trash:   { action:"recTrash" }
  sep1:    { action:"uiSep" }
  run:     { disKey:"ui::run" action:"jobRun" select }
  sep2:    { action:"uiSep" }
  debug:   { disKey:"ui::debug" action:"jobDebug3" }
  sep3:    { action:"uiSep" }
  enable:  { action:"recEnable" }
  disable: { action:"recDisable" }
  data:    { expr:"readAll(job).sortDis" }
status: Trio:
  view:    { inherit:"table" }
  cancel:  { disKey:"ui::cancel" action:"jobCancel" select tag:"handle" }
  details: { disKey:"ui::details" action:"jobLog" select tag:"handle" }
  data:    { expr:"jobStatusAll()" }