Home
Help
Search
Login
Register
OPNsense Forum
»
English Forums
»
Development and Code Review
(Moderator:
fabian
) »
Registering callbacks on controller actions?
« previous
next »
Print
Pages: [
1
]
Author
Topic: Registering callbacks on controller actions? (Read 5037 times)
ndejong
Newbie
Posts: 18
Karma: 0
Registering callbacks on controller actions?
«
on:
August 18, 2018, 11:35:52 am »
Recently I found myself writing a whole monitoring daemon [#1] to watch for system changes which then called some function - in the back of my mind what I really wanted was some way to register myself for a callback action when other system actions occurred.
I also noticed recently that a plugin and package for Redis exists which might open up a reasonable mechanism to implement a system wide callback mechanism using Redis pubsub [#2]
My high level estimate is for something to be added to `ControllerBase.php` that extends the Phalcon `afterDispatch()` or `afterExecuteRoute()` functions that publish details of controller actions to Redis - this then would allow other components to subscribe to events published and then cause actions etc.
That said, I'm not sure this would entirely address my own use case because the event I'm looking for is anything that causes the configuration to change which can happen via legacy code that does not come down the MVC route - maybe there is another place to catch all `save()` calls somewhere which writes appropriate data to the Redis pubsub.
Also, it is not clear to me how much pressure Redis might put on the limited resources of lower end hardware and if this then has flow on effects on other functionality.
Links:-
[#1] -
https://github.com/verbnetworks/opnsense-plugins/blob/master/net-mgmt/configsync/src/opnsense/scripts/VerbNetworks/ConfigSync/MonitorDaemon.py
[#2] -
https://redis.io/topics/pubsub
Logged
fabian
Moderator
Hero Member
Posts: 2769
Karma: 200
OPNsense Contributor (Language, VPN, Proxy, etc.)
Re: Registering callbacks on controller actions?
«
Reply #1 on:
August 18, 2018, 12:07:56 pm »
Hello ndejong,
I wrote the redis plugin a long time ago (a year maybe) because I needed redis for rspamd - it is a soft dependency to cache data etc.
Since it is a plugin, it has to be installed manually so it is hard to include some stuff depending on it in core.
Logged
ndejong
Newbie
Posts: 18
Karma: 0
Re: Registering callbacks on controller actions?
«
Reply #2 on:
August 19, 2018, 02:57:26 am »
Hi Fabian
That makes sense
What I am suggesting would require new functionality at the core which in turn would create a major new dependency (aka Redis) on the OPNsense system - all of which sounds very resource heavy and is way beyond my pay-grade
What did not occur to me earlier is that Phalcon actually has a callback mechanism that allows you to register for callbacks on events:-
-
https://docs.phalconphp.com/en/3.3/api/Phalcon_Events_Manager
With this, it seems like it might be possible to achieve what I was originally thinking of here without the heavyweight Redis approach.
A quick grep of both the opnsense-core and opnsense-plugins repos shows only one instance of the Phalcon `attach()` function being used `opnsense/mvc/app/config/services_api.php` so perhaps use of callbacks in OPNsense is not (yet) a common thing??
Logged
ndejong
Newbie
Posts: 18
Karma: 0
Re: Registering callbacks on controller actions?
«
Reply #3 on:
August 22, 2018, 02:41:43 am »
Perhaps the question to my last post should be:-
Does anyone know if all functionality that writes to the config.xml file, especially legacy code functionality, all pass through the same save() function that is capable of attaching a Phalcon style callback to it?
Logged
fabian
Moderator
Hero Member
Posts: 2769
Karma: 200
OPNsense Contributor (Language, VPN, Proxy, etc.)
Re: Registering callbacks on controller actions?
«
Reply #4 on:
August 22, 2018, 06:28:37 pm »
why not modify the write config call itself for a notification?
Logged
ndejong
Newbie
Posts: 18
Karma: 0
Re: Registering callbacks on controller actions?
«
Reply #5 on:
August 22, 2018, 07:52:01 pm »
... because that means changing core code.
I'm not interested in calls made to `save()` by my plugin, I'm interested in creating an event callback to my plugin for all events (including legacy code) that happen to make a `save()`
Logged
ndejong
Newbie
Posts: 18
Karma: 0
Re: Registering callbacks on controller actions?
«
Reply #6 on:
August 27, 2018, 02:00:29 am »
After taking a look at what is required to register a global callback I don't believe it is possible to do from the context of a Plugin - which logically makes good sense.
That said, my own experience is with other frameworks and I'm not expert with Phalcon so there may be an approach buried away that is not clear, perhaps as a Dependency Injection but even then it would not seem possible without adding code to the core.
If anyone knows of an approach to attach a callback to the core from a plugin please do post herewith - while it does not seem like it should be possible it would be good to know if it is.
Logged
Print
Pages: [
1
]
« previous
next »
OPNsense Forum
»
English Forums
»
Development and Code Review
(Moderator:
fabian
) »
Registering callbacks on controller actions?