run code after eventmachine starts?

32 views
Skip to first unread message

Joseph McDonald

unread,
Dec 30, 2011, 9:08:17 PM12/30/11
to sinatrarb

I want to run this block of code when my classic sinatra app starts:

# using em-dir-watcher
EMDirWatcher.watch(".", :grace_period => 0.2) do |paths|
// files changed...
end

But I get an error that eventmachine is not initialized. after the
server starts (thin) eventmachine is initialized and it works. but
the way I get it started is too hacky:

get '/startdw' do
EMDirWatcher.watch(".", :grace_period => 0.2) do |paths|
// files changed
end
return "started dirwatcher"
end

I hit that url to start the dirwatcher. Is there a way to run a block
of code right after the webserver starts (or eventmachine is
initialized)?

I am also using sinatra-synchrony, but i get the same error with or
without it.


ericgj

unread,
Jan 1, 2012, 10:07:52 AM1/1/12
to sinatrarb
You might need to switch to modular style, where you can pass a block
into `run!`, which I haven't tried but think would work for this.
Classic style apps don't give you this hook AFAIK.
i.e.

YourApp.run! do |server|
if EM.reactor_running?
EMDirWatcher.watch(".", :grace_period => 0.2) do |paths|
    // files changed...
  end
end
end

Reply all
Reply to author
Forward
0 new messages