[server] [2/n] Run MySQL migrations on server instantiation
Summary:
We want the MySQL migrations to run on server instantiation. We only want them run once, so it's best in the parent process before cluster.fork().
Blocking the event loop is not seen as best practice. Instead, by making server.js async we can accommodate for migrations() completion before forking workers. If migrations succeed, workers are forked. If migrations fail, Node and Nodemon will fail before workers are forked.
I had some more thoughts regarding improving server.js, and can go into details if need-be. (might be a good idea to separate logic for Master and Workers into separate files, and use IPC signals more between Master/Workers. Ideally, we don't want Master node handling anything other than forking and delegating Workers. It'd be nice for Master to delegate doMigrations() to a singly spawned Worker. Master can listen for Worker's completion signal to decide whether to fork more Workers (on migrations success) or end execution.
Test Plan:
Ran yarn dev in server to execute dist/server.js:
Migrations succeed? Workers forked
Migrations fail? process.exit(2) // this is to force Nodemon to exit as well. Otherwise, Nodemon persists while db is messed up.
Check PID of Master node and verify only Master is performing migrations.
Were Workers forked before finishing the migrations? (no)
Because InnoDB defaults to wrapping SQL statements in transactions, row-level locks are created implicitly as-needed. I think the current approach may need to be reworked if clustering becomes a concern.
Reviewers: atul, ashoat, varun
Reviewed By: atul, ashoat
Subscribers: varun, benschac, ashoat, palys-swm, Adrian, karol-bisztyga, boristopalov
Differential Revision: https://phabricator.ashoat.com/D2722