# Heroku, needs constant AppController updates?

**URL:** <https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195>\
**Category:** Rails\
**Created:** [January 3, 2011, 11:37am UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195 "2011-01-03T11:37:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 11:37am UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/1 "2011-01-03T11:37:34Z")

</div>

Dear All,

Fairly new to rails and Heroku, so could be doing something wacky - do  
let me know if you think my code practice is off, even if unrelated to  
this error, I’d like to learn!

I’m using Rails 3.0.0, ruby 1.8.7, and ‘sqlite3-ruby’, ‘1.2.5’, :require  
=\> ‘sqlite3’.

I’ve got an application that goes off and scrapes websites for event  
information, to do this I’m using:

nokogiri - for scraping  
date - to help with parsing  
chronic - to help recognise dates  
open-uri - to open the url

all of these are just gem ‘gem-name’, so latest unspecified versions.

The code that does this scraping is in the ApplicationController class -  
a bad place for it?  
I’ve just got it working as a background process using delayed\_job, I  
have a class called EventSearcher with the required ‘perform’ method.

My error occurs when I upload to Heroku, if I make changes to the app,  
but NOT to ApplicationController,  
do $git add . / git commit … / git push heroku master / heroku  
workers 1 / heroku open  
then the process runs, appears successful in the logs, but returns no  
events

The error can be solved I’ve discovered by making small changes to  
Application controller, such as adding the line  
logger.debug"Changing AppController again"  
then doing $git add . / git commit … / git push heroku master /  
heroku workers 1 / heroku open  
this time the process runs, appears in logs to take longer, and returns  
the scraped info as required.

Any ideas why this should be??

I have tried solving this myself by putting in 'logger.debug" commands,  
but these don’t show on my heroku logs, any idea how to get them to show  
up?

I’ve put the code below, for ApplicationController, EventSearcher, and  
part of SuggestedEvents it’s a bit of a mess I know, but runs fine on  
local machine.

-------------- Application Controller

class ApplicationController \< ActionController::Base

require ‘date’  
require ‘nokogiri’  
require ‘open-uri’  
require ‘chronic’

protect\_from\_forgery  
include SessionsHelper

```
def authenticate
  deny_access unless signed_in?
end

```

def find\_all\_events

@event\_searches = EventSearch.all

@event\_searches.each do |es|

do\_search(es)

end  
end

def find\_one\_band\_events(event\_id)

es = EventSearch.where(:id =\> event\_id)[0]  
do\_search(es)  
end

def do\_search(event\_search)

logger.debug"Changing AppController again"  
logger.debug"in do search"  
url = event\_search.urlOne

if validate(url)

logger.debug"Passed Validate URL"

doc = Nokogiri::HTML(open(url))

#search the doc with the saved css search strings  
if !(event\_search.eventDateCSS.empty?)  
startDate = doc.css(event\_search.eventDateCSS) else eventDate = 0  
end  
if !(event\_search.eventNameCSS.empty?)  
eventName = doc.css(event\_search.eventNameCSS) else eventName = 0  
end  
if !(event\_search.eventLocationCSS.empty?)  
location = doc.css(event\_search.eventLocationCSS) else location = 0  
end  
if !(event\_search.eventTimeCSS.empty?)  
time = doc.css(event\_search.eventTimeCSS) else time = 0 end  
if !(event\_search.priceCSS.empty?)  
price = doc.css(event\_search.priceCSS) else price = 0 end  
if !(event\_search.descriptionCSS.empty?)  
description = doc.css(event\_search.descriptionCSS) else description  
= 0 end  
i=0

if eventDate != 0  
while i \< startDate.length # find all events on that page going  
through each case of start date

```
SuggestedEvent.new do |@savedEvent|

  if eventDate != 0
    @savedEvent.eventDate = date_from_string(startDate[i].content)
    logger.debug(startDate[i].content)
    end

  test = SuggestedEvent.where(:bandName => event_search.bandName,
                :eventDate => @savedEvent.eventDate)

  if !test[0]

  @savedEvent.bandName = event_search.bandName
  if eventName != 0
    @savedEvent.eventName = remove_whitespace(eventName[i].content)
    end
  if time != 0
    @savedEvent.doorsOpen = Chronic.parse(time[i].content) end
  if price != 0
    @savedEvent.price = price[i].content end
  if description !=0
    @savedEvent.description =

```

remove\_whitespace(description[i].content) end

```
  if location != 0
    savedVenueName = paramMatcher('Venue','venueName',

```

remove\_whitespace(location[i].content))  
if savedVenueName  
@savedEvent.venue = savedVenueName[0]  
@savedEvent.city\_location = savedVenueName[1]  
@savedEvent.longitude = savedVenueName[2]  
@savedEvent.latitude = savedVenueName[3]  
else  
@newVenue = Venue.new  
@newVenue.venueName = remove\_whitespace(location[i].content)  
@newVenue.old\_venue\_name = @newVenue.venueName  
@newVenue.save  
@savedEvent.venue = remove\_whitespace(location[i].content)  
end #of if  
else  
@savedEvent.venue = ‘No saved venue found’  
end #of location !=  
@savedEvent.save  
end # of if !test  
end # of .new do  
i += 1  
end # of while

end #of if eventDate !=0

end # of if url passes

end # of def

# get the date from whatever string gets thrown

def date\_from\_string(date)

# should remove st rd nd and th

firstParse = Chronic.parse(date)  
r1 = /[a-zA-Z]/

#daY Less than 12, assume chronic wrong for Britain, as long as also no  
characters such as December,  
#where it would be right  
if firstParse.day \<= 12 and !r1.match(date)

#swap month with day  
firstParse = firstParse.change(:day =\> firstParse.month, :month =\>  
firstParse.day)

end #of if

return firstParse

end #of def

# remove whitespace and other characters such as newline

def remove\_whitespace(dirty\_name)

return dirty\_name.split(’ ').join(" ")

end

#find names in strings from the net  
def paramMatcher(modelString, param, cssScrapedString)

case modelString  
when ‘Venue’  
venues = Venue.all  
i=0  
venues.each do |v|

```
  if v.venueName.scan(cssScrapedString)[0] or

```

cssScrapedString.scan(v.venueName)[0]

```
  return [v.venueName, v.city_location, v.longitude, v.latitude]
  end

end # of .each

```

end # of case

return nil  
end # of def param Matcher

def validate(url)  
begin  
uri = URI.parse(url)  
if uri.class != URI::HTTP  
return false  
end  
rescue URI::InvalidURIError  
return false  
end  
return true  
end

end # of class

----------------- Event Searcher

class EventSearcher \< ApplicationController

attr\_accessor :all\_events  
attr\_accessor :one\_event\_id

def perform

logger.debug"in perform"

if all\_events == 1 # searches all band names

logger.debug"in perform, all\_events == 1"

find\_all\_events

end

if one\_event\_id != nil #searches just one band name using id

logger.debug"in perform, one\_event != nil"  
logger.debug(one\_event\_id)  
find\_one\_band\_events(one\_event\_id)

end

end # Of Perform

end #of class

-------------- Suggested Events

class SuggestedEventsController \< ApplicationController

#… other functions removed for brevity…

def search\_all\_events

logger.debug"Sugg E search all events"  
#the instance of the object that gets put on the queue  
@event\_searcher = EventSearcher.new  
@event\_searcher.one\_event\_id = nil  
@event\_searcher.all\_events = 1  
Delayed::Job.enqueue @event\_searcher

flash[:notice] = “Finding All events, this takes a while, refresh  
after 10 mins”  
redirect\_to suggested\_events\_path

end

end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 1:17pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/2 "2011-01-03T13:17:43Z")

</div>

On Jan 3, 10:37am, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> information, to do this I’m using:
> 
> nokogiri - for scraping  
> date - to help with parsing  
> chronic - to help recognise dates  
> open-uri - to open the url
> 
> all of these are just gem ‘gem-name’, so latest unspecified versions.
> 
> The code that does this scraping is in the ApplicationController class -  
> a bad place for it?

I don’t know if it is related to your problem, but it does seem a  
bizarre place to put that code - I’d stick it all in the EventSearcher  
class and have EventSearcher inherit from object directy)

Fred

Fred

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 2:04pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/3 "2011-01-03T14:04:06Z")

</div>

Thanks for the advice Fred,

I’ve moved the search logic / parsing across to the EventSearcher class  
now as you suggested, and it seems to be working. I had it in there to  
begin with, and moved it out because it couldn’t find other classes it  
needed. But I now think this was caused because I didn’t restart the  
local server before retrying.

When you say “and have EventSearcher inherit from object directy)” could  
you explain how to do this, currently I have EventSearcher inherit from  
Application controller with

class EventSearcher \< ApplicationController

is this what you mean?

thanks,

Mike

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 2:24pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/4 "2011-01-03T14:24:18Z")

</div>

On 3 Jan 2011, at 13:04, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Application controller with

I meant just  
class EventSearcher  
…  
end

(which is the same as \< Object)

Fred

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 3:05pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/5 "2011-01-03T15:05:26Z")

</div>

I tried making EventSearcher just inherit from object, ie:

class EventSearcher

but then I get errors from almost the first line, eg.

failed with NameError: undefined local variable or method `logger’

for my debug statements, and I don’t suppose it’ll know about my model  
Classes either. Having it inherit from ApplicationController solves  
these problems, but is there a better way of doing this?

cheers,

Mike

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 4:59pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/6 "2011-01-03T16:59:21Z")

</div>

Cool, learning plenty here, cheers!

I’ve got a logger method in the class now, and so taken away the  
unnecessary inheritance. Cheers!

My heroku app is working for now, although I’m not sure it’ll stay  
working, it seems to stop fetching events after a day, and needs a new  
git push, with changes to specifically the “def do\_search(event\_search)”  
method which is the one that does the work of getting the url, and  
parsing the html. A bit weird, as it works every time on local machine.  
I’ve asked them about this on the heroku mailing list as well.

Thanks for your help!

M

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 8:02pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/7 "2011-01-03T20:02:52Z")

</div>

On Jan 3, 3:59pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve asked them about this on the heroku mailing list as well.  
> If your daemon is idle for extended periods of time it might be your  
> mysql connection timing out. You could try tossing in a call to  
> ActiveRecord::Base.connection\_pool.verify\_active\_connections! to weed  
> out dead connections

Fred

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 8:26pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/8 "2011-01-03T20:26:13Z")

</div>

Michael B. wrote in post #972049:

> Cool, learning plenty here, cheers!
> 
> I’ve got a logger method in the class now, and so taken away the  
> unnecessary inheritance. Cheers!
> 
> My heroku app is working for now, although I’m not sure it’ll stay  
> working, it seems to stop fetching events after a day, and needs a new  
> git push, with changes to specifically the “def do\_search(event\_search)”  
> method which is the one that does the work of getting the url, and  
> parsing the html. A bit weird, as it works every time on local machine.  
> I’ve asked them about this on the heroku mailing list as well.

Why don’t you set up a cron job or use Resque or delayed\_job to run the  
job periodically?

> Thanks for your help!
> 
> M

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [January 3, 2011, 3:42pm UTC](https://www.ruby-forum.com/t/heroku-needs-constant-appcontroller-updates/226195/9 "2011-01-03T15:42:53Z")

</div>

On 3 Jan 2011, at 14:05, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> these problems, but is there a better way of doing this?  
> Finding your model classes should not be a problem. logger is an  
> instance method on controllers, so you don’t get it here. Rails.logger  
> will get you the same logger though (and you could define a logger  
> method yourself to save some typing.

Fred
