ensure_all and ensure_qs

26 views
Skip to first unread message

Amiramix

unread,
Oct 10, 2012, 11:41:11 AM10/10/12
to zotonic-d...@googlegroups.com
What's the difference between z_context:ensure_all and z_context:ensure_qs and why Zotonic code calls z_context:continue_session when using the later but not when using the former?
Thanks!

Marc Worrell

unread,
Oct 10, 2012, 1:35:09 PM10/10/12
to zotonic-d...@googlegroups.com
ensure_qs - make sure that all query string args (post) is parsed and available in the context.

ensure_all - same as ensure_qs, but also makes sure that there is a session and page process.

continue_session - makes sure that when there is a session that it is filled in in the context, and when not then no new one is started.


- Marc

Amiramix

unread,
Oct 10, 2012, 1:44:59 PM10/10/12
to zotonic-d...@googlegroups.com
Does it mean that we can process the request without extending the session? E.g. call ensure_qs with or without continue_session? What happens if a page requests some resources and, whereas the page itself extends the session, the requested resources do not? E.g. say that a page loads a JSON and the resource to return the JSON doesn't call continue_session?

Marc Worrell

unread,
Oct 10, 2012, 2:02:47 PM10/10/12
to zotonic-d...@googlegroups.com
Yes, that is the whole idea.
The session should be optional, especially when serving anonymous or public content.

On 10 okt. 2012, at 19:44, Amiramix wrote:

> Does it mean that we can process the request without extending the session? E.g. call ensure_qs with or without continue_session? What happens if a page requests some resources and, whereas the page itself extends the session, the requested resources do not? E.g. say that a page loads a JSON and the resource to return the JSON doesn't call continue_session?

That might give different access permissions. When there is no session, then no user will come from that session, so you end up with an anonymous #context.

- Marc

Amiramix

unread,
Oct 11, 2012, 7:54:57 AM10/11/12
to zotonic-d...@googlegroups.com
Mark, you mentioned there is one process per page. Is there any boundary which methods Zotonic calls within that process as opposed to methods called within the context of other processes like gen_servers (e.g. z_session)? For example, do the methods called on a resource by webzmachine (is_authorized, allowed_methods, content_types_provided, etc.) get executed within the context of the same process? If I store something in a process dictionary in one of them will that be available in the other method?

Marc Worrell

unread,
Oct 11, 2012, 9:53:23 AM10/11/12
to zotonic-d...@googlegroups.com
Hi,

There is a process per "open page/tab" in a browser. 
They are connected to the session and timeout when there hasn't been any activity.

We process the request in the Mochiweb/Webzmachine process.
Not in the session or page processes.

Don't use process dictionary variables.
We exclusively use the process dictionary for caching.
The caching routines periodically flush this cache to prevent it from growing too large.

When you want to store values the store them in:

  - the context for request duration/scope
  - the page process for duration of the page/websocket connections
  - the session for session duration/scope



- Marc

Amiramix

unread,
Oct 11, 2012, 1:38:04 PM10/11/12
to zotonic-d...@googlegroups.com
Many thanks for your explanation. I understand all but the page process. I mean I understand the principle but how do I get access to the page/websocket connection process? If it isn't the webzmachine process then which methods are called in the context of the page process?

Marc Worrell

unread,
Oct 11, 2012, 3:07:01 PM10/11/12
to zotonic-d...@googlegroups.com
On 11 okt. 2012, at 19:38, Amiramix wrote:

> Many thanks for your explanation. I understand all but the page process. I mean I understand the principle but how do I get access to the page/websocket connection process? If it isn't the webzmachine process then which methods are called in the context of the page process?

You can push content (scripts) using z_session_page:add_script/2 and /1.
You can also push it to all the open tabs/pages of a session using z_session:add_script/2 and /1

Normally you render actions or scripts in a fresh context and then send the contents of that context to the session or page.

- Marc
Reply all
Reply to author
Forward
0 new messages