Git Product home page Git Product logo

Comments (3)

chadrem avatar chadrem commented on July 30, 2024

Hi @thesmart,

Thanks for the feedback. Those hooks look like a good idea as they would simplify the syntax.

You can still accomplish what you want using the current code. A simple way using class inheritance should work well. You end up overriding the shutdown_handler and perform_handler methods in a custom worker class. The perform__handler is called every time you ask the worker to do some work. The shutdown_handler is called only once when you want to get rid of the worker. I hope this helps!

# Create a custom worker class.
# This class represents a single event driven thread.
class MyWorker < Workers::Worker
  private

  def shutdown_handler(event)
    disconnect_db
    super
  end

  def perform_handler(event)
    connect_db
    super
  end

  def connect_db
    # Open the DB connection if it doesn't exist.
    puts "connect_db() called."
  end

  def disconnect_db
    # Close the DB connection if it exists.
    puts "disconnect_db() called."
  end
end

# Create a pool with 10 custom workers.
exception_handler = proc { |e|
  puts "Don't forget to handle your exceptions..."
  puts "Exception: #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
}
pool = Workers::Pool.new(:worker_class => MyWorker, :on_exception => exception_handler)
pool.perform do
  puts "Hello World!"
end
pool.shutdown

from workers.

thesmart avatar thesmart commented on July 30, 2024

This is very helpful thanks. Ended up with something like this for ActiveRecord:

  # A worker that manages ActiveRecord pool connections
  class MyWorker < ::Workers::Worker
    private

    def shutdown_handler(event)
      ::ActiveRecord::Base.connection_pool.checkin(@connection) if @connection
      @connection = nil
      super
    end

    def perform_handler(event)
      # Force timezone to be UTC time. Use: Time.zone.new instead of Time.new
      # This was necessary because timezone is stored in a Thread variable
      Time.zone = 'UTC'
      Figma::Services::ActiveRecord.service
      @connection = ::ActiveRecord::Base.connection_pool.checkout
      begin
        super
      ensure
        ::ActiveRecord::Base.connection_pool.checkin(@connection) if @connection
        @connection = nil
      end
    end

  end

from workers.

chadrem avatar chadrem commented on July 30, 2024

Cool nice work. That looks like it should work. If you make an updates/changes please update this issue. I imagine it will be helpful to others in the future. Thank you !

from workers.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.