Hi,

Instead of starting them from an http handler, I would store in the
database the locations and their users. Then, from main() I would start
just one go routine that will check the db for any pending location that
needs to be fetched, if found, then go do work, sleep for x seconds, then
check the database again.

That way, when a user no longer wants to be notified about location Z, you
just remove their user from the database and then go routine will not find
it any more.

Thanks



On Sun, Feb 19, 2017 at 5:32 AM, <egortict...@gmail.com> wrote:

> Thanks for your answer!
>
> Am I right, when starting goroutine like this? (With infinite loop inside
> goroutine function)
>
> func (ctl *Controller) startWatchUserLocation(username, location string) {
>      for {
>            data, err := ctl.ScrapeRoomsWithLocation(username, location) *<-
> scraping info from html*
>             if err != nil {
>
>                        log.Println(err)
>              }
>
>              fmt.Sprint(data)
>              ctl.writeScrapeResultsToDB(data)  *<- writing info in
> database*
>              data = make([]byte, 0)
>              time.Sleep(30 * time.Second)
>       }
> }
>
> And then in http handler I call :
>
> func (ctl *Controller) ScraperHandler(w http.ResponseWriter, r
> *http.Request) {
>
>        username := r.FormValue("username")
>        location := r.FormValue("location")
>        *go* ctl.startWatchUserLocation(username, location)
> }
>
> And one more quetion: how can I close specific background
> process(goroutine with infinite loop) when client want to stop tracking
> some location?
> For example, clients sends another http requset, with some flag "Stop
> traking" and close its goroutine?
>
>
>
>
> Воскресенье, 19 февраля 2017, 1:02 +03:00 от Diego Medina <
> fmpwiz...@gmail.com>:
>
> Hi,
>
> See below:
>
>  My first idea was to make some background process, which will scrape
> rooms every 30 seconds, save the results *(in mongo, in cookies or
> somewhere else, advise me please)*, match new scrape results with
> previous and save differences (new rooms) in DB for future posting to user
> in some form (email or list on html page).
>
>
>
> if by scrape rooms  you mean having your program go to a website, fetch
> the html, parse it and extract data from them, going every 30 seconds may:
> 1. Break their term of use
> 2. Related to 1, they may mark your bot as spam and block it because you
> are going too often/generate too much load on their servers.
>
> With the legal stuff aside, it would be better to store this info on your
> database, each user has have their locations in the database. Storing them
> in cookies would mean that using their cell phone browser will give them
> diff results than their computers.
>
> Also store the new rooms data in your database, this allows you to resend
> them an email if they didn't get it, and/or render a page with all the
> listings when they go back to your site.
>
>
> 2) What is the best way to make that background process in go?
>
>
>
> If you are just starting with Go, go simple and simply start the
> background process as a go routing, with a time.tick of x minutes
>
>
>
> 3) This would be great if you'd point me on some examples relating to the
> case.
>
>
>
>
> Don't have any.
>
>
> I appreciate your concern.
>
>
>


-- 
Diego Medina
Lift/Scala Consultant
di...@fmpwizard.com
https://blog.fmpwizard.com/

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to