after-update.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Post-update hook for the manage client (MANAGE_UPDATE_POST_HOOK).
  4. *
  5. * Runs after every successful deployment, after the release migrations in
  6. * migrations/ and only if those succeeded. At this point the new files are
  7. * already in place — there is no rollback — so this does housekeeping that is
  8. * safe to repeat and never anything that could fail a working installation.
  9. *
  10. * Deliberately not done here: the per-gallery data migration from
  11. * app/migrate.php. It touches every gallery file and stays an operator
  12. * decision, announced on the dashboard and run from admin/migrate.php.
  13. */
  14. declare(strict_types=1);
  15. /**
  16. * @param array $context app_root, instance, from_version, to_version,
  17. * backup_dir, run_id, migrations
  18. */
  19. function after_update_hook(array $context): void
  20. {
  21. $root = (string)$context['app_root'];
  22. // 1. Directories a fresh checkout does not have and an update never
  23. // creates by copying, because the release package carries no data.
  24. foreach ([$root . '/data', $root . '/data/galleries', $root . '/media'] as $dir) {
  25. if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
  26. throw new RuntimeException('Could not create directory: ' . $dir);
  27. }
  28. }
  29. // 2. The archive worker's lock. If the update landed while a gallery ZIP
  30. // was being built, that PHP process is gone but its lock file is not,
  31. // and every later run would refuse to start. The queue itself is left
  32. // alone — the next visitor kicks it (see app/archive.php).
  33. $lock = $root . '/data/archive.lock';
  34. if (is_file($lock) && time() - (int)@filemtime($lock) > 300) {
  35. @unlink($lock);
  36. }
  37. // 3. Discard compiled bytecode of the files just replaced, for hosts where
  38. // OPcache would otherwise keep serving the old code until it restarts.
  39. if (function_exists('opcache_reset') && ini_get('opcache.enable')) {
  40. @opcache_reset();
  41. }
  42. // 4. A line per update, so "when did this installation change" is
  43. // answerable without the manage server.
  44. @file_put_contents(
  45. $root . '/data/update-history.log',
  46. sprintf(
  47. "%s %s -> %s (migrations: %s)\n",
  48. date(DATE_ATOM),
  49. $context['from_version'] !== '' ? $context['from_version'] : 'unknown',
  50. $context['to_version'],
  51. $context['migrations'] === [] ? 'none' : implode(', ', $context['migrations']),
  52. ),
  53. FILE_APPEND | LOCK_EX,
  54. );
  55. }