Fast and distributed workflow runner using ActiveJob and Redis
Fast and distributed workflow runner using ActiveJob and Redis
Gush is a parallel workflow runner using only Redis as storage and ActiveJob for scheduling and executing jobs.
Gush relies on directed acyclic graphs to store dependencies, see Parallelizing Operations With Dependencies by Stephen Toub to learn more about this method.
This README is about the latest master code, which might differ from what is released on RubyGems. See tags to browse previous READMEs.
gush to Gemfilegem 'gush', '~> 5.0'
GushfileWhen using Gush and its CLI commands you need a Gushfile in the root directory.
Gushfile should require all your workflows and jobs.
For RoR it is enough to require the full environment:
require_relative './config/environment.rb'
and make sure your jobs and workflows are correctly loaded by adding their directories to autoload_paths, inside config/application.rb:
config.autoload_paths += ["#{Rails.root}/app/jobs", "#{Rails.root}/app/workflows"]
Simply require any jobs and workflows manually in Gushfile:
require_relative 'lib/workflows/example_workflow.rb'
require_relative 'lib/jobs/some_job.rb'
require_relative 'lib/jobs/some_other_job.rb'
The DSL for defining jobs consists of a single run method.
Here is a complete example of a workflow you can create:
# app/workflows/sample_workflow.rb
class SampleWorkflow B[FetchJob1]
A --> C[FetchJob2]
B --> D[PersistJob1]
C --> E[PersistJob2]
D --> F[NormalizeJob]
E --> F
F --> G[IndexJob]
G --> H{Finish}
Let's start with the simplest workflow possible, consisting of a single job:
class SimpleWorkflow {url: "http://url.com/book.pdf"}
end
end
Now that we have defined our workflow and its jobs, we can use it:
Important: The command to start background workers depends on the backend you chose for ActiveJob. For example, in case of Sidekiq this would be:
bundle exec sidekiq -q gush
Click here to see backends section in official ActiveJob documentation about configuring backends
Hint: gush uses gush queue name by default. Keep that in mind, because some backends (like Sidekiq) will only run jobs from explicitly stated queues.
flow = PublishBookWorkflow.create("http://url.com/book.pdf", "978-0470081204")
flow.start!
Now Gush will start processing jobs in the background using ActiveJob and your chosen backend.
flow.reload
flow.status
#=> :running|:finished|:failed
reload is needed to see the latest status, since workflows are updated asynchronously.
flow = Workflow.find(id)
To get workflows with pagination, use start and stop (inclusive) index values:
flows = Workflow.page(0, 99)
Or in reverse order:
flows = Workflow.page(0, 99, order: :desc)
Workflows can accept a hash of globals that are automatically forwarded as parameters to all jobs.
This is useful to have common functionality across workflow and job classes, such as tracking the creator id for all instances:
class SimpleWorkflow {:creator_id=>123}
flow.jobs.first.params
=> {:creator_id=>123, :url=>"http://foo.com"}
Note: job params with the same key as globals will take precedence over the globals.
Gush offers a useful tool to pass results of a job to its dependencies, so they can act differently.
Example:
Let's assume you have two jobs, DownloadVideo, EncodeVideo.
The latter needs to know where the first one saved the file to be able to open it.
class DownloadVideo B[NotificationJob]
A --> C[NotificationJob]
A --> D[NotificationJob]
A --> E[NotificationJob]
A --> F[NotificationJob]
B --> G[AdminNotificationJob]
C --> G
D --> G
E --> G
F --> G
G --> H{Finish}
There might be a case you want to configure different jobs in the workflow using different queues. Based on the above the example, we want to config AdminNotificationJob to use queue admin and NotificationJob use queue user.
class NotifyWorkflow
of a page of workflows:
bundle exec gush list
of the most recent 100 workflows
bundle exec gush list -99 -1
This requires that you have imagemagick installed on your computer:
bundle exec gush viz
In order to prevent getting the RedisMutex::LockError error when having a large number of jobs, you can customize these 2 fields locking_duration and polling_interval as below
# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
config.concurrency = 5
config.locking_duration = 2 # how long you want to wait for the lock to be released, in seconds
config.polling_interval = 0.3 # how long the polling interval should be, in seconds
end
Running NotifyWorkflow.create inserts multiple keys into Redis every time it is run. This data might be useful for analysis but at a certain point it can be purged. By default gush and Redis will keep keys forever. To configure expiration you need to do two things.
config.ttl in seconds. Best NOT to set TTL to be too short (like minutes) but about a week in length.# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
config.concurrency = 5
config.ttl = 3600*24*7
end
Client#expire_workflows periodically, which will clear all expired stored workflow and job data and indexes. This method can be called at any rate, but ideally should be called at least once for every 1000 workflows created.If you need more control over individual workflow expiration, you can call flow.expire!(ttl) with a TTL different from the Gush configuration, or with -1 to never expire the workflow.
Since we do not know how long our workflow execution will take we might want to avoid starting the next scheduled workflow iteration while the current one with same class is still running. Long term this could be moved into core library, perhaps Workflow.find_by_class(klass)
# config/initializers/gush.rb
GUSH_CLIENT = Gush::Client.new
# call this method before NotifyWorkflow.create
def find_by_class klass
GUSH_CLIENT.all_workflows.each do |flow|
return true if flow.to_hash[:name] == klass && flow.running?
end
return false
end
Gush 3.0 adds indexing for fast workflow pagination and changes the mechanism for expiring workflow data from Redis.
Run bundle exec gush migrate after upgrading. This will update internal data structures.
Periodically run Gush::Client.new.expire_workflows to expire data. Workflows will be automatically enrolled in this expiration, so there is no longer a need to call workflow.expire!.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)No open issues yet, or sync has not completed.