Skip to content

Instantly share code, notes, and snippets.

@myfashionhub
Last active August 29, 2015 14:25
Show Gist options
  • Save myfashionhub/03007c5b9ae6d2ff4553 to your computer and use it in GitHub Desktop.
Save myfashionhub/03007c5b9ae6d2ff4553 to your computer and use it in GitHub Desktop.
Bedrocket API: Distribution worker

Adding a new distribution to Bedrocket API:

Bedrocket-api:

  • If the service doesn’t already exist (Google, Facebook, Dailymotion), add the service & its oauth options:
# bedrocket-api/app/models/[service name]_service.rb

class DailymotionChannelService < SocialService
  oauth 2, :dailymotion, client_options: :client_options, authorize_params: :authorize_params

  store_accessor :properties,      :refresh_token
  store_accessor :properties,      :access_token

  def client_options
  end
  
  def account
  end
end
  • Add the provider distribution model and task model. Examples:
# bedrocket-api/app/models/[provider]_distribution.rb

class DailymotionDistribution < Distribution
  ATTRIBUTES = [
    playlist_ids: []
  ]

  store_accessor  :properties,        :playlist_ids
end
# bedrocket-api/app/models/[provider]_distribution_task.rb

class DailymotionDistributionTask < Task
  ATTRIBUTES = [
    :distribution_id,
    :video_title,
    :video_description,
    video_tags: []
  ]

  store_accessor  :properties,        :distribution_id
  store_accessor  :properties,        :video_title
  store_accessor  :properties,        :video_description
  store_accessor  :properties,        :video_tags

  validates   :distribution_id, presence: true
end
  • Add serializer for each of the model in bedrocket-api/app/serializers/

Bedrocket ruby-boxxspring-sdk:

  • Add the service if it doesn’t already exist:
module Boxxspring
  class DailymotionChannelService < SocialService 
  end
end
  • Add the distribution and distribution task in resources:
# ruby-boxxspring-sdk/lib/boxxspring/resources/[provider]_distribution.rb

module Boxxspring
  class DailymotionDistribution < Distribution
    field  :playlist_ids
  end
end
# ruby-boxxspring-sdk/lib/boxxspring/resources/[provider]_distribution_task.rb

module Boxxspring
  class DailymotionDistribution < Task
    field   :distribution_id
    field   :video_title
    field   :video_description
  end
end
  • Require these new files in the SDK:
# ruby-boxxspring-sdk/lib/boxxspring.rb

require 'boxxspring/resources/dailymotion_channel_service'
require 'boxxspring/resources/dailymotion_distribution'
require 'boxxspring/resources/dailymotion_distribution_task'
  • For deployment, we may want to increase the gem version (and update it on rubygems.org) if enough changes have been made since last version.

The distribution worker:

  • In development (before merging the new SDK branch into develop or master), point the boxxspring gem to the new git branch:
# Gemfile

gem 'boxxspring', git: 'https://github.com/bedrocketjmd/ruby-boxxspring-sdk.git', branch: 'feature/[provider]-distribution'
  • Run bundle update boxxspring each time pushing new changes to the SDK’s origin

  • The distribution worker inherits from the Boxxspring worker task base, thus inheriting these methods:

class TwitterDistributionWorker < Boxxspring::Worker::TaskBase
  
  def task_write_state(task, task_state, task_message)
    # Return the task on completion
  end

  def task_write(task)
    # Update all task attributes & return the updated task
  end

  def self.logger
    # Logs to STDOUT in dev and Papertrail in other environments
  end
end
  • The worker logger is configured in each environment. For example:
# dailymotion-workers/config/environments/acceptance.rb

Boxxspring::Worker.configuration do 
  logger( RemoteSyslogLogger.new( 
    'logs2.papertrailapp.com', 
    48686, 
    program: "boxxspring-twitter-workers",
    local_hostname: "#{ENV[ 'LOG_SYSTEM' ]}.acceptance"
  ) )
end
  • Bedrocket API configuration is set up in the initializers:
# dailymotion-workers/config/initializers/boxxspring.rb

Boxxspring.configuration do
 api_uri ENV[ 'API_URL' ]
end

Boxxspring::Worker.configuration do 
 api_credentials( {
  application_id:     ENV[ 'APPLICATION_ID' ], 
  application_secret: ENV[ 'APPLICATION_SECRET' ]
 } )
end
  • Create new application in the API rails console and obtain application_id and application_secret irb> Application.create(name:'Dailymotion Workers')

  • Environment variables are stored in .env, which is git ignored. Required variables:

API_URL=https://api.acceptance.boxxspring.com
APPLICATION_ID=
APPLICATION_SECRET=

AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=[unique to each developer]
AWS_SECRET_ACCESS_KEY=[unique to each developer]
LOG_SYSTEM=dailymotion_workers
WORKERS_ENV=acceptance

# Unique set of keys for each environment
[PROVIDER]_API_KEY=
[PROVIDER]_API_SECRET=

Running the worker:

  • Create a new queue for the worker in Amazon SQS and subscribe it to the corresponding environment’s tasks.

  • On acceptance or staging, create the distribution task in the rails console:

$ heroku run rails c -a [heroku app name]

irb(main):001:0> task = DailymotionDistributionTask.create(
  type: "DailymotionDistributionTask",
  property_id: 2,
  subject_type: “Artifact”,
  subject_id: [id of a video article artifact],
  properties: { "distribution_id"=>"160", "video_title"=>"Sample Video MOV" }
)
  • To see if task is valid task.save. If there’s an error, task won’t save. See error with task.errors

  • If task is idle or failed and you want it to run again, task.update(state:’idle’).save

Deploy the workers

In the Rails console:

  • Create a new application in the API of the environment you’re deploying to:
irb> Application.create(name:'Dailymotion Workers')
  • Find permissions the app needs (read/write/delete services, sources and distributions):
irb> Permission.where('code_name LIKE ?', '%services%')
  • Assign permission to the app
irb> Application.find([APP ID]).permissions << Permission.find([PERMISSION ID])

In the shell:

  • Assign values to APPLICATION_ID and APPICATION_SECRET in heroku env
$ heroku config:set APPLICATION_ID=[application id] -a [heroku app name]
API_URL=
WORKS_ENV=[acceptance, staging or production]
LOG_SYSTEM=[provider]_workers
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
  • Note: Must create a different set of PROVIDER_API_KEY and PROVIDER_API_SECRET in each environment.

  • Deploy necessary branches in the API and Ruby SDK

$ git push heroku feature/branch-name:master -a [heroku app name]
  • Scale the worker in Heroku:
$ heroku ps:scale dailymotion-distribution=1 --remote [heroku remote name]

Check to see if the worker is running: $heroku ps --remote [heroku remote name]

Others

  • Create a queue ([environment]-dailymotion_distribution) in AWS and subscribe it to the corresponding environment's tasks

  • Monitor log on Papertrail to see progress of the worker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment