@xeplr/ui-jobs 1.6.5 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/package.json +1 -1
- package/src/index.jsx +1 -0
- package/src/lib/incrementalModel.js +57 -0
- package/src/lib/newJobModel.js +41 -0
- package/src/pages/Jobs.jsx +53 -5
- package/src/useJobsUrl.js +4 -1
package/README.md
CHANGED
|
@@ -130,6 +130,7 @@ Three steps (four with `chainsApiBase`), all reachable at any time, with **Save
|
|
|
130
130
|
1. **Job** — name, description, action.
|
|
131
131
|
2. **Inputs** — a control per field of the action's `inputSchema`: a select for `options`, true/false for `boolean`, a number input, a JSON textarea for `object` / `array` (invalid JSON blocks the save), text otherwise. Fields marked `system: true` are never shown — the server fills them, and rendering them would ask for a credential. Optional fields with a `default` are under **Advanced**, whose summary lists their values. Values are converted to the declared types on save, since `@xeplr/schema-handler` rejects `"1000"` for a number.
|
|
132
132
|
3. **Schedule** — cron (`CronScheduleField`), status, **Start from** (`startAt`), **Retry on failure** (0–3, default 0), **Give up after** (`timeoutMinutes`, blank = server default of 30).
|
|
133
|
+
**Each run takes** (only for an action with a `window` input) — which rows each run takes, the job's decision (`incrementalMode`): *Everything, each run* (none), *Since the last successful run* (`period`), or *Continue from what is already loaded* (`watermark`, offered only when the action's `window` field says `watermark: true`, since the action has to read its own target). With `watermark`, **Also re-read the last … days** (`reloadDays`). A range given when the job is started always wins. Logic in `src/lib/incrementalModel.js`.
|
|
133
134
|
4. **After this job** (`JobChainSection`, `useJobChainController`, `lib/jobChainModel.js`) — "Run another job when this one finishes", then rules checked top to bottom, first match wins. Each rule has:
|
|
134
135
|
- **If**: a JSON condition, e.g. `{ "field": "status", "op": "=", "value": "success" }`, or `{ "all": [ … ] }` / `{ "any": [ … ] }`. Empty means "otherwise" and must come last.
|
|
135
136
|
- **Run**: another job.
|
|
@@ -150,6 +151,18 @@ A job's saved `acceptsInputs` (what the job before may fill) is kept as it is bu
|
|
|
150
151
|
|
|
151
152
|
A new job with a schedule defaults to **Paused**, so it can be written now and started later. A job with no schedule is saved as `ready` and runs only when triggered.
|
|
152
153
|
|
|
154
|
+
## Opening a new job already filled in
|
|
155
|
+
|
|
156
|
+
A host can link straight to a new job with its action and inputs chosen, e.g. BI's "+ New job" on a movement:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
import { newJobQuery } from '@xeplr/ui-jobs'
|
|
160
|
+
navigate(jobsPath + newJobQuery({ action: 'move-run', inputs: { moveId }, name: 'Move: Orders' }))
|
|
161
|
+
// → ?edit=new&action=move-run&inputs={"moveId":"…"}&name=Move%3A%20Orders
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The link is not trusted: an action the server does not offer, or inputs that are not a JSON object, start a blank job instead (`src/lib/newJobModel.js`).
|
|
165
|
+
|
|
153
166
|
## Option sources
|
|
154
167
|
|
|
155
168
|
An input field can ask for a choice from a kind of thing — `optionsFrom: 'connections'`. This package never learns what a connection is; your app says where each kind is listed:
|
package/package.json
CHANGED
package/src/index.jsx
CHANGED
|
@@ -103,3 +103,4 @@ export { ROUTES as jobsRoutes } from './routes.js';
|
|
|
103
103
|
// and a copy of it in the host is a second implementation free to drift.
|
|
104
104
|
export { default as CronScheduleField, CRON_PRESETS } from './components/CronScheduleField.jsx';
|
|
105
105
|
export { describeCron } from './pages/jobsList.js';
|
|
106
|
+
export { newJobQuery } from './lib/newJobModel.js';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// WHICH ROWS EACH RUN TAKES — the job's decision (@xeplr/jobs'
|
|
2
|
+
// incrementalMode and reloadDays), not the action's.
|
|
3
|
+
//
|
|
4
|
+
// Offered only for an action that takes a date range: one whose inputSchema
|
|
5
|
+
// has a `window` field. "Continue from what is already loaded" needs the
|
|
6
|
+
// action to read its own target, so it is offered only when that field says
|
|
7
|
+
// it can (`watermark: true`, as BI's move-run does). An action that never
|
|
8
|
+
// declared it would be handed a word it does not know.
|
|
9
|
+
//
|
|
10
|
+
// Pure: no React.
|
|
11
|
+
|
|
12
|
+
export const NONE = ''
|
|
13
|
+
|
|
14
|
+
/** The `window` field of an action, or null when it takes no range. */
|
|
15
|
+
export function windowField(action) {
|
|
16
|
+
return ((action && action.inputSchema) || []).find((f) => f && f.name === 'window') || null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The choices this action allows, in the order they are shown. */
|
|
20
|
+
export function incrementalChoices(action) {
|
|
21
|
+
const field = windowField(action)
|
|
22
|
+
if (!field) return []
|
|
23
|
+
const out = [
|
|
24
|
+
{ value: NONE, label: 'Everything, each run', help: 'No range is worked out: each run takes what its inputs say.' },
|
|
25
|
+
{ value: 'period', label: 'Since the last successful run', help: 'Each run covers from where the last successful one stopped, up to now. A failed run is picked up by the next.' }
|
|
26
|
+
]
|
|
27
|
+
if (field.watermark) {
|
|
28
|
+
out.push({ value: 'watermark', label: 'Continue from what is already loaded', help: 'Each run reads the highest value the target already holds and takes everything after it.' })
|
|
29
|
+
}
|
|
30
|
+
return out
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The two fields as the form holds them: mode, and days as text. */
|
|
34
|
+
export function incrementalDraft(job) {
|
|
35
|
+
return {
|
|
36
|
+
incrementalMode: (job && job.incrementalMode) || NONE,
|
|
37
|
+
reloadDays: job && job.reloadDays ? String(job.reloadDays) : ''
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What is saved. A mode the action no longer allows (the action was changed)
|
|
43
|
+
* is cleared rather than kept; reloadDays belongs to 'watermark' only.
|
|
44
|
+
* Returns { body } or { problem } in words.
|
|
45
|
+
*/
|
|
46
|
+
export function incrementalBody(draft, action) {
|
|
47
|
+
const allowed = incrementalChoices(action).map((c) => c.value)
|
|
48
|
+
const mode = allowed.includes(draft.incrementalMode) ? draft.incrementalMode : NONE
|
|
49
|
+
if (mode !== 'watermark') return { body: { incrementalMode: mode || null, reloadDays: null } }
|
|
50
|
+
const text = String(draft.reloadDays == null ? '' : draft.reloadDays).trim()
|
|
51
|
+
if (text === '') return { body: { incrementalMode: mode, reloadDays: null } }
|
|
52
|
+
const days = Number(text)
|
|
53
|
+
if (!Number.isInteger(days) || days < 1) {
|
|
54
|
+
return { problem: 'Re-read the last … days takes a whole number of days, 1 or more — or leave it empty.' }
|
|
55
|
+
}
|
|
56
|
+
return { body: { incrementalMode: mode, reloadDays: days } }
|
|
57
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// A NEW JOB, STARTED FROM SOMEWHERE ELSE — the host opens the editor already
|
|
2
|
+
// filled in, so "+ New job" on a movement does not make somebody pick the
|
|
3
|
+
// action and the movement again:
|
|
4
|
+
//
|
|
5
|
+
// ?edit=new&action=move-run&inputs={"moveId":"abc"}&name=Nightly%20orders
|
|
6
|
+
//
|
|
7
|
+
// What arrives in a URL is not trusted: the action must be one this server
|
|
8
|
+
// offers, and inputs must be a JSON object. Anything else starts a blank job.
|
|
9
|
+
//
|
|
10
|
+
// Pure: no React.
|
|
11
|
+
|
|
12
|
+
/** The new job's starting values from the URL's action / inputs / name. */
|
|
13
|
+
export function newJobFrom(params, actions) {
|
|
14
|
+
const get = (k) => (params && typeof params.get === 'function' ? params.get(k) : params && params[k]) || null
|
|
15
|
+
const action = get('action')
|
|
16
|
+
const known = action && (actions || []).some((a) => a && a.name === action)
|
|
17
|
+
const out = {}
|
|
18
|
+
if (known) out.actionName = action
|
|
19
|
+
const text = get('inputs')
|
|
20
|
+
if (known && text) {
|
|
21
|
+
try {
|
|
22
|
+
const parsed = JSON.parse(text)
|
|
23
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) out.inputs = parsed
|
|
24
|
+
} catch (e) { /* not JSON: no inputs */ }
|
|
25
|
+
}
|
|
26
|
+
const name = get('name')
|
|
27
|
+
if (name) out.name = String(name).slice(0, 200)
|
|
28
|
+
return out
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The URL keys this reads, cleared when the editor closes. */
|
|
32
|
+
export const NEW_JOB_KEYS = ['action', 'inputs', 'name']
|
|
33
|
+
|
|
34
|
+
/** The link a host builds: the jobs page's path plus these. */
|
|
35
|
+
export function newJobQuery({ action, inputs, name }) {
|
|
36
|
+
const q = new URLSearchParams({ edit: 'new' })
|
|
37
|
+
if (action) q.set('action', action)
|
|
38
|
+
if (inputs) q.set('inputs', JSON.stringify(inputs))
|
|
39
|
+
if (name) q.set('name', name)
|
|
40
|
+
return '?' + q.toString()
|
|
41
|
+
}
|
package/src/pages/Jobs.jsx
CHANGED
|
@@ -17,6 +17,8 @@ import { useRunContract } from '../useRunContract.js'
|
|
|
17
17
|
import { useJobsUrl } from '../useJobsUrl.js'
|
|
18
18
|
import { useJobChainController } from '../useJobChainController.js'
|
|
19
19
|
import { acceptableFields } from '../lib/jobChainModel.js'
|
|
20
|
+
import { incrementalChoices, incrementalDraft, incrementalBody } from '../lib/incrementalModel.js'
|
|
21
|
+
import { newJobFrom, NEW_JOB_KEYS } from '../lib/newJobModel.js'
|
|
20
22
|
import './Jobs.css'
|
|
21
23
|
|
|
22
24
|
// SCHEDULED JOBS — @xeplr/jobs, in this product's chrome.
|
|
@@ -515,7 +517,9 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved, stepKey, onStep }) {
|
|
|
515
517
|
// Inputs the job BEFORE this one in a chain may fill (@xeplr/jobs'
|
|
516
518
|
// acceptsInputs). No longer edited here — a job takes what it needs from
|
|
517
519
|
// the job before through its parameters — but kept as saved.
|
|
518
|
-
acceptsInputs: Array.isArray(job?.acceptsInputs) ? job.acceptsInputs : []
|
|
520
|
+
acceptsInputs: Array.isArray(job?.acceptsInputs) ? job.acceptsInputs : [],
|
|
521
|
+
// WHICH ROWS EACH RUN TAKES — the job's decision (see incrementalModel).
|
|
522
|
+
...incrementalDraft(job)
|
|
519
523
|
}))
|
|
520
524
|
const [saving, setSaving] = useState(false)
|
|
521
525
|
const chain = useJobChainController(job?.id || null)
|
|
@@ -539,6 +543,12 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved, stepKey, onStep }) {
|
|
|
539
543
|
raiseSnackbar(`Not valid JSON: ${broken.join(', ')}`, { design: 'error' })
|
|
540
544
|
return
|
|
541
545
|
}
|
|
546
|
+
const incremental = incrementalBody(draft, action)
|
|
547
|
+
if (incremental.problem) {
|
|
548
|
+
setStep(2)
|
|
549
|
+
raiseSnackbar(incremental.problem, { design: 'error' })
|
|
550
|
+
return
|
|
551
|
+
}
|
|
542
552
|
// The rules are checked BEFORE anything is saved, so a typo in one does
|
|
543
553
|
// not leave a job saved and its chain not.
|
|
544
554
|
const chainCheck = chain.check(job?.id)
|
|
@@ -579,7 +589,8 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved, stepKey, onStep }) {
|
|
|
579
589
|
// strings is what made every number/boolean field fail validation.
|
|
580
590
|
inputs: fromFieldValues(action, draft.inputs),
|
|
581
591
|
// Only names that are still fields of the chosen action.
|
|
582
|
-
acceptsInputs: draft.acceptsInputs.filter((n) => acceptableFields(fields).some((f) => f.name === n))
|
|
592
|
+
acceptsInputs: draft.acceptsInputs.filter((n) => acceptableFields(fields).some((f) => f.name === n)),
|
|
593
|
+
...incremental.body
|
|
583
594
|
})
|
|
584
595
|
if (!job?.id) {
|
|
585
596
|
const newId = res && Array.isArray(res.updatedIds) ? res.updatedIds[0] : null
|
|
@@ -756,6 +767,38 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved, stepKey, onStep }) {
|
|
|
756
767
|
</div>
|
|
757
768
|
)}
|
|
758
769
|
|
|
770
|
+
{/* WHICH ROWS EACH RUN TAKES — only for an action that takes a range.
|
|
771
|
+
The job decides this, not the action: the same movement can run
|
|
772
|
+
nightly "since the last run" and by hand for one month. */}
|
|
773
|
+
{incrementalChoices(action).length > 0 && (() => {
|
|
774
|
+
const choices = incrementalChoices(action)
|
|
775
|
+
const on = choices.find((c) => c.value === draft.incrementalMode) || choices[0]
|
|
776
|
+
return (
|
|
777
|
+
<>
|
|
778
|
+
<div className="jb-field">
|
|
779
|
+
<label htmlFor="jb-incremental">Each run takes</label>
|
|
780
|
+
<select id="jb-incremental" value={on.value} onChange={(e) => patch({ incrementalMode: e.target.value })}>
|
|
781
|
+
{choices.map((c) => <option key={c.value} value={c.value}>{c.label}</option>)}
|
|
782
|
+
</select>
|
|
783
|
+
<span className="jb-help">
|
|
784
|
+
{on.help} A range given when it is started (by a workflow, or by the job before it) always wins.
|
|
785
|
+
</span>
|
|
786
|
+
</div>
|
|
787
|
+
{on.value === 'watermark' && (
|
|
788
|
+
<div className="jb-field">
|
|
789
|
+
<label htmlFor="jb-reload">Also re-read the last</label>
|
|
790
|
+
<input id="jb-reload" type="number" min="1" step="1" placeholder="—"
|
|
791
|
+
value={draft.reloadDays} onChange={(e) => patch({ reloadDays: e.target.value })} />
|
|
792
|
+
<span className="jb-help">
|
|
793
|
+
Days. Rows changed after they were loaded (a late payment, an edited order) are below the highest
|
|
794
|
+
value, so they are only picked up by re-reading. Empty: no re-read.
|
|
795
|
+
</span>
|
|
796
|
+
</div>
|
|
797
|
+
)}
|
|
798
|
+
</>
|
|
799
|
+
)
|
|
800
|
+
})()}
|
|
801
|
+
|
|
759
802
|
{/* RETRY — off by default. An action that failed once usually fails
|
|
760
803
|
again, and silently re-running something that moves data is worse
|
|
761
804
|
than leaving it failed and visible. */}
|
|
@@ -865,11 +908,14 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
|
|
|
865
908
|
const openRuns = url.runs
|
|
866
909
|
const setOpenRuns = (id) => url.set({ runs: id, log: null })
|
|
867
910
|
// ?edit=new is a new job; ?edit=<id> is that job once the list has loaded.
|
|
868
|
-
|
|
911
|
+
// A new job may arrive filled in from a link (newJobFrom), once the actions
|
|
912
|
+
// it is checked against have loaded.
|
|
913
|
+
const editing = url.edit === 'new' ? newJobFrom(url.params, actions)
|
|
869
914
|
: url.edit && jobs ? (jobs.find((j) => j.id === url.edit) || null)
|
|
870
915
|
: null
|
|
871
916
|
const editMissing = Boolean(url.edit && url.edit !== 'new' && jobs && !editing)
|
|
872
|
-
const setEditing = (job) => url.set({ edit: job ? (job.id || 'new') : null, step: null }
|
|
917
|
+
const setEditing = (job) => url.set(Object.assign({ edit: job ? (job.id || 'new') : null, step: null },
|
|
918
|
+
Object.fromEntries(NEW_JOB_KEYS.map((k) => [k, null]))))
|
|
873
919
|
const [busyId, setBusyId] = useState(null)
|
|
874
920
|
// Bumped by anything that creates an occurrence, so an OPEN history refetches
|
|
875
921
|
// rather than sitting there showing the state from before Run now was pressed.
|
|
@@ -1048,7 +1094,9 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
|
|
|
1048
1094
|
|
|
1049
1095
|
{editing && (
|
|
1050
1096
|
<JobEditor
|
|
1051
|
-
|
|
1097
|
+
// A new job filled in from a link is checked against the actions,
|
|
1098
|
+
// so it starts again once they have loaded.
|
|
1099
|
+
key={url.edit + (url.edit === 'new' && actions.length ? ':ready' : '')}
|
|
1052
1100
|
job={editing.id ? editing : null}
|
|
1053
1101
|
jobs={jobs || []}
|
|
1054
1102
|
actions={actions}
|
package/src/useJobsUrl.js
CHANGED
|
@@ -5,6 +5,7 @@ import { useSearchParams } from 'react-router-dom'
|
|
|
5
5
|
// link lands on exactly this screen:
|
|
6
6
|
//
|
|
7
7
|
// ?edit=<jobId> | ?edit=new the editor, open on that job (or a new one)
|
|
8
|
+
// &action=…&inputs=…&name=… a new job already filled in (lib/newJobModel.js)
|
|
8
9
|
// &step=job|inputs|schedule|after its step
|
|
9
10
|
// ?runs=<jobId> that job's run history, expanded
|
|
10
11
|
// &log=<occurrenceId> one run's log, open
|
|
@@ -12,7 +13,7 @@ import { useSearchParams } from 'react-router-dom'
|
|
|
12
13
|
//
|
|
13
14
|
// Opening and closing things adds a history entry, so Back closes them;
|
|
14
15
|
// typing and switching steps replaces it, so Back is not a keystroke undo.
|
|
15
|
-
const KEYS = ['edit', 'step', 'runs', 'log', 'q', 'sort']
|
|
16
|
+
const KEYS = ['edit', 'step', 'runs', 'log', 'q', 'sort', 'action', 'inputs', 'name']
|
|
16
17
|
|
|
17
18
|
export function useJobsUrl() {
|
|
18
19
|
const [params, setParams] = useSearchParams()
|
|
@@ -37,6 +38,8 @@ export function useJobsUrl() {
|
|
|
37
38
|
log: read('log'),
|
|
38
39
|
q: read('q') || '',
|
|
39
40
|
sort: read('sort') || 'name',
|
|
41
|
+
// The raw params, for a new job filled in from the link (newJobFrom).
|
|
42
|
+
params: params,
|
|
40
43
|
set: set,
|
|
41
44
|
KEYS: KEYS
|
|
42
45
|
}
|