こんにちは、トレンド調査ラボの井上寛之(@inohiro)です。 普段は、クックパッドの検索ログを基にした法人向けデータサービス「たべみる」の開発や、 広告事業周辺のデータ分析などを担当しています。 Amazon Redshiftなどのデータベースに蓄積されたログなどの大量のデータに対して、 日次や週次などの単位でバッチ処理を行っている方は多くいらっしゃると思います。 ログなどを扱うバッチ処理では、処理対象が膨大であるとアプリケーションが使うメモリが増大し、 枯渇してしまう恐れもあるため、データの扱いに気をつける必要があります。 データベース内で完結するバッチ処理ならばそこまで気にする必要は無いかもしれませんが、 外部のプログラムからデータを読み出して処理する場合は特に注意が必要です。 そこで考えられる一つの工夫として、処理対象を分割して、繰り返して処理を行う方法が挙げられます。 一般的な
Rails 5.0.1 で rails new したときに作成される Gemfile の先頭に次のブロックが挿入されるようになっていました。 git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" end これの意味 Gemfile の中で :github オプションをつけた gem について、HTTPS 経由で GitHub から取得します。 git_source は Bundler で定義されているメソッド で、引数のオプション(ここでは :github)が付いた gem の取得先 URL をブロック内で指定できます。 やる理由 Bundler が持つ :g
先週、とうとう Mackerel にグラフアノテーション機能がリリースされました。 mackerel.io この機能を使えば、「デプロイ実施」とか「ミドルウェアの設定を変更」などといったオペレーションの実施の記録に加えて、「経験値2倍キャンペーン開始」「CM放映スタート」といったビジネス施策としてのイベントの投稿もできるようになります。これらのイベントの以前・以後、といったグラフの見方がしやすくなるので、これは便利!! "Mackerel オタク" を自称している身(参考)としてはスルーできないワクワク機能!ということで、早速自分のアプリケーションにも組み込んでみることにしました。 capistrano のデプロイタスクにグラフアノテーションの POST を組み込む 今回はとりあえずのお試しということで、capistrano のデプロイタスクのうち deploy:starting でデプロ
[rails]capistrano3でrole指定したサーバが複数台ある場合に、ある1台でのみ実行する方法 capistrano 3.2.1 で確認 capistrano3でrole指定したサーバが複数台ある場合に、ある1台でのみ実行する方法です。 namespace :deploy do task :primary_task do # primaryメソッドを使用 on primary :app do # :app role のprimaryサーバでのみ実行される処理 end end end上記のようにprimaryメソッドを使うことでできました。 db:migrateもこのprimaryメソッドで実行されているようです。 server設定で明示的にprimaryを指定していない場合は、該当roleの1台目のサーバがprimaryになるようです。 検証として、以前このブログで紹介した s
dependent: :restrict_with_error と :restrict_with_exception の違いRails はじめに:dependent オプションとは? dependent オプションはRailsであるモデルが子のレコードを持っている場合、親レコードを削除するときに子レコードをどうするのかを決めるオプションです。実際の挙動はいくつかの選択肢の中から選ぶことができます。 オプションの種類 :destroy 親と一緒に子レコードも削除する。(無理心中パターン) :delete_all 親と一緒に子レコードも削除する。ただし、直接DBのレコードを削除するので、子レコードのコールバック処理は実行されない。 :nullify 子レコードの外部キーを NULL 更新する。(みなしごパターン) :restrict_with_exception 子レコードがある場合は Act
はじめに 2016年のバージョン5.0のリリースに引き続き、2017年の春にRuby on Rails(以下Rails)のバージョン5.1がリリースされました。 Rails 5.0は、APIモードやAction Cableなど、APIやWebSocket周りの新機能を中心としたリリースでしたが、Rails 5.1は、React / Vue.js / webpack / Yarn(npmパッケージ)のサポートなど、フロントエンド周りの新機能を中心としたリリースとなりました。 本記事では、GitHubのRailsプロジェクトのIssuesやPull Requestsの履歴、Railsの公式ブログの記事をもとに、Rails 5.1の主要な新機能・変更点の紹介を行います。 ※ 他のバージョンのRailsの主要な新機能・機能追加・変更点については以下を参照してください。 今から知っておきたいRail
rake db:migrate して、検証のために rake db:rollback したところエラーで怒られたのでシェア 環境 Ruby : ruby 2.2.3p173 Rails : Rails 4.2.1 状況 既存のインデックスにUNIQUEインデックスに変更したかったので以下のような migration を書いた class AddUniquenessToCompanies < ActiveRecord::Migration def change remove_index :companies, :url add_index :companies, :url, unique: true end end $ rake db:migrate == 20160124130800 AddUniquenessToCompanies: migrating =================
はじめに kakurenboを公開してから1年が経過し、いろいろ思うところがあり acts_as_paranoid の呪縛から脱却した論理削除gemである kakurenbo-puti を公開しました。 経緯についてはこちらをご覧ください。 kakurenbo-puti kakurenbo(acts_as_paranoid系のgem)はrailsの削除機能を論理削除に置き換えることを目的に作られたgemですが、kakurenbo-putiはrailsに論理削除機能を追加する目的で作られたgemです。 置き換えとは異なり、ActiveRecordを魔改造するgemではありませんので、ActiveRecordの内部構造に左右されにくく、コードもシンプル(コアファイルは70行程度)になるため、堅牢さや保守性の面でかなり優秀なgemと言えると思います。 alfa-jpn/kakurenbo-put
私が論理削除で使うライブラリは専らkakurenbo-putiです。 ActiveRecordと疎結合なので、ハマることが少ないです。 今回はDeviseと合わせてみました。 kakurenbo-putiを使えるようにする Gemfileに以下を追加します。 gem 'kakurenbo-puti' bundle installしてから、カラムを追加しましょう。 rails g migration AddSoftDestroyedAtToUser soft_destroyed_at:datetime:index bin/rake db:migrateを実行します。 あとは、Modelにsoft_deletableを入れるだけ。 class User < ActiveRecord::Base soft_deletable end こうすると、論理削除用のメソッドが生えてきます。 emailの
この記事はSupership株式会社 Advent Calendar 2016の10日目の記事になります。 私はSupershipの所属ではありませんが、同じSyn.ホールディングスのグループ会社である株式会社Socketの @Peranikov と申します。 Socketでは、弊社のプロダクトであるFlipdeskの開発メンバーとしてRubyとScalaに取り組んでおります。 FlipdeskはRuby on Railsで稼働しており、AWS上にインスタンスを立てて運営しております。各インスタンスのログを集約するためにfluentdを使用している方は増えてきているかと思いますが、弊社もご多分に漏れずfluentdを使用しております。今回は、RailsのログをLTSV形式で出力し、fluentdを使ってCloudWatch Logsに集約する方法をご紹介します。 RailsのログをLTSV
はじめに みなさんにdisられて久しいsprockets氏ですが、メリットはそのままこれまでの問題を解決してくれるsprockets-commonerという素晴らしいgemを見つけたので紹介します。 sprockets-commonerとは sprockets-commonerとは、Railsコミッターも在籍するShopifyで作られたsprocketsの拡張gemです。 このgemの機能の中でも特に嬉しいのは以下の2つの機能です。 1. node.js/ESnextでフロントエンドを書けるようになる sprockets-commonerを入れると、sprockets管理下のJSファイルをbabelでトランスパイルしてくれるようになります。 そのため、node.js/ESnextでフロントエンドを書けるようになります。 一応、sprockets次バージョンの4でもESnextは書けるように
Action Mailerのdelivery_methodに独自の配信方法を追加してみます。 Action Mailerは内部で mail gem を使っていますが、このgemは配信方法がpluggableになっており、 配信方法にsmtp、sendmail、file(メールをファイルとして保存する)、testが選べます1。 で、自社でメール送信APIというものを作った2のでAction MailerからそのAPIにリクエストを投げるようにしたい。 Railsはとてもカスタマイズ性が優れており、実は独自の配信方法を簡単追加できるようになっている。 まず普通にAction Mailerのクラスを作る。 # app/mailers/user_mailer.rb class UserMailer < ActionMailer::Base def welcome_email mail(from:
こんにちは Rails5.1に向けて、DHHのjqueryを依存から外す発言を発端にフロントエンド周りが急激に発展しているので、簡単にですがまとめてみました。 各issue, PRの詳細には踏み込みませんが、知見に溢れているので読んでみるの推奨です。 間違い、足りないものがあったら編集リクエストお願いします。 jQuery依存を無くす話が出る rails(issue): Drop jQuery as a dependency jquery-ujsはjqueryに依存しないようにする jquery-ujs: Drop jQuery as a dependency "jquery"-ujsじゃなくなったので名前変更 rails-ujs誕生 実際にRailsからjquery依存がなくなる rails: Drop jQuery as a dependency jsライブラリを入れる方法がnpmパッ
Sprockets 4の概要 今年リリースされる予定のRuby on Rails 5.1にはフロントエンド周りに関する多くの変更が予定されていますが、現在ベータ版のSprockets 4にも大きな機能追加と変更が行われています。 Sprockets 4は、以下のような新しい機能を持っています。 ソースマップ BabelによるES6 (ECMAScript6/ES2015)のサポート npm管理下のアセットのサポート 本記事はすでにSprocketsについてある程度知っている、Railsなどで使ったことがある読者を対象としています。 アップグレード 以前のバージョンであるSprockets 3.xは、Sprockets 4.xへの橋渡しとなるバージョンで、それ以前のバージョンで使えていた多くの機能が非推奨となっています。これらの機能は4.xでは削除されて使用できなくなっているので、Rails
DynamoDB テーブルとクライアントのクラスをマッピングする Aws::Record を使用して DynamoDB を Ruby on Rails から操作する方法をチュートリアル風にメモっとく。 Java や C# 向けには高レベルプログラミングインターフェイスが公式に用意されている。しかし、公式な Ruby 向けの高レベルプログラミングインターフェイスの Aws::Record は2016年8月にリリースされたばかりで、開発者ガイドにも詳細な記載はない。(AWS SDK for Ruby バージョン1には似たような機能が搭載されていたが、AWS SDK for Ruby バージョン2には搭載されていない。)この記事では Aws::Record と Ruby on Rails を使用して Amazon DynamoDB 入門ガイドの Ruby および DynamoDB のチュートリア
※ 弊社 feedforce で毎週行っている、インフラ共有会を元にした記事で す。AWS で Rails を使っている方を想定していますが、数人規模のチー ムを意識した内容になっています。 こちらの記事で yaml_vault を知ったので、実際に試してみました。 yaml_vault+KMSでRailsアプリのconfig/secrets.ymlを暗号化してgitにコミットして管理する - Qiita 環境変数つらい# SaaS の API Key などの秘匿情報をコードに埋め込まないために、Rails で dotenv を使っている方も多いかと思います。 しかし、README.md にあるように 作者の @bkeepers さんは production での利用は奨励していません。 dotenv was originally created to load configuration
ActiveRecordで多階層カテゴリ という投稿をしたところ、 @jnchito さんに Ancestry なるgemを教えていただきました。 stefankroes/ancestry Organise ActiveRecord model into a tree structure 準備 $ rails g model category name:string $ rake db:migrate mysql> desc categories; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+---
unicornはconfで timeout 20 とかやっとくと、20秒以上かかったらそのworkerが殺される。それはいい。問題はその殺され方にあって、タイムアウトしたunicorn workerはmasterにKILLシグナルで強制的に殺される。KILLで殺されてしまうと、worker側でtrapして安全に終了処理をすることができない。 一番困るのは、Railsのloggerは(production環境のデフォルトだと)リクエストが終了するまでバッファリングしているので、リクエストの途中でKILLされてしまうとloggerがflushされない。つまり、unicornのタイムアウト時には、リクエストのログは一切production.logには出力されない。異常時のログが出ないとか、まじで困ると思うんだけど、みんなどうしてんだろ。 これに対処するためにはunicornのコードに手を入れるの
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く