Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.4KB

  1. <?php
  2. /**
  3. * @file src/Model/Process.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseObject;
  7. use Friendica\Database\DBA;
  8. use Friendica\Util\DateTimeFormat;
  9. /**
  10. * @brief functions for interacting with a process
  11. */
  12. class Process extends BaseObject
  13. {
  14. /**
  15. * Insert a new process row. If the pid parameter is omitted, we use the current pid
  16. *
  17. * @param string $command
  18. * @param string $pid
  19. * @return bool
  20. */
  21. public static function insert($command, $pid = null)
  22. {
  23. $return = true;
  24. if (is_null($pid)) {
  25. $pid = getmypid();
  26. }
  27. DBA::transaction();
  28. if (!DBA::exists('process', ['pid' => $pid])) {
  29. $return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
  30. }
  31. DBA::commit();
  32. return $return;
  33. }
  34. /**
  35. * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
  36. *
  37. * @param string $pid
  38. * @return bool
  39. */
  40. public static function deleteByPid($pid = null)
  41. {
  42. if ($pid === null) {
  43. $pid = getmypid();
  44. }
  45. return DBA::delete('process', ['pid' => $pid]);
  46. }
  47. /**
  48. * Clean the process table of inactive physical processes
  49. */
  50. public static function deleteInactive()
  51. {
  52. DBA::transaction();
  53. $processes = DBA::select('process', ['pid']);
  54. while($process = DBA::fetch($processes)) {
  55. if (!posix_kill($process['pid'], 0)) {
  56. self::deleteByPid($process['pid']);
  57. }
  58. }
  59. DBA::commit();
  60. }
  61. }