How to add shared state to Loco? #469
Replies: 5 comments 2 replies
-
I suppose it is best to build an "initializer" which essentially is a shared state that you can then call up in your request. It is quite simple to build one actually... Unfortunately the docs does not have step-by-step instructions. However look into the view-initializer implementation. |
Beta Was this translation helpful? Give feedback.
-
Hey @georgeboot, As @schungx mentioned, the method to share state involves utilizing Could you provide more details on what you're aiming to achieve? |
Beta Was this translation helpful? Give feedback.
-
I think what @georgeboot is asking here relates to #522 where the internal If you wanted to implement a background task that used the shared libvips context you wouldn't be able to. Instead you would have to create a new libvips context in the |
Beta Was this translation helpful? Give feedback.
-
This is a great question (and I believe should be worth a section in the docs). Shared state in controllers: Shared state in workers: Note that by-design sharing state between controllers and workers have no meaning, because even though you may choose to run workers in the same process as controllers initially (and share state) -- you'd want to quickly switch to proper workers backed by queue and running in a standalone workers process as you scale horizontally, and so workers should by-design have no shared state with controllers, for your own good. Tasks |
Beta Was this translation helpful? Give feedback.
-
I think it would be great to expand on the philosophy behind this in the docs just to add some clarity! IMO where this difference gets a bit confusing is in two main places.
Granted, even in Rails, the state isn't truly shared between workers running on a dedicated machine and the main application server itself but it gives the illusion of this via the whole app being booted to run the worker jobs. So anything that is available via things like initializers in the main application server is also available in workers. |
Beta Was this translation helpful? Give feedback.
-
I recently started playing with Loco and an very impressed. We use Laravel (PHP) but needs better performance for our data operations (converting images, transforming coordinate systems, processing large pointclouds etc).
While experimenting with libvips for image manipulation, I noticed that the crate depends on a "context":
What is the best way in Loco for this variable to be available everywhere?
My initial though was to use the
after_routes
hook like so:but I believe it's then only available in routes, and not in, say, taks or workers.
Beta Was this translation helpful? Give feedback.
All reactions