Bootstraping details

The online documentation describes very precisely the mechanism of bootstraping a custom App. But it’s not clear about the instantiation of the Application itself.

If I get it right every time a NextCloud page is requested even if it’s not a page of the Application, the Application class of any Apps using iBootstrap is loaded into memory.

So let’s say user UserA open home page of application AppX in his browser. If AppZ implements iBootstrap, the AppZ Application class will be loaded in memory and all bootstraping methods will be called. Then once the page is fully rendered and returned to UserA. The corresponding instance of AppZ Application class received shutdown calls and is deleted.

Is that how it happens?

So to maintain informations that application would need between to request, Application class shall store data into Context object. Then the Application class can expect the Context object receive next time it is instantiated will contain the same data Applicaton class has stored during previous call.

Which means in case the server would be shutdown, all the context are lost? Or is there any persistence of Context that would allow data to still existes after a NextCloud server reboot?

Also what happens if multiple users, or even same user, run multiple requests at same time, there will be many instances of Application class existing together at same time? Or NextCloud only use one instance an reuse it for all requests?

Assuming each request creates a dedicated instance of Application class. If there is multiple instances of Application class existing at same time, they probably receive the same Context, right?
Is there mechanism to ensure any update of Context by one instance of Application will be reflected to all other existing instances of Application class at that time?

Hi @denisosteo

Each request has a completely autonomous environment and shares no state with another request. All Application objects are terminated at the end of a request as far as I know. I’m not sure which Context object you are talking about, but to share state between requests you can use

  • the PHP session
  • the memory cache
  • the database
  • the file system

That’s it, i think.

1 Like

For Context, I talk about IRegistrationContext and IBootContext respectively passed to the Application class in register and boot function call.

Are they completely new Instance each team the Application class is instantiated (each request) or they are persisted and further instantion of Apllication class receive the same instance?

They are also recreated each request

Hello.

The IBootContext is part of the instantiatin process. Let us track this down a bit:

First, all classes of all Apps are loaded (biut no obect are instantiated).

In the next step, the objects are instantiated. Do do so, the corresponding Application class would be needed. They hav parameterless constructors and must not rely on other apps. So they can be instantiated directly.

For the activated apps, the important classes are to be instantiated. Here comes the bootstrap context into play. The NC dependency injector checks which objects are needed for the classes to be constructed. First NC creates an empty bootstrap context. NC then tries to instantiate one class after the other. If a dependency is missing,this one is recursively built before. After all dependencies are build, the object is instantiated and directly registered in the bootstrap context. That way, if a certain class is needed multiple times, the instance from the bootstrap context is reused. (This happensof course also for all dependencies and their dependencies, putting all instances in the context.)

After the bootstrap process,all needed classes are instantiated and the actual request can be carried out.

After the answer has been sent to the client by the NC core, the request between client and browser terminates (see HTTP protocol) and thereby any PHP interpreter terminates the current execution as well. The bootstrap context (as well as any other non-persitently stored data) is removed from access/RAM).

There might be caches in the PHP core to speed up some things (OPcache) but these are only to speed up the loading of the PHP files and interpretation of the classes. No data is persisted (intentionally to have a pristane request environment each time). This is also true, as in the bootstrap context, objects of classes are stored which cannot be shared between different PHP processes.

Christian

1 Like

Thanks a lot for clarification. That means if we want to save context for user we need to persists data in file or database.
Like assuming I build a list of contact for a marketing activity. A user have open several contact he is working on. Then need to close this browser and continue his work later on another computer. In fact even same computer but after closing an re-opening nrowser or even the browser tab where NextCloud was displaying. And we want the same list of contacts to be still open for him.

Hello,

yes, this is correct. This is app-specific information that needs storage. Depending on the use case, you could also put it in cache but this might be lost. So, if you really must ensure that state to be preserved, I’d say put it in the database.
For the filesystem, you have the option (just as an addendum) to use the users file system (he can then see the data) or the app file system (a bit restricted but sufficient for you if you want to avoid the DB for some reason).

Sorry for the late reply, I was on a trip last week just came home.

Chris

1 Like

Hi Christian,

Thanks a lot for confirmation. Database sounds definitely the right place to store such data.

Hope you had a nice journey.

1 Like