Skip to content

Instantly share code, notes, and snippets.

@grohiro
grohiro / cpan.yml
Created May 7, 2019 08:46
Install CPAN module with Ansible
- yum:
name: perl-App-cpanminus
- cpanm:
name: "{{ item }}"
with_items:
- "Sys::Syslog"
- "DateTime"
- "LWP::Protocol::https"
@grohiro
grohiro / non_blocking_stream.php
Created April 1, 2019 16:07
Stream と Looper でノンブロッキングIOを抽象化
<?php
/**
* 複数のHTTPリクエストをノンブロッキングで多重化する
* Stream 抽象化版
* Streamクラスは `read()` メソッドが呼ばれるたびにソケットからデータを読み込む
* 読み込むデータがなくなったら `closed = true` に設定する。
*
* Looper クラスは全ての Stream が `closed = true` の状態になるまで `read()` を実行し続ける
* 全ての Stream が `closed` になったら全てのデータ読み込みが完了したことになるので終了する。
*
@grohiro
grohiro / non_blocking.php
Created April 1, 2019 05:57
PHPで外部HTTPサーバへのリクエストをノンブロッキングで実装した
<?php
/**
* 複数のHTTPリクエストをノンブロッキングで多重化する
* fsockopen()版
*
* $blocking = true を設定すると順に処理される
* $blocking = false を設定すると準備できている $sock から処理される
*/
$blocking = false;
snippet laravel-mutator-enum
public function set${1:CamelCase}Attribute($${2:camelCase})
{
if (!is_null($$2)) {
$this->attributes['${3:snake_case}'] = $$2->id;
} else {
$this->attributes['$3'] = null;
}
}
@grohiro
grohiro / gist:fb2d6daf7021c5152b2688f674d7f59f
Created November 26, 2018 11:56
Prevent double form POST with IE
// Bad. IE POSTs the form twice
<button class="btn btn-primary" onclick="if(window.confirm('よろしいですか?')){$(this).parent().submit();}else{return false;}">Go</button>
// Good
<button class="btn btn-primary" onclick="return confirm('よろしいですか?')">Go</button>
@grohiro
grohiro / gist:9a9483cf6701c56e86c3b50d4cb8a6eb
Created September 18, 2018 00:38
UltiSnipsとの組合せでPHPの複数行コメントのインデントが壊れる問題の修正
" Indent multiline /* comments correctly {{{
" UltiSnipsとの組合せでPHPの複数行コメントのインデントが壊れる問題の修正
" snippetを展開するとコメントの中にいることがわからないので再チェックしてフラグをOnにする
if last_line =~ '^\s*/\*'
echo "In multiline comment!"
let b:PHP_InsideMultilineComment = 1
endif
" ------------------------------------------------------------
@grohiro
grohiro / daemon-template.sh
Created June 23, 2018 01:05
デーモンを制御するスクリプトの雛形
function get_pid() {
if [ ! -e $PID ]
then
echo -1
return
fi
if [ $(pgrep -F $PID | wc -l) -eq 0 ]
then
rm -f "$PID"
#!/bin/bash
ctags -R --languages=php app/ tests/ config/ vendor/
@grohiro
grohiro / gist:b2806db70c1108fe5e0198224643f24d
Created October 16, 2017 10:54
Laravel logrotate config
/var/www/webapp/shared/storage/logs/*.log {
missingok
notifempty
nocompress
daily
rotate 5
}
task('logrotate:link', function () {
run('sudo ln -fs {{ current_path }}/etc/logrotate.conf /etc/logrotate.d/AppName');
});
// Create a symlink to rotate laravel logs.
on(host('stage'), function ($host) {
after('deploy:symlink', 'logrotate:link');
});