{"id":40775256,"url":"https://github.com/artofcodelabs/generic_job","last_synced_at":"2026-01-21T19:07:40.254Z","repository":{"id":32371633,"uuid":"132287191","full_name":"artofcodelabs/generic_job","owner":"artofcodelabs","description":"Run instance and class methods in the background jobs with ease. GenericJob is a higher level abstraction on the top of ActiveJob.","archived":false,"fork":false,"pushed_at":"2024-12-10T23:36:11.000Z","size":121,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-11T00:27:31.538Z","etag":null,"topics":["activejob","enhancement","plugin","queueing","rails","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/artofcodelabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-05T21:52:24.000Z","updated_at":"2024-12-10T23:35:21.000Z","dependencies_parsed_at":"2024-02-25T21:31:49.368Z","dependency_job_id":"6bebb25c-3a12-4ab0-9c72-2bf17bc61017","html_url":"https://github.com/artofcodelabs/generic_job","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/artofcodelabs/generic_job","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofcodelabs%2Fgeneric_job","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofcodelabs%2Fgeneric_job/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofcodelabs%2Fgeneric_job/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofcodelabs%2Fgeneric_job/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artofcodelabs","download_url":"https://codeload.github.com/artofcodelabs/generic_job/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofcodelabs%2Fgeneric_job/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28640316,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T18:04:35.752Z","status":"ssl_error","status_checked_at":"2026-01-21T18:03:55.054Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activejob","enhancement","plugin","queueing","rails","ruby","ruby-on-rails"],"created_at":"2026-01-21T19:07:40.150Z","updated_at":"2026-01-21T19:07:40.249Z","avatar_url":"https://github.com/artofcodelabs.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GenericJob\n\n## 🤔 Why you may need it?\n\nGenericJob is a Rails plugin.  \n\nActive Job does a great work when it comes to declaring jobs and making them run on a variety of queuing backends.  \nTo do so, you have to create a job in `app/jobs`, implement `perform` method and enqueue a job by calling `perform_later`.\n\nBut most often you just want to call a given model's or service object's method in the background.\n\nThere is an effort involved with that: \n\n1. You have to define another _job_ for every class or even a method that you want to run in the background. \n2. If the purpose of this `ApplicationJob` class is to only run a specific method in the background, it feels redundant.\n3. On the other hand, if you decide to have an additional logic in the _Job_ class, you basically lock it down to work in the background process only.\n\nThere should be an easier way. And there is. But before looking at the details, let's form our manifesto.\n\n\n### 📜 Manifesto\n\n**Every method should be possible to run in the background with the minimal effort. Wherever it makes sense.**\n\n_Controller's method running in the background does not make sense for example_ 😉\n\n\nThis is what GenericJob does. It creates a default _Job_ called `GenericJob` and delivers a module named `GenericJob::Async`.  \nInclude it in a given class to be able to run its methods in the background.\n\n## 🎮 Usage\n\nLet's assume that we have following model and \"service\" classes in the app.\n\n```ruby\nclass User \u003c ApplicationRecord\n  include GenericJob::Async\n\n  validates :full_name, presence: true\n\n  def self.fetch_twitter_for_all! opts = {}\n    find_each { |user| user.fetch_twitter! opts }\n  end\n\n  # This method stays in the model for the sake of convenience.\n  # Remember that the optimal way when dealing with external services like Twitter API \n  # is to call methods like this *** in the background ***\n  # to avoid hanging up other application requests\n  def fetch_twitter! opts = {}\n    TwitterFetcher.new(resource: self).fetch(\n      skip_email: opts[:only_name]\n    )\n  end\nend\n\nclass TwitterFetcher\n  include GenericJob::Async\n\n  def self.fetch_for_all class_name = 'User', ids = [], opts = {}\n    klass = class_name.constantize\n    records = ids.empty? ? klass.all : klass.find(ids)\n    records.each { |record| new(resource: record).fetch opts }\n  end\n\n  def initialize resource: nil, resource_id: nil, resource_class: 'User'\n    @resource = resource\n    @resource ||= resource_class.constantize.find resource_id\n  end\n\n  def fetch opts = {}\n    fetch_name\n    fetch_email unless opts[:skip_email]\n    @resource.save!\n  end\n\n  private\n\n    def fetch_name\n      # implement!\n    end\n\n    def fetch_email\n      # implement!\n    end\nend\n```\n\nAll you need to do to be able to run methods in the background is to include `GenericJob::Async` in both classes.\n\nNow you can do things like these:\n\n```ruby\n# this code calls the \"fetch_twitter!\" method in the background for the 1st User \n# in the DB with the all args passed. Before that the job is enqueued on the \"low\" queue\nUser.first.async(queue: 'low')\n          .fetch_twitter! only_name: true\n\n# this line calls \"fetch_twitter_for_all!\" class method on a queuing backend\n# with the passed args. The job is enqueued on the 'default' queue\nUser.async.fetch_twitter_for_all! only_name: true\n\n# this code initializes an object in the background and then calls \n# the \"fetch\" method. Given arguments are passed to the constructor \n# and the \"fetch\" method accordingly\nTwitterFetcher.async(queue: :default)\n              .new(resource_id: 101)\n              .fetch skip_email: true\n\n# this line calls \"fetch_for_all\" class method on a queuing backend\n# with the passed args through the \"default\" queue\nTwitterFetcher.async.fetch_for_all 'User', [12, 13], skip_email: true\n```\n\nAs you can see on the preceding examples - the usage of the `async` method is essential. This method calls `ActiveJob::Core`'s [**set** class method](http://api.rubyonrails.org/v5.0/classes/ActiveJob/Core/ClassMethods.html#method-i-set) under the hood, so you can pass to `async` method all the options that `set` supports. And remember - this is still an `ActiveJob`, so passed method attributes must have serialized type.\n\nFor more examples look inside the `test` directory.  \nBoth exemplary classes above are taken from the dummy test app inside.\n\n## 📥 Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'generic_job'\n```\n\nAnd then execute:\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n```bash\n$ gem install generic_job\n```\n\n## 📄 License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## 👨‍🏭 Author\n\nZbigniew Humeniuk from [Art of Code](http://artofcode.co)\n\n## 👀 See also\n\nIf you want to make your life easier in the other areas of the web app development as well, I strongly recommend you to take a look at my other project called [Loco framework](https://github.com/locoframework) 🙂. It is even more powerful and makes a front-end \u003c-\u003e back-end communication a breeze (among other things).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartofcodelabs%2Fgeneric_job","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartofcodelabs%2Fgeneric_job","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartofcodelabs%2Fgeneric_job/lists"}