Heroku scheduler

[vagrant@localhost heroku]$ heroku addons:create scheduler:standard
Creating scheduler:standard on ⬢ peaceful-garden-15590… !
▸ Please verify your account to install this add-on plan (please enter a credit card) For more information, see
▸ https://devcenter.heroku.com/categories/billing Verify now at https://heroku.com/verify
[vagrant@localhost heroku]$ heroku addons:open scheduler
▸ Couldn’t find that add on.

あれ、何で?
コンソールにログインして見ましょう。

Heroku schedulerって金かかるの?

Herokuにdeploy

[vagrant@localhost local]$ mkdir heroku
[vagrant@localhost local]$ cd heroku
[vagrant@localhost heroku]$ ls
[vagrant@localhost heroku]$ touch index.php
[vagrant@localhost heroku]$ touch composer.json
[vagrant@localhost heroku]$ ls
composer.json index.php
[vagrant@localhost heroku]$ git init
Initialized empty Git repository in /home/vagrant/local/heroku/.git/
[vagrant@localhost heroku]$ git add .
[vagrant@localhost heroku]$ git commit -m “Hello heroku commit”
[master (root-commit) 683c29e] Hello heroku commit
Committer: vagrant
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config –global user.name “Your Name”
git config –global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

git commit –amend –author=’Your Name

1 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 composer.json
create mode 100644 index.php
[vagrant@localhost heroku]$ heroku create
Creating app… done, ⬢ peaceful-garden-15590
https://peaceful-garden-15590.herokuapp.com/ | https://git.heroku.com/peaceful-garden-15590.git
[vagrant@localhost heroku]$ git push heroku master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 295 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Compressing source files… done.
remote: Building source:
remote:
remote: —–> PHP app detected
remote:
remote: NOTICE: Your ‘composer.json’ is completely empty!
remote:
remote: A completely empty file is not a valid JSON document.
remote:
remote: Heroku automatically corrected this problem, but it is strongly
remote: recommended you change the contents to at least ‘{}’.
remote:
remote: For documentation on Composer and dependency management, check
remote: out the introduction at https://getcomposer.org/doc/00-intro.md
remote:
remote: —–> Bootstrapping…
remote: —–> Installing platform packages…
remote: NOTICE: No runtime required in composer.lock; using PHP ^7.0.0
remote: – php (7.3.8)
remote: – apache (2.4.39)
remote: – nginx (1.16.0)
remote: —–> Installing dependencies…
remote: Composer version 1.9.0 2019-08-02 20:55:32
remote: —–> Preparing runtime environment…
remote: NOTICE: No Procfile, using ‘web: heroku-php-apache2’.
remote: —–> Checking for additional extensions to install…
remote: —–> Discovering process types
remote: Procfile declares types -> web
remote:
remote: —–> Compressing…
remote: Done: 15.7M
remote: —–> Launching…
remote: Released v3
remote: https://peaceful-garden-15590.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy… done.
To https://git.heroku.com/peaceful-garden-15590.git
* [new branch] master -> master

ほう、rubyじゃなくてもいけるやんか。
で、Herokuでcronってどうやるんだ?

あれ、Heroku Schedulerで行けそう?? もしかして^^

.bash_profile

– 設定ファイル
– ログインした時に読み込まれる
– ログインシェルがbashの状態でログインした時に読み込まれる
– bash_profileはログインした直後のホームディレクトリにある

なんかエラーが出ている

[vagrant@localhost rails]$ gem install pg -v ‘1.1.4’
Building native extensions. This could take a while…
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.

current directory: /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pg-1.1.4/ext
/home/vagrant/.rbenv/versions/2.3.1/bin/ruby -I /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0 -r ./siteconf20190809-24267-2gatu1.rb extconf.rb
checking for pg_config… no
No pg_config… trying anyway. If building fails, please try again with
–with-pg-config=/path/to/pg_config
checking for libpq-fe.h… no
Can’t find the ‘libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/home/vagrant/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
–with-pg
–without-pg
–enable-windows-cross
–disable-windows-cross
–with-pg-config
–without-pg-config
–with-pg_config
–without-pg_config
–with-pg-dir
–without-pg-dir
–with-pg-include
–without-pg-include=${pg-dir}/include
–with-pg-lib
–without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pg-1.1.4 for inspection.
Results logged to /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/gem_make.out
[vagrant@localhost rails]$ find ~/.rbenv | grep mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/msgpack-1.3.1/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/nio4r-2.4.0/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/ffi-1.11.1/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/nokogiri-1.10.3/mkmf.log
[vagrant@localhost rails]$ cat /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log
find_executable: checking for pg_config… ——————– no

——————–

find_header: checking for libpq-fe.h… ——————– no

“gcc -o conftest -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-linux -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/home/vagrant/.rbenv/versions/2.3.1/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat conftest.c -L. -L/home/vagrant/.rbenv/versions/2.3.1/lib -Wl,-R/home/vagrant/.rbenv/versions/2.3.1/lib -L. -L/home/vagrant/.rbenv/versions/2.3.1/lib -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-R/home/vagrant/.rbenv/versions/2.3.1/lib -L/home/vagrant/.rbenv/versions/2.3.1/lib -lruby-static -lpthread -lrt -ldl -lcrypt -lm -lc”
checked program was:
/* begin */
1: #include “ruby.h”
2:
3: int main(int argc, char **argv)
4: {
5: return 0;
6: }
/* end */

“gcc -E -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-linux -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/home/vagrant/.rbenv/versions/2.3.1/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat conftest.c -o conftest.i”
conftest.c:3:22: error: libpq-fe.h: そのようなファイルやディレクトリはありません
checked program was:
/* begin */
1: #include “ruby.h”
2:
3: #include /* end */

——————–
え?

ターミナルベースでherokuにログイン

[vagrant@localhost ~]$ heroku –version
heroku/7.27.1 linux-x64 node-v11.14.0
[vagrant@localhost ~]$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/54d1a025-4f83-4774-93e8-afe0faba719a
› Warning: Cannot open browser.
heroku: Waiting for login… ⣯
^C
[vagrant@localhost ~]$ heroku login –interactive
heroku: Enter your login credentials
Email:

Herokuを使おう

やっと戻って来ました

toolbeltはCLIのようですね。
https://devcenter.heroku.com/articles/heroku-cli

[vagrant@localhost ~]$ curl https://cli-assets.heroku.com/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
105 1892 105 1892 0 0 2942 0 –:–:– –:–:– –:–:– 19306
This script requires superuser access.
You will be prompted for your password by sudo.
Your path is missing /usr/local/bin, you need to add this to use this installer.
[vagrant@localhost ~]$ sudo curl https://cli-assets.heroku.com/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
105 1892 105 1892 0 0 5341 0 –:–:– –:–:– –:–:– 25567
This script requires superuser access.
You will be prompted for your password by sudo.
Your path is missing /usr/local/bin, you need to add this to use this installer.
[vagrant@localhost ~]$ wget https://cli-assets.heroku.com/branches/stable/heroku-linux-amd64.tar.gz
–2019-08-09 00:02:07– https://cli-assets.heroku.com/branches/stable/heroku-linux-amd64.tar.gz
cli-assets.heroku.com をDNSに問いあわせています… 13.32.54.88, 13.32.54.49, 13.32.54.116, …
cli-assets.heroku.com|13.32.54.88|:443 に接続しています… 接続しました。
HTTP による接続要求を送信しました、応答を待っています… 301 Moved Permanently
場所: https://cli-assets.heroku.com/heroku-linux-x64.tar.gz [続く]
–2019-08-09 00:02:08– https://cli-assets.heroku.com/heroku-linux-x64.tar.gz
cli-assets.heroku.com:443 への接続を再利用します。
HTTP による接続要求を送信しました、応答を待っています… 200 OK
長さ: 29106114 (28M) [application/gzip]
`heroku-linux-amd64.tar.gz’ に保存中

100%[==============================================================================>] 29,106,114 1.58M/s 時間 16s

2019-08-09 00:02:24 (1.76 MB/s) – `heroku-linux-amd64.tar.gz’ へ保存完了 [29106114/29106114]

[vagrant@localhost ~]$ ls
centos6 heroku-linux-amd64.tar.gz index.php zend

[vagrant@localhost ~]$ ls
centos6 heroku-linux-amd64.tar.gz index.php zend
[vagrant@localhost ~]$ tar xf heroku-linux-amd64.tar.gz
[vagrant@localhost ~]$ ls
centos6 heroku heroku-linux-amd64.tar.gz index.php zend
[vagrant@localhost ~]$ sed -i -e “/^export PATH$/i PATH=\$HOME/heroku/bin:\$PATH” ~/.bash_profile
[vagrant@localhost ~]$ export PATH=$HOME/heroku-client/bin:$PATH
[vagrant@localhost ~]$ heroku –version
bash: heroku: コマンドが見つかりません
[vagrant@localhost ~]$ export PATH=$HOME/heroku/bin:$PATH
[vagrant@localhost ~]$ heroku –version
heroku/7.27.1 linux-x64 node-v11.14.0

おいおいおい

[vagrant@localhost .ssh]$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
65:0a:17:62:84:01:83:3e:c4:44:43:b5:e1:d0:63:0a vagrant@localhost.localdomain
The key’s randomart image is:
+–[ RSA 4096]—-+
|=B=+.++ . |
|Eo+=+. . . |
|+ oo. . . o |
| + o + |
| . S |
| |
| |
| |
| |
+—————–+
[vagrant@localhost .ssh]$ ls
authorized_keys id_rsa id_rsa.pub
なんか眠くなって来た。

sudo passwd postgres

[vagrant@localhost ~]$ sudo find / -name pg_hba.conf
/var/lib/pgsql/9.6/data/pg_hba.conf
[vagrant@localhost ~]$ sudo cat /var/lib/pgsql/9.6/data/pg_hba.conf
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 ident
#host replication postgres ::1/128 ident

[vagrant@localhost ~]$ sudo passwd postgres
ユーザー postgres のパスワードを変更。
新しいパスワード:
よくないパスワード: 辞書の単語に基づいています
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
[vagrant@localhost ~]$ su – postgres
パスワード:
-bash-4.1$ psql
psql (9.6.15)
“help” でヘルプを表示します.

postgres=# \q
-bash-4.1$ exit
logout

来た!事前準備にどれだけ時間かかってるんじゃ、という感じ。

postgresをインストールする

sudo yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
sudo yum update
sudo yum install postgresql96 postgresql96-server postgresql96-libs
[vagrant@localhost ~]$ rpm -qa | grep postgres
postgresql96-server-9.6.15-1PGDG.rhel6.x86_64
postgresql96-9.6.15-1PGDG.rhel6.x86_64
postgresql96-libs-9.6.15-1PGDG.rhel6.x86_64

[vagrant@localhost ~]$ service postgresql-9.6 initdb
データベースを初期化中: mkdir: ディレクトリ `/var/lib/pgsql’ を作成できません: 許可がありません
[vagrant@localhost ~]$ sudo service postgresql-9.6 initdb
データベースを初期化中: [ OK ]

[vagrant@localhost ~]$ sudo service postgresql-9.6 start
postgresql-9.6 サービスを開始中: [ OK ]
[vagrant@localhost ~]$ service postgresql-9.6 status
postgresql-9.6 (pid 20665) を実行中…
[vagrant@localhost ~]$ chkconfig postgresql-9.6 –list
postgresql-9.6 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[vagrant@localhost ~]$ chkconfig postgresql-9.6 on
この操作を実行するためには権限が十分ではありません。
[vagrant@localhost ~]$ sudo chkconfig postgresql-9.6 on
[vagrant@localhost ~]$ chkconfig postgresql-9.6 –list
postgresql-9.6 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[vagrant@localhost ~]$ su – postgres
パスワード:
su: パスワードが違います
なにいいいいいいいいいいいい

ip a

Address information is displayed for each network device.
[vagrant@localhost public]$ ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:40:17:62 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::a00:27ff:fe40:1762/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:65:e0:5c brd ff:ff:ff:ff:ff:ff
inet 192.168.33.10/24 brd 192.168.33.255 scope global eth1
inet6 fe80::a00:27ff:fe65:e05c/64 scope link
valid_lft forever preferred_lft forever

[vagrant@localhost public]$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[vagrant@localhost public]$ rails -v
bash: rails: コマンドが見つかりません
[vagrant@localhost public]$ git –version
git version 1.7.1
[vagrant@localhost public]$ psql –version
bash: psql: コマンドが見つかりません

railsと、psqlのインストールが必要か。
[vagrant@localhost ~]$ gem update –system

gem install rails
[vagrant@localhost ~]$ gem install bundler
Fetching bundler-2.0.2.gem
bundler’s executable “bundle” conflicts with /home/vagrant/.rbenv/versions/2.3.1/bin/bundle
Overwrite the executable? [yN] y
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Installing ri documentation for bundler-2.0.2
Done installing documentation for bundler after 4 seconds
1 gem installed
[vagrant@localhost ~]$ rails -v
Rails 5.2.3

php.ini session.save_handler

/etc/php.ini

[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = files

session.save_hanlder defines the hanlder to use when saving and retrieving data related to the session. The default is files. Npte that each extension can use its own save_handler.

先ほどElastiCacheで作成したmemcacheのエンドポイントをphp.iniに設定する

;session.save_handler = files
;session.save_path = "/tmp"
session.save_handler = "memcached"
session.save_path = "php-session.xxxxx.cfg.apne1.cache.amazonaws.com:11211""

続いて動作確認

session_start();
echo "This is Web Server 1<br>";

if (isset($_SESSION["username"])){
	echo $_SESSION["username"];
} else {
	$_SESSION["username"] = "hoge";
}

なるほど、sessionを共有する両方のphp.iniファイルで、エンドポイントを指定して共有しているのね。

$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('foo', 'var',60);
var_dump($m->get('foo'));
$m->add('hoge', 'fuga', 60);
$m->add('hoge', 'piyo', 60);
var_dump($m->get('hoge'));
$m->flush();
var_dump($m->get('foo'));