What is the correct way to do a postback within a resource?

11 views
Skip to first unread message

Alain O'Dea

unread,
Apr 10, 2012, 12:07:40 AM4/10/12
to zotonic-d...@googlegroups.com
I would like to call mod_zotonic_status_vcs:event/2 from a Webmachine resource.

I'm essentially trying to trigger a {vcs_up, [{site, SiteName}]} postback when Github sends me a POST as part of their Post-Receive Hooks.

My initial thought is to construct a #postback{} record and call the event/2 directly, but that seems like an underhanded hack especially since I can't think of valid values for trigger and target :)

What is the correct way to do a postback within a resource?

Thanks,
Alain

Arjan Scherpenisse

unread,
Apr 10, 2012, 2:38:44 AM4/10/12
to zotonic-d...@googlegroups.com

On 04/10/2012 06:07 AM, Alain O'Dea wrote:
> I would like to call mod_zotonic_status_vcs:event/2 from a Webmachine
> resource.
>
> I'm essentially trying to trigger a {vcs_up, [{site, SiteName}]}
> postback when Github sends me a POST as part of their Post-Receive Hooks.
>
> My initial thought is to construct a #postback{} record and call the
> event/2 directly, but that seems like an underhanded hack especially
> since I can't think of valid values for trigger and target :)

Since #postback{} is a record, you dont need to fill in values for
trigger and target. That's why the postback has been converted to a
record in the first place :)

I guess the correct way to do this is to use z_notifier...

z_notifier:notify(#postback{message={vcs_up, [{site, SiteName}]}}, Context)

would do the trick.

It's not a real "postback" of course, since there is no browser<->server
interaction, but I guess in your case that's not the point anyway.

Arjan

Alain O'Dea

unread,
Apr 10, 2012, 9:42:39 PM4/10/12
to zotonic-d...@googlegroups.com
I'll give that a try.  Thank you Arjan :)

I got no indication that that worker, but when I tried a direct call I get a crash.  The code in mod_zotonic_status_vcs fairly assumes a particular context of browser<->server and does things with templates. I will have to separate the update logic from the presentation logic in mod_zotonic_status_vcs eventually.  For now I'll code shop it to get it working.

How does z_notifier:notify/2 know to call mod_zotonic_status_vcs:event/2?

There are no calls to z_notifier:observe/3 or z_notifier:observe/4 in mod_zotonic_status_vcs,  I see that (z_module_manager.erl, line 716) searches through the exported functions and collects all functions beginning with observe_ and pid_observe_ and adds them as observers.  That only explains possible calls to mod_zotonic_status_vcs:observe_zotonic_status_init/3 when notifications occur with a zotonic_static_init event.

Would I be correct to assume that z_notifier:notify/2 will not work in my case based on this?

Thanks,
Alain

Marc Worrell

unread,
Apr 11, 2012, 3:10:21 AM4/11/12
to zotonic-d...@googlegroups.com
On 11 apr 2012, at 03:42, Alain O'Dea wrote:

How does z_notifier:notify/2 know to call mod_zotonic_status_vcs:event/2?

Actually, it doesn't.
It will call observe_postback/2 (if present when the module was loaded).

There are no calls to z_notifier:observe/3 or z_notifier:observe/4 in mod_zotonic_status_vcs,  I see that (z_module_manager.erl, line 716) searches through the exported functions and collects all functions beginning with observe_ and pid_observe_ and adds them as observers.  That only explains possible calls to mod_zotonic_status_vcs:observe_zotonic_status_init/3 when notifications occur with a zotonic_static_init event.

Would I be correct to assume that z_notifier:notify/2 will not work in my case based on this?

I think we need to reorder mod_zotoonic_status_vcs a bit so that the API is visible from other modules.

Best,

Marc

Arjan Scherpenisse

unread,
Apr 11, 2012, 3:22:33 AM4/11/12
to zotonic-d...@googlegroups.com
Yes indeed, I was mistaken to say you could use z_notifier, that's not
the way to go. Some refactoring is indeed needed to be able to better
expose this from other modules.

As always, pull requests welcome :-D

Arjan

On 04/11/2012 09:10 AM, Marc Worrell wrote:
>
> On 11 apr 2012, at 03:42, Alain O'Dea wrote:
>

>> How does *z_notifier:notify/2* know to call
>> *mod_zotonic_status_vcs:event/2*?


>
> Actually, it doesn't.
> It will call observe_postback/2 (if present when the module was loaded).
>

>> There are no calls to *z_notifier:observe/3* or *z_notifier:observe/4*
>> in *mod_zotonic_status_vcs*, I see that (z_module_manager.erl, line
>> 716)
>> <https://github.com/zotonic/zotonic/blob/master/src/support/z_module_manager.erl#L716>


>> searches through the exported functions and collects all functions
>> beginning with observe_ and pid_observe_ and adds them as observers.
>> That only explains possible calls to

>> *mod_zotonic_status_vcs:observe_zotonic_status_init/3* when
>> notifications occur with a *zotonic_static_init event*.
>>
>> Would I be correct to assume that *z_notifier:notify/2* will not work

Reply all
Reply to author
Forward
0 new messages