DEV Community

Kent Fujii
Kent Fujii

Posted on

I've created a tiny library connecting Rails and Digdag

What's this?

I've created a tiny library connecting Rails and Digdag.
The library "mogura" is now available.

What is this used for?

Rake tasks are a way to manage batches in Rails.
However, batch processing tends to bloat the codebase and increase operational costs.

In some cases, introducing a professional Python ETL tool such as Airflow may be a hurdle with learning/building/operating/expenses or recruiting human resources.
In addition, it is often not a realistic option to rewrite the Rails-related domain logic contained in Rake tasks as microservices in Python.
There may be a demand to include a workflow engine within a Rails application.

I have been looking for a way to use the domain logic of a Rails application while still satisfying the requirements of a workflow engine.
And I have come up with the option of creating a tiny library connecting Rails and Digdag.

Usage

Add the following to your Rails project Gemfile and bundle install

gem 'mogura'
Enter fullscreen mode Exit fullscreen mode

Once the library is installed, try the following command in the root directory of your Rails project

$ bundle exec mogura help
Commands:
  mogura help [COMMAND] # Describe available commands or one specific command
  mogura init # Initialize Digdag files
  mogura push # Push Digdag workflows
  mogura version # Prints version
Enter fullscreen mode Exit fullscreen mode

bundle exec mogura init will create the following files in your Rails project

RAILS_ROOT/config/digdag/sample.dig
RAILS_ROOT/app/dags/sample_dag.rb
Enter fullscreen mode Exit fullscreen mode
_export:
  rb:
    require: {{ RAILS_ROOT }}/config/environment
+run:
  rb>: SampleDag.run
Enter fullscreen mode Exit fullscreen mode
class SampleDag
  def run
    puts "Hello Rails #{Rails.env}"
  end
end
Enter fullscreen mode Exit fullscreen mode

Commanding bundle exec mogura push will register sample.dig with Digdag.
By loading the Rails environment from Digdag inside sample.dig, you can call your domain logic of the Rails from your workflow.

Thanks for reading!

Currently, only a single Digdag project can be registered as workflows, but I think it would be better if multiple Digdag projects could be registered.
Also, this library currently allows Digdag operations only from the CLI, but I would like to be able to register and edit workflows from your domain logic of the Rails application.
And I'm actively looking for feedback or contributions to this library, feel free to have new functionality ideas to add! :)

Top comments (0)