|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +""" |
| 5 | +Resource Watch Feature Example |
| 6 | +
|
| 7 | +This example demonstrates how to use the resource watch feature in OpenViking. |
| 8 | +The watch feature allows you to automatically re-process resources at specified |
| 9 | +intervals. |
| 10 | +
|
| 11 | +Key features: |
| 12 | +- Create resources with watch enabled |
| 13 | +- Update watch intervals (cancel then re-create) |
| 14 | +- Cancel watch tasks |
| 15 | +- Handle conflict errors |
| 16 | +""" |
| 17 | + |
| 18 | +import asyncio |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from openviking import AsyncOpenViking |
| 22 | +from openviking_cli.exceptions import ConflictError |
| 23 | + |
| 24 | + |
| 25 | +async def example_basic_watch(): |
| 26 | + client = AsyncOpenViking(path="./data_watch_example") |
| 27 | + await client.initialize() |
| 28 | + |
| 29 | + try: |
| 30 | + test_file = Path("./test_resource.md") |
| 31 | + test_file.write_text( |
| 32 | + """# Test Resource |
| 33 | +
|
| 34 | +## Content |
| 35 | +This is a test resource for watch functionality. |
| 36 | +
|
| 37 | +## Version |
| 38 | +Version: 1.0 |
| 39 | +""" |
| 40 | + ) |
| 41 | + |
| 42 | + to_uri = "viking://resources/watched_resource" |
| 43 | + |
| 44 | + print("\nAdding resource with watch_interval=60.0 minutes...") |
| 45 | + result = await client.add_resource( |
| 46 | + path=str(test_file), |
| 47 | + to=to_uri, |
| 48 | + reason="Example: monitoring a document", |
| 49 | + instruction="Check for updates and re-index", |
| 50 | + watch_interval=60.0, |
| 51 | + ) |
| 52 | + |
| 53 | + print("Resource added successfully!") |
| 54 | + print(f" Root URI: {result['root_uri']}") |
| 55 | + finally: |
| 56 | + await client.close() |
| 57 | + |
| 58 | + |
| 59 | +async def example_update_watch_interval(): |
| 60 | + client = AsyncOpenViking(path="./data_watch_example") |
| 61 | + await client.initialize() |
| 62 | + |
| 63 | + try: |
| 64 | + test_file = Path("./test_resource.md") |
| 65 | + to_uri = "viking://resources/watched_resource" |
| 66 | + |
| 67 | + print("\nUpdating watch interval by canceling then re-creating...") |
| 68 | + await client.add_resource( |
| 69 | + path=str(test_file), |
| 70 | + to=to_uri, |
| 71 | + watch_interval=0, |
| 72 | + ) |
| 73 | + await client.add_resource( |
| 74 | + path=str(test_file), |
| 75 | + to=to_uri, |
| 76 | + reason="Updated: more frequent monitoring", |
| 77 | + watch_interval=120.0, |
| 78 | + ) |
| 79 | + print("Watch task updated successfully!") |
| 80 | + finally: |
| 81 | + await client.close() |
| 82 | + |
| 83 | + |
| 84 | +async def example_cancel_watch(): |
| 85 | + client = AsyncOpenViking(path="./data_watch_example") |
| 86 | + await client.initialize() |
| 87 | + |
| 88 | + try: |
| 89 | + test_file = Path("./test_resource.md") |
| 90 | + to_uri = "viking://resources/watched_resource" |
| 91 | + |
| 92 | + print("\nCancelling watch by setting interval to 0...") |
| 93 | + await client.add_resource( |
| 94 | + path=str(test_file), |
| 95 | + to=to_uri, |
| 96 | + watch_interval=0, |
| 97 | + ) |
| 98 | + print("Watch task cancelled successfully!") |
| 99 | + finally: |
| 100 | + await client.close() |
| 101 | + |
| 102 | + |
| 103 | +async def example_handle_conflict(): |
| 104 | + client = AsyncOpenViking(path="./data_watch_example") |
| 105 | + await client.initialize() |
| 106 | + |
| 107 | + try: |
| 108 | + test_file = Path("./test_resource.md") |
| 109 | + to_uri = "viking://resources/conflict_example" |
| 110 | + |
| 111 | + print("\nCreating first watch task...") |
| 112 | + await client.add_resource( |
| 113 | + path=str(test_file), |
| 114 | + to=to_uri, |
| 115 | + watch_interval=30.0, |
| 116 | + ) |
| 117 | + print(" First watch task created successfully") |
| 118 | + |
| 119 | + print("\nAttempting to create second watch task for same URI...") |
| 120 | + try: |
| 121 | + await client.add_resource( |
| 122 | + path=str(test_file), |
| 123 | + to=to_uri, |
| 124 | + watch_interval=60.0, |
| 125 | + ) |
| 126 | + print(" ERROR: This should not happen!") |
| 127 | + except ConflictError as e: |
| 128 | + print(" ConflictError caught as expected!") |
| 129 | + print(f" Error message: {e}") |
| 130 | + finally: |
| 131 | + await client.close() |
| 132 | + |
| 133 | + |
| 134 | +async def main(): |
| 135 | + print("\n" + "=" * 60) |
| 136 | + print("OpenViking Resource Watch Examples") |
| 137 | + print("=" * 60) |
| 138 | + |
| 139 | + await example_basic_watch() |
| 140 | + await example_update_watch_interval() |
| 141 | + await example_cancel_watch() |
| 142 | + await example_handle_conflict() |
| 143 | + |
| 144 | + print("\n" + "=" * 60) |
| 145 | + print("All examples completed!") |
| 146 | + print("=" * 60) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + asyncio.run(main()) |
| 151 | + |
0 commit comments