動かざることバグの如し

近づきたいよ 君の理想に

Atomエディタがクソ重いときに試したほうがいいこと

Atomと呼ばれるテキストエディタがある。

atom.io

Electronベースで(拡張機能次第で)高機能だが、しばらく使っているとめっちゃもっさりしてくる。

タブの移動ができないぐらいには重かった。

そんなときは一度キャッシュを消すと軽くなった。

方法

Atomを閉じた状態で Atomディレクトリ内の compile-cachestorage を削除、起動する。

AtomディレクトリはOSによって異なる。例えばMac OSの場合は

cd ~/.atom
rm -rf compile-cache/*
rm -rf storage/*

で おk

pgetコマンドでwgetより爆速ダウンロード

一般的にCUIでダウンロードするコマンドっていうと「curl」か「wget」だが、いかんせん遅い。というのも分割ダウンロードに対応できてないからである。

以前にaxelコマンドという爆速ダウンローダーで記事書いたけど、それに迫る、凌ぐほどの爆速ダウンロード可能なツールがあったのでメモ

インストール

ココからバイナリファイルをダウンロードできる

Ubuntuとかの場合は「pget_linux_amd64.tar.gz」、Macの場合は「pget_darwin_amd64.zip」を選択。

以下、Ubuntuの場合

wget https://github.com/Code-Hex/pget/releases/download/0.0.6/pget_linux_amd64.tar.gz
tar xzfv pget_linux_amd64.tar.gz 
sudo mv pget_linux_amd64/pget /usr/local/bin/

使い方

$ pget 
Pget v0.0.6, parallel file download client
Usage: pget [options] URL
  Options:
  -h,  --help                   print usage and exit
  -v,  --version                display the version of pget and exit
  -p,  --procs <num>            split ratio to download file
  -o,  --output <filename>      output file to <filename>
  -d,  --target-dir <path>        path to the directory to save the downloaded file, filename will be taken from url
  -t,  --timeout <seconds>      timeout of checking request in seconds
  -u,  --user-agent <agent>     identify as <agent>
  -r,  --referer <referer>      identify as <referer>
  --check-update                check if there is update available
  --trace                       display detail error messages

見たまんまではあるが、例えばUbuntuOSのisoファイルを10分割でダウンロードしたい場合は

pget -p 10 http://cdimage.ubuntulinux.jp/releases/16.04/ubuntu-ja-16.04-desktop-amd64.iso

すると

$ pget -p 10 http://cdimage.ubuntulinux.jp/releases/16.04/ubuntu-ja-16.04-desktop-amd64.iso
Checking now http://cdimage.ubuntulinux.jp/releases/16.04/ubuntu-ja-16.04-desktop-amd64.iso
Download start from http://cdimage.ubuntulinux.jp/releases/16.04/ubuntu-ja-16.04-desktop-amd64.iso
 1388429312 / 1388429312 [============================================================================================================================================] 100.00% 30s

binding with files...
 1388429312 / 1388429312 [=============================================================================================================================================] 100.00% 1s
Complete

でダウンロードできる。マジ快適

ただし、サーバー側がAccept-Rangesに対応している必要があって、未対応(というかレジューム非対応)のサーバーに対してリクエストするとエラーを返す

pget -p 10 https://dumps.wikimedia.org/jawiki/20170701/jawiki-20^C0701-pages-articles-multistream.xml.bz2
Checking now https://dumps.wikimedia.org/jawiki/20170701/jawiki-20^C0701-pages-articles-multistream.xml.bz2
Error:
  not supported range access: https://dumps.wikimedia.org/jawiki/20170701/jawiki-20^C0701-pages-articles-multistream.xml.bz2

CentOS6でhttpsに接続するとSSL Connection Errorされる件

環境

症状

要はSSLが問題なので、httpsのサイトならなんでもよくて、例えば

curl https://twitter.com

ってやると通常はtwitterのソースが表示されるが、

SSL Connection Error

ってでる。おかげでyum updateやbundle installもできない

原因

curl のバージョンが古い

yum update curl 
$ curl -V
curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp 
Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz

が、アップデートしても症状は改善しない。なんやねん

どうもnssが原因らしい nssはNetwork Security Servicesの略でそういうライブラリ群とのこと

yum update nss

これでいけた

結論

定期的にyum updateしましょうね

ActionCable使ったRailsのnginx設定

ActionCableはwebsocketを使うので通常のRailsと設定が違う。たくさんコケる要素があるのでそのメモをまとめた。

環境

  • Rails 5.0.3
  • puma
  • Redisは今回使わない

nginxの設定

upstream puma-realtime {
  server unix:///var/www/realtime/shared/tmp/sockets/puma.sock;
}

server {
  listen 80;
  server_name hogehoge.com;
  root /var/www/realtime/current/public;
  client_max_body_size 0;

  location / {
    try_files $uri @proxy;
  }

  location /cable {
    proxy_pass http://puma-realtime;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
  }

  location @proxy {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://puma-realtime;
  }
}

もう一つコケる要素があって、developでは勝手にルーティングしてくれるけど、production環境では明示的にwebsocketのルーティングを示さなければならない。

config/routes.rb

mount ActionCable.server => '/cable'

その他設定項目

config/environments/production.rb

ActionCable.server.config.disable_request_forgery_protection = true
config.web_console.whitelisted_ips = '0.0.0.0/0'

config/cable.yml

productionの項目を adapter: asyncにしても動作したからRedisは必須というわけではない。使う場合は以下のようにadapter: redisを用いる。

development:
  adapter: async

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

javascriptでGETパラメータを整形するときはURLSearchParams

※ レガシーブラウザの対応を除く

この記事で終了、ってなっちゃうけど。

qiita.com

試してみる

let url = new URL('http://thr3a.hatenablog.com/search?q=rails&order=date');
let params = new URLSearchParams(url.search.slice(1));

params.get('q')
// => "rails"

params.has('q')
//  => true

params.set('order', 'popular')

params.getAll('q')
//  => ["rails"]

params.append('hoge', 114514)

params.delete('q');

params.toString();
// => 'order=popular&hoge=114514'

で、対応ブラウザは?

そもそも最近でたAPIだから少ない IEは全滅

http://caniuse.com/#search=URLSearchParamscaniuse.com

ChromeとかFirefox等モダンブラウザ限定でIEなんか関係ないって場面ではどんどん使っていきたい