Gost

The Go simple worker

Photo from emiliano-iko

Originalmente publicado el 21 de Febrero de 2014

There are a lot of fantastic projects out there. One of them being goworker which is a Resque compatible project.

But sometimes you want something simpler. And as usual is easy to get that kind of simplicity of the right Ruby projects.

This time the inspiration comes from Ost. This is a lightweight lib to enqueue jobs to be executed in workers.

To push a job you simply do:

Ost[:videos_to_process].push(@video.id)

And to do something with it:

require "ost"

Ost[:videos_to_process].each do |id|
  # Do something with it!
end

Inspired bit its simplicity I wrote Gost a Go package that allows you to start Ost compatible queues or use it as it own thing.

Some examples:

import "github.com/elcuervo/gost"

// ...

gost := gost.Connect(":6379")
gost.Push("my_jobs", "id_to_be_procesed")

And that’s it, the id_to_be_procesed id is pushed to the my_jobs queue ready for the next worker to fetch it.

gost.Each("my_jobs", func(id string) bool {
  if(doesSomethingWithTheId(id)) {
    // Everything is ok
    return true
  } else {
    // If the fn returns false the
    // items is kept in the backup key
    return false
  }
})

You can do more things like knowing how many items are to be executed:

gost.Items("my_jobs")

Or stoping the worker. Useful if you want to make your worker gracefully exit given a system signal.

gost.Stop()