{"id":13427870,"url":"https://github.com/lardawge/carrierwave_backgrounder","last_synced_at":"2025-03-16T00:32:24.605Z","repository":{"id":657173,"uuid":"1496951","full_name":"lardawge/carrierwave_backgrounder","owner":"lardawge","description":"Offload CarrierWave's image processing and storage to a background process using ActiveJob or Sidekiq..","archived":false,"fork":false,"pushed_at":"2024-07-05T08:33:39.000Z","size":983,"stargazers_count":736,"open_issues_count":5,"forks_count":402,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-01T00:56:45.502Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lardawge.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2011-03-18T16:36:03.000Z","updated_at":"2024-08-13T18:09:07.000Z","dependencies_parsed_at":"2024-06-18T13:39:17.978Z","dependency_job_id":"7d55eb65-3820-44c3-9ec3-dafaf1d1ed0e","html_url":"https://github.com/lardawge/carrierwave_backgrounder","commit_stats":{"total_commits":266,"total_committers":45,"mean_commits":5.911111111111111,"dds":0.2857142857142857,"last_synced_commit":"e1faa5e264d6a2ee0a58ade6b0d05d1147266b86"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lardawge%2Fcarrierwave_backgrounder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lardawge%2Fcarrierwave_backgrounder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lardawge%2Fcarrierwave_backgrounder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lardawge%2Fcarrierwave_backgrounder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lardawge","download_url":"https://codeload.github.com/lardawge/carrierwave_backgrounder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221631816,"owners_count":16855013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-07-31T01:00:41.667Z","updated_at":"2024-10-27T05:30:29.561Z","avatar_url":"https://github.com/lardawge.png","language":"Ruby","funding_links":[],"categories":["File Uploading","Ruby","文件上传"],"sub_categories":["Omniauth"],"readme":"# CarrierWave Backgrounder\n\n[![Build Status](https://app.travis-ci.com/lardawge/carrierwave_backgrounder.svg?branch=master)](https://app.travis-ci.com/lardawge/carrierwave_backgrounder)\n[![Code Climate](https://codeclimate.com/github/lardawge/carrierwave_backgrounder.png)](https://codeclimate.com/github/lardawge/carrierwave_backgrounder)\n\nNOTICE: Version 1.0.0 contains breaking changes if you are coming from an earlier version.\nThe most notible change is the removal of queue backend options other than active_job and sidekiq.\nIf you are using other backends, switch over to active_job which should support your preference.\nIf you are using Sidekiq, there is nothing to change.\n\nI am a fan of CarrierWave. That being said, I don't like tying up requests waiting for images to process.\n\nThis gem addresses that by offloading processing or storaging/processing to a background task.\nWe currently support ActiveJob and Sidekiq.\n\n## Background options\n\nThere are currently two offerings for backgrounding upload tasks which are as follows;\n\n```ruby\n# This stores the original file with no processing/versioning.\n# It will upload the original file to s3.\n# This was developed to use in cases where you do not have access to the cached location such as Heroku.\n\nBackgrounder::ORM::Base::process_in_background\n```\n\n```ruby\n# This does nothing to the file after it is cached which makes it fast.\n# It requires a column in the database which stores the cache location set by carrierwave so the background job can access it.\n# The drawback to using this method is the need for a central location to store the cached files.\n#\n# IMPORTANT: Only use this method if you have full control over your tmp storage directory and can mount it on every deployed server.\n\nBackgrounder::ORM::Base::store_in_background\n```\n\n## Installation and Usage\n\nThese instructions assume you have previously set up [CarrierWave](https://github.com/jnicklas/carrierwave) and your queuing lib of choice.\n\nTake a look at the [Rails app](spec/support/dummy_app) to see examples of setup.\n\nIn Rails, add the following your Gemfile:\n\n```ruby\ngem 'carrierwave_backgrounder'\n```\n\nRun the generator which will create an initializer in config/initializers.\n```bash\nrails g carrierwave_backgrounder:install\n```\n\nYou can pass additional configuration options to Sidekiq:\n\n```ruby\nCarrierWave::Backgrounder.configure do |c|\n  c.backend :sidekiq, queue: :awesome_queue, size: 3\nend\n```\n\n**IMPORTANT FOR SIDEKIQ BACKEND** - carrierwave (default queue name) should be added to your queue list or it will not run:\n\n```yml\n:queues:\n  - [carrierwave, 1]\n  - default\n```\n\nIn your CarrierWave uploader file you will need to add a cache directory as well as change cache_storage to File:\n\n```ruby\nclass AvatarUploader \u003c CarrierWave::Uploader::Base\n  include ::CarrierWave::Backgrounder::Delay\n\n  # This is required if you are using S3 or any other remote storage.\n  # CarrierWave's default is to store the cached file remotely which is slow and uses bandwidth.\n  # By setting this to File, it will only store on saving of the record.\n  cache_storage CarrierWave::Storage::File\n\n  # It is recommended to set this to a persisted location if you are using `::store_in_background`.\n  def cache_dir\n    \"path/that/persists\"\n  end\n\n  #etc...\nend\n```\n\n### To use process_in_background\n\nIn your model:\n\n```ruby\nmount_uploader :avatar, AvatarUploader\nprocess_in_background :avatar\n```\n\nOptionally you can add a column to the database which will be set to `true` when\nthe background processing has started and to `false` when the background processing is complete.\n\nThis is set to true in the after_commit hook when the job is created. It is very useful if you are waiting to notify the user of completion.\n\n```ruby\nadd_column :users, :avatar_processing, :boolean, null: false, default: false\n```\n\n### To use store_in_background\n\nIn your model:\n\n```ruby\nmount_uploader :avatar, AvatarUploader\nstore_in_background :avatar\n```\n\nAdd a column to the model which will store the temp file location:\n\n```ruby\nadd_column :users, :avatar_tmp, :string\n```\n\n## Usage Tips\n\n### Bypass backgrounding\nIf you need to process/store the upload immediately:\n\n```ruby\n@user.process_\u003ccolumn\u003e_upload = true\n```\n\nThis must be set before you assign an upload:\n\n```ruby\n@user = User.new\n@user.process_avatar_upload = true\n@user.attributes = params[:user]\n```\n\n### Override worker\nTo override the worker in cases where additional methods need to be called or you have app specific requirements, pass the worker class as the second argument:\n\n```ruby\nprocess_in_background :avatar, MyParanoidWorker\n```\n\nThen create a worker that subclasses carrierwave_backgrounder's worker.\nEach method, #store_in_background and #process_in_background has there own worker.\n\n#### For Sidekiq\n`process_in_background` subclass `::CarrierWave::Workers::ProcessAsset`\n`store_in_background` subclass `::CarrierWave::Workers::StoreAsset`\n\n#### For ActiveJob\n`process_in_background` subclass `::CarrierWave::Workers::ActiveJob::ProcessAsset`\n`store_in_background` subclass `::CarrierWave::Workers::ActiveJob::StoreAsset`\n\n```ruby\n# Sidekiq Example\n\nclass User \u003c ActiveRecord::Base\n  mount_uploader :avatar, AvatarUploader\n  process_in_background :avatar, MyParanoidWorker\nend\n\nclass MyParanoidWorker \u003c ::CarrierWave::Workers::ProcessAsset\n  # ...or subclass CarrierWave::Workers::StoreAsset if you're using store_in_background\n\n  def error(job, exception)\n    report_job_failure  # or whatever\n  end\n\n  # other hooks you might care about\nend\n```\n\n```ruby\n# ActiveJob Example\n\nclass User \u003c ActiveRecord::Base\n  mount_uploader :avatar, AvatarUploader\n  process_in_background :avatar, MyActiveJobWorker\nend\n\nclass MyActiveJobWorker \u003c ::CarrierWave::Workers::ActiveJob::StoreAsset\n  after_perform do\n    # your code here\n  end\nend\n```\n\n### Testing with Rspec\nWe use the after_commit hook when using active_record. This creates a problem when testing with Rspec because after_commit never gets fired\nif you're using transactional fixtures. One solution to the problem is to use the [TestAfterCommit gem](https://github.com/grosser/test_after_commit).\nThere are various other solutions in which case google is your friend.\n\n## License\n\nCopyright (c) 2011 Larry Sprock\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flardawge%2Fcarrierwave_backgrounder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flardawge%2Fcarrierwave_backgrounder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flardawge%2Fcarrierwave_backgrounder/lists"}