Comments (3)
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.
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.
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)
- Error when running example with ruby 2.0.0-p648 HOT 2
- Tests? HOT 1
- Readme can't work HOT 1
- "Nested" workers cause parent to quit when join returns HOT 9
- Need property or method that recive count tasks in @input_queue HOT 1
- Workers::TaskGroup::run is not signal safe
- Race Condition with Nested Timers Causing Deadlock HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from workers.