You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Matteo Caprari <ma...@gmail.com> on 2009/11/12 16:46:55 UTC

best way to send a STOMP message on attachment upload?

Hi.

i need to notify an erlang process each time a couchdb database is updated.
Reading the docs I learned how to use [update_notification] to fire a
script that sends that message.

It's fine, but  my feeling is that sending a message from couchdb
would be faster than starting a new 'heavy' process each time.

I'd like to hear some comments. Thanks!
--
:Matteo Caprari
matteo.caprari@gmail.com

Re: best way to send a STOMP message on attachment upload?

Posted by Matteo Caprari <ma...@gmail.com>.
Hi Matt.

Thanks for your answer.

I'll have an erlang http client "subscribe" to the continuous changes
feed and react as needed.
The sequence number is a nice touch, it will be helpful to recover from crashes.

-teo

On Thu, Nov 12, 2009 at 4:04 PM, Matt Goodall <ma...@gmail.com> wrote:
> 2009/11/12 Matteo Caprari <ma...@gmail.com>:
>> Hi.
>>
>> i need to notify an erlang process each time a couchdb database is updated.
>> Reading the docs I learned how to use [update_notification] to fire a
>> script that sends that message.
>>
>> It's fine, but  my feeling is that sending a message from couchdb
>> would be faster than starting a new 'heavy' process each time.
>
> Actually, update_notification processes are not started each time.
> They stay running and communicating with the CouchDB unless they are
> either killed, exit or die ... in which case CouchDB starts it up
> again.
>
> However, I would recommend looking at the _changes API. It lets you
> decouple these sorts of processes from the CouchDB server, making it
> much easier to manage, distribute and upgrade. See
> http://wiki.apache.org/couchdb/HTTP_database_API#Changes for details,
> specifically the 'longpoll' or 'continuous' feed.
>
> - Matt
>
>>
>> I'd like to hear some comments. Thanks!
>> --
>> :Matteo Caprari
>> matteo.caprari@gmail.com
>>
>



-- 
:Matteo Caprari
matteo.caprari@gmail.com

Re: best way to send a STOMP message on attachment upload?

Posted by Matt Goodall <ma...@gmail.com>.
2009/11/12 Matteo Caprari <ma...@gmail.com>:
> Hi.
>
> i need to notify an erlang process each time a couchdb database is updated.
> Reading the docs I learned how to use [update_notification] to fire a
> script that sends that message.
>
> It's fine, but  my feeling is that sending a message from couchdb
> would be faster than starting a new 'heavy' process each time.

Actually, update_notification processes are not started each time.
They stay running and communicating with the CouchDB unless they are
either killed, exit or die ... in which case CouchDB starts it up
again.

However, I would recommend looking at the _changes API. It lets you
decouple these sorts of processes from the CouchDB server, making it
much easier to manage, distribute and upgrade. See
http://wiki.apache.org/couchdb/HTTP_database_API#Changes for details,
specifically the 'longpoll' or 'continuous' feed.

- Matt

>
> I'd like to hear some comments. Thanks!
> --
> :Matteo Caprari
> matteo.caprari@gmail.com
>

Re: best way to send a STOMP message on attachment upload?

Posted by Adam Kocoloski <ko...@apache.org>.
On Nov 12, 2009, at 10:46 AM, Matteo Caprari wrote:

> Hi.
> 
> i need to notify an erlang process each time a couchdb database is updated.
> Reading the docs I learned how to use [update_notification] to fire a
> script that sends that message.
> 
> It's fine, but  my feeling is that sending a message from couchdb
> would be faster than starting a new 'heavy' process each time.
> 
> I'd like to hear some comments. Thanks!
> --
> :Matteo Caprari
> matteo.caprari@gmail.com

Hi Matteo, if you're willing to hack the source a bit this is easy to do.  CouchDB has a gen_event system which generates the following set of events

{created, DbName}
{updated, DbName}
{deleted, DbName}

So, the easiest way to plug into this is to use that same update_notification system you've been using, but instead of configuring an external script in the .ini system just pass an Erlang function to it directly, e.g.:

couch_db_update_notifier:start_link(fun({created, DbName}) ->
        my_created_action();
    ({updated, DbName}) ->
        my_updated_action();
    ({deleted, DbName}) ->
        my_deleted_action()
    end).

You just need to figure out how to execute that bit of code during CouchDB's startup, e.g. by calling it in couch_server_sup:start_server.  Best, Adam

P.S. Couch doesn't actually spawn the external script each time, right?  It should keep that script running permanently and just send the new messages over stdio.