ansible / event-driven-ansible

Ansible Collection for EDA
https://ansible.readthedocs.io/projects/ansible-eda/
Apache License 2.0
280 stars 109 forks source link

file_watch exits with ERROR - Cannot instantiate typing.Optional #164

Closed jpmens closed 3 months ago

jpmens commented 1 year ago

After an ansible-galaxy collection install ansible.eda I attempt to use the file_watch source plugin:

- name: Rulebook to do something
  hosts: localhost
  sources:
      - ansible.eda.file_watch:
          path: "files/"
          recursive: false
          ignore_regexes: [ '.*\.o' ]
  rules:
      - name: trigger on range
        condition: event.type == 'FileCreatedEvent'
        action:
           run_module:
               name: debug
               module_args:
                   msg: "I just saw {{ event.src_path }}"

Upon startup, the rulebook fails:

$ ansible-rulebook -i inventory -r r1.yml
2023-08-13 11:26:47,534 - ansible_rulebook.engine - ERROR - Source error Cannot instantiate typing.Optional
2023-08-13 11:26:47,534 - ansible_rulebook.engine - ERROR - Shutting down source: ansible.eda.file_watch error : Cannot instantiate typing.Optional
2023-08-13 11:26:47,551 - ansible_rulebook.app - ERROR - Cannot instantiate typing.Optional
2023-08-13 11:26:47,552 - ansible_rulebook.cli - ERROR - Terminating One of the source plugins failed

With the help of this issue I somewhat fix it with this patch:

--- /tmp/old    2023-08-13 11:29:16.000000000 +0200
+++ /Users/jpm/.ansible/collections/ansible_collections/ansible/eda/extensions/eda/plugins/event_source/file_watch.py   2023-08-13 11:29:08.000000000 +0200
@@ -35,7 +35,7 @@
     root_path = args["path"]

     class Handler(RegexMatchingEventHandler):
-        def __init__(self: "Handler", **kwargs: Optional(list[str])) -> None:
+        def __init__(self: "Handler", **kwargs: Optional[list[str]]) -> None:
             RegexMatchingEventHandler.__init__(self, **kwargs)

         def on_created(self: "Handler", event: dict) -> None:

By "somewhat" fix it, I mean that the watch part seems to work, but I see the following when the rule fires:

$ ansible-rulebook -i inventory -r r1.ym
2023-08-13 11:31:16,348 - asyncio - ERROR - Exception in callback FilteredQueue.put_nowait({'change': 'modified', 'meta': {'received_at': '2023-08-13T09:31:16.348116Z', 'source': {'name': 'ansible.eda.file_watch', 'type': 'ansible.eda.file_watch'}, 'uuid': 'cba22dd7-98a...-112734581631'}, 'root_path': 'files/', 'src_path': '/Users/jpm/tst/files', ...})
handle: <Handle FilteredQueue.put_nowait({'change': 'modified', 'meta': {'received_at': '2023-08-13T09:31:16.348116Z', 'source': {'name': 'ansible.eda.file_watch', 'type': 'ansible.eda.file_watch'}, 'uuid': 'cba22dd7-98a...-112734581631'}, 'root_path': 'files/', 'src_path': '/Users/jpm/tst/files', ...})>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/Users/jpm/take/training/rulebook/env.v3/lib/python3.11/site-packages/ansible_rulebook/engine.py", line 93, in put_nowait
    self.queue.put_nowait(data)
  File "/usr/local/Cellar/python@3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/queues.py", line 143, in put_nowait
    raise QueueFull
asyncio.queues.QueueFull
localhost | SUCCESS => {
    "msg": "I just saw /Users/jpm/tst/files/bla4"
}
ssbarnea commented 3 months ago

Closing as this fix is no longer needed with current codebase.