Add on_create trigger

This commit is contained in:
Tim Van Baak 2024-09-21 14:55:10 -07:00
parent c21f55da8d
commit 0c4177b783
6 changed files with 68 additions and 2 deletions

View File

@ -330,6 +330,8 @@ def update_items(source: LocalSource, fetched_items: List[Item]):
Update the source with a batch of new items, doing creations, updates, and
deletions as necessary.
"""
config = source.get_config()
# Get a list of item ids that already existed for this source.
prior_ids = source.get_item_ids()
print(f"Found {len(prior_ids)} prior items", file=sys.stderr)
@ -345,9 +347,19 @@ def update_items(source: LocalSource, fetched_items: List[Item]):
# Write all the new items to the source directory.
for item in new_items:
# TODO: support on-create trigger
source.save_item(item)
# If the source has an on-create trigger, run it for new items
if "on_create" in config.get("action", {}):
for item in new_items:
try:
execute_action(source, item["id"], "on_create")
except Exception as ex:
print(
f"on_create failed for {source.source_name}/{item['id']}:\n{ex}",
file=sys.stderr,
)
# Update the other items using the fetched items' values.
for upd_item in upd_items:
old_item = source.get_item(upd_item["id"])

View File

@ -2,6 +2,7 @@
"demo": [
"demo_basic_callback",
"demo_logging",
"demo_raw_sh"
"demo_raw_sh",
"demo_oncreate"
]
}

View File

@ -23,6 +23,7 @@ def using_source() -> Callable:
clean_source(source_path)
sources.append(source_path)
return LocalSource(test_data, name)
yield _using_source
for source_path in sources:

View File

@ -0,0 +1,22 @@
{
"action": {
"fetch": {
"exe": "./update.py",
"args": [
"fetch"
]
},
"update": {
"exe": "./update.py",
"args": [
"update"
]
},
"on_create": {
"exe": "./update.py",
"args": [
"update"
]
}
}
}

29
tests/demo_oncreate/update.py Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
import argparse, json, sys, time
parser = argparse.ArgumentParser()
parser.add_argument("action")
args = parser.parse_args()
print("args:", args, file=sys.stderr, flush=True)
if args.action == "fetch":
print(
json.dumps(
{
"id": str(int(time.time())),
"title": "Title has not been updated",
"action": {
"update": 0,
},
}
)
)
if args.action == "update" or args.action == "on_create":
item = sys.stdin.readline()
item = json.loads(item)
item["action"]["update"] += 1
item["title"] = f"Updated {item['action']['update']} times"
print(json.dumps(item))

View File

@ -8,6 +8,7 @@ def test_default_source(using_source):
fetch = fetch_items(source)
assert len(fetch) == 0
def test_basic_lifecycle(using_source):
source: LocalSource = using_source("test_inbox")
state = {"inbox": [{"id": "first"}]}