Skip to content

Commit aa5189f

Browse files
radakamdenik
authored andcommitted
Add job_runs lifecycle.triggers.on_value_change
Adds `lifecycle.triggers.on_value_change` for `resources.job_runs` (direct engine): watch an interpolated expression (a variable, workspace field, or a ${resources.*} reference) and re-fire the run when its resolved value changes. - Snapshot the expression before interpolation and use the trimmed form as the stable state key (SnapshotJobRunValueTriggers), so a value change is a modification of one entry rather than a change to the set of watches. - Resolution reuses existing mechanisms: normal variable interpolation resolves ${var.*}/${workspace.*} into triggers_state, and the planner resolves ${resources.*} via the references PrepareInputConfig registers, so the deploy graph depends on them. The resolved value is stored in state directly. - Removing or adding a watch is classified in OverrideChangeDesc alongside on_file_change: adding/changing recreates the run, removing keeps it. Built on the on_file_change work (#6309): no PersistState, path.String()-based change classification, prevent_destroy left to the generic checkForPreventDestroy. Co-authored-by: Isaac
1 parent f19b229 commit aa5189f

32 files changed

Lines changed: 895 additions & 19 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* direct: `resources.job_runs` can set `lifecycle.triggers.on_value_change` to an interpolated expression (for example `${resources.jobs.foo.id}`) to re-fire the run when that resolved value changes or a watch is added. Resolved values are recorded as SHA-256 hashes; removing a watch leaves the run in place. ([#6341](https://github.com/databricks/cli/pull/6341))

‎acceptance/bundle/refschema/out.fields.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,13 @@ resources.job_runs.*.lifecycle.triggers []resources.JobRunTrigger INPUT
929929
resources.job_runs.*.lifecycle.triggers[*] resources.JobRunTrigger INPUT
930930
resources.job_runs.*.lifecycle.triggers[*].on_bundle_deploy *bool INPUT
931931
resources.job_runs.*.lifecycle.triggers[*].on_file_change *string INPUT
932+
resources.job_runs.*.lifecycle.triggers[*].on_value_change *string INPUT
932933
resources.job_runs.*.lifecycle.triggers_state *resources.JobRunTriggersState INPUT STATE
933934
resources.job_runs.*.lifecycle.triggers_state.on_bundle_deploy string INPUT STATE
934935
resources.job_runs.*.lifecycle.triggers_state.on_file_change map[string]string INPUT STATE
935936
resources.job_runs.*.lifecycle.triggers_state.on_file_change.* string INPUT STATE
937+
resources.job_runs.*.lifecycle.triggers_state.on_value_change map[string]string INPUT STATE
938+
resources.job_runs.*.lifecycle.triggers_state.on_value_change.* string INPUT STATE
936939
resources.job_runs.*.modified_status string INPUT
937940
resources.job_runs.*.notebook_params map[string]string ALL
938941
resources.job_runs.*.notebook_params.* string ALL
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
bundle:
2+
name: job-runs-on-value-change
3+
4+
variables:
5+
stamp:
6+
default: v1
7+
other:
8+
default: a1
9+
number:
10+
default: 42
11+
long:
12+
default: a-long-non-hex-watched-value-used-to-exercise-a-lengthy-string-zzz
13+
14+
resources:
15+
jobs:
16+
my_job:
17+
name: my-job
18+
tasks:
19+
- task_key: main
20+
notebook_task:
21+
notebook_path: /Workspace/test
22+
23+
job_runs:
24+
my_run:
25+
job_id: ${resources.jobs.my_job.id}
26+
lifecycle:
27+
triggers:
28+
- on_file_change: watched.txt
29+
- on_value_change: " ${var.stamp} "
30+
- on_value_change: ${var.other}
31+
- on_value_change: ${var.number}
32+
- on_value_change: ${workspace.root_path}
33+
- on_value_change: ${var.long}

‎acceptance/bundle/resources/job_runs/on_value_change/out.test.toml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
2+
=== first deploy triggers a run
3+
>>> [CLI] bundle deploy
4+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
5+
Output from job_runs.my_run: id=[MY_RUN_ID]: Run URL: [DATABRICKS_URL]/jobs/[MY_JOB_ID]/runs/[MY_RUN_ID]?o=[NUMID]
6+
Output from job_runs.my_run: id=[MY_RUN_ID]: SUCCESS
7+
Created job_runs.my_run
8+
Created jobs.my_job
9+
Files: 4 uploaded, 0 deleted
10+
Resources: 2 created, 0 changed, 0 deleted, 0 unchanged
11+
12+
>>> read_id.py my_job
13+
[MY_JOB_ID]
14+
15+
>>> print_requests.py //jobs/run-now
16+
{
17+
"method": "POST",
18+
"path": "/api/2.2/jobs/run-now",
19+
"body": {
20+
"idempotency_token": "[UUID]",
21+
"job_id": [MY_JOB_ID]
22+
}
23+
}
24+
25+
=== resolved values are stored as hashes
26+
>>> print_state.py
27+
{
28+
"on_file_change": {
29+
"watched.txt": "[FILE_HASH][0]"
30+
},
31+
"on_value_change": {
32+
"${var.long}": "a-long-non-hex-watched-value-used-to-exercise-a-lengthy-string-zzz",
33+
"${var.number}": "42",
34+
"${var.other}": "a1",
35+
"${var.stamp}": "v1",
36+
"${workspace.root_path}": "/Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default"
37+
}
38+
}
39+
40+
=== redeploy with unchanged values plans nothing
41+
>>> [CLI] bundle plan
42+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
43+
44+
>>> [CLI] bundle deploy
45+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
46+
Files: 0 uploaded, 0 deleted
47+
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged
48+
49+
>>> print_requests.py //jobs/run-now
50+
51+
=== changing one value re-fires
52+
>>> [CLI] bundle plan
53+
recreate job_runs.my_run
54+
55+
Plan: 1 to add, 0 to change, 1 to delete, 1 unchanged
56+
57+
>>> changes
58+
{
59+
"lifecycle.triggers_state.on_value_change['${var.stamp}']": {
60+
"action": "recreate",
61+
"reason": "immutable",
62+
"old": "v1",
63+
"new": "v2"
64+
},
65+
"result_state": {
66+
"action": "skip",
67+
"reason": "remote_already_set",
68+
"new": "SUCCESS",
69+
"remote": "SUCCESS"
70+
}
71+
}
72+
73+
>>> [CLI] bundle deploy
74+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
75+
Output from job_runs.my_run: id=[MY_RUN_ID_2]: Run URL: [DATABRICKS_URL]/jobs/[MY_JOB_ID]/runs/[MY_RUN_ID_2]?o=[NUMID]
76+
Output from job_runs.my_run: id=[MY_RUN_ID_2]: SUCCESS
77+
Recreated job_runs.my_run
78+
Files: 1 uploaded, 0 deleted
79+
Resources: 1 created, 0 changed, 1 deleted, 1 unchanged
80+
81+
>>> print_requests.py --keep //jobs/runs/delete
82+
{
83+
"method": "POST",
84+
"path": "/api/2.2/jobs/runs/delete",
85+
"body": {
86+
"run_id": [MY_RUN_ID]
87+
}
88+
}
89+
90+
>>> print_requests.py //jobs/run-now
91+
{
92+
"method": "POST",
93+
"path": "/api/2.2/jobs/run-now",
94+
"body": {
95+
"idempotency_token": "[UUID]",
96+
"job_id": [MY_JOB_ID]
97+
}
98+
}
99+
100+
=== removing one on_value_change through a saved plan does not run
101+
>>> [CLI] bundle plan
102+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
103+
104+
>>> [CLI] bundle deploy --plan tmp.plan.json
105+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
106+
Files: 2 uploaded, 0 deleted
107+
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged
108+
109+
>>> print_requests.py //jobs/run-now
110+
111+
=== re-adding the same on_value_change after the saved plan re-fires
112+
>>> [CLI] bundle plan
113+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
114+
115+
>>> changes
116+
{
117+
"result_state": {
118+
"action": "skip",
119+
"reason": "remote_already_set",
120+
"new": "SUCCESS",
121+
"remote": "SUCCESS"
122+
}
123+
}
124+
125+
>>> [CLI] bundle deploy
126+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
127+
Files: 1 uploaded, 0 deleted
128+
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged
129+
130+
>>> print_requests.py --keep //jobs/runs/delete
131+
132+
>>> print_requests.py //jobs/run-now
133+
134+
=== removing the same on_value_change again does not run
135+
>>> [CLI] bundle plan
136+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
137+
138+
>>> [CLI] bundle deploy
139+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
140+
Files: 1 uploaded, 0 deleted
141+
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged
142+
143+
>>> print_requests.py //jobs/run-now
144+
145+
=== changing a remaining watch to a removed value re-fires
146+
>>> [CLI] bundle plan
147+
recreate job_runs.my_run
148+
149+
Plan: 1 to add, 0 to change, 1 to delete, 1 unchanged
150+
151+
>>> changes
152+
{
153+
"lifecycle.triggers_state.on_value_change['${var.stamp}']": {
154+
"action": "recreate",
155+
"reason": "immutable",
156+
"old": "v2",
157+
"new": "a1"
158+
},
159+
"result_state": {
160+
"action": "skip",
161+
"reason": "remote_already_set",
162+
"new": "SUCCESS",
163+
"remote": "SUCCESS"
164+
}
165+
}
166+
167+
>>> [CLI] bundle deploy
168+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
169+
Output from job_runs.my_run: id=[MY_RUN_ID_4]: Run URL: [DATABRICKS_URL]/jobs/[MY_JOB_ID]/runs/[MY_RUN_ID_4]?o=[NUMID]
170+
Output from job_runs.my_run: id=[MY_RUN_ID_4]: SUCCESS
171+
Recreated job_runs.my_run
172+
Files: 1 uploaded, 0 deleted
173+
Resources: 1 created, 0 changed, 1 deleted, 1 unchanged
174+
175+
>>> print_requests.py --keep //jobs/runs/delete
176+
{
177+
"method": "POST",
178+
"path": "/api/2.2/jobs/runs/delete",
179+
"body": {
180+
"run_id": [MY_RUN_ID_2]
181+
}
182+
}
183+
184+
>>> print_requests.py //jobs/run-now
185+
{
186+
"method": "POST",
187+
"path": "/api/2.2/jobs/run-now",
188+
"body": {
189+
"idempotency_token": "[UUID]",
190+
"job_id": [MY_JOB_ID]
191+
}
192+
}
193+
194+
=== removing the remaining on_value_change entries is unchanged and does not run
195+
>>> [CLI] bundle plan
196+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
197+
198+
>>> [CLI] bundle deploy
199+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
200+
Files: 1 uploaded, 0 deleted
201+
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged
202+
203+
>>> print_requests.py //jobs/run-now
204+
205+
=== file changes still re-fire after value triggers are removed
206+
>>> [CLI] bundle plan
207+
recreate job_runs.my_run
208+
209+
Plan: 1 to add, 0 to change, 1 to delete, 1 unchanged
210+
211+
>>> changes
212+
{
213+
"lifecycle.triggers_state.on_file_change['watched.txt']": {
214+
"action": "recreate",
215+
"reason": "immutable",
216+
"old": "[FILE_HASH][0]",
217+
"new": "[FILE_HASH][1]"
218+
},
219+
"result_state": {
220+
"action": "skip",
221+
"reason": "remote_already_set",
222+
"new": "SUCCESS",
223+
"remote": "SUCCESS"
224+
}
225+
}
226+
227+
>>> [CLI] bundle deploy
228+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default/files...
229+
Output from job_runs.my_run: id=[MY_RUN_ID_5]: Run URL: [DATABRICKS_URL]/jobs/[MY_JOB_ID]/runs/[MY_RUN_ID_5]?o=[NUMID]
230+
Output from job_runs.my_run: id=[MY_RUN_ID_5]: SUCCESS
231+
Recreated job_runs.my_run
232+
Files: 1 uploaded, 0 deleted
233+
Resources: 1 created, 0 changed, 1 deleted, 1 unchanged
234+
235+
>>> print_requests.py --keep //jobs/runs/delete
236+
{
237+
"method": "POST",
238+
"path": "/api/2.2/jobs/runs/delete",
239+
"body": {
240+
"run_id": [MY_RUN_ID_4]
241+
}
242+
}
243+
244+
>>> print_requests.py //jobs/run-now
245+
{
246+
"method": "POST",
247+
"path": "/api/2.2/jobs/run-now",
248+
"body": {
249+
"idempotency_token": "[UUID]",
250+
"job_id": [MY_JOB_ID]
251+
}
252+
}
253+
254+
>>> [CLI] bundle destroy --auto-approve
255+
The following resources will be deleted:
256+
delete resources.job_runs.my_run
257+
delete resources.jobs.my_job
258+
259+
All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/job-runs-on-value-change/default
260+
261+
Destroy: 2 deleted

0 commit comments

Comments
 (0)