Record.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use walle\command\Command;
  5. /**
  6. * This is the model class for table "record".
  7. *
  8. * @property integer $id
  9. * @property integer $user_id
  10. * @property integer $task_id
  11. * @property integer $status
  12. * @property integer $action
  13. * @property integer $at
  14. * @property integer $duration
  15. * @property integer $memo
  16. */
  17. class Record extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * 服务器权限检查
  21. */
  22. const ACTION_PERMSSION = 1;
  23. /**
  24. * 本地代码更新
  25. */
  26. const ACTION_CLONE = 2;
  27. /**
  28. * 同步代码到服务器
  29. */
  30. const ACTION_SYNC = 3;
  31. /**
  32. * 软链接
  33. */
  34. const ACTION_LINK = 4;
  35. public static $ACTION_PERCENT = [
  36. self::ACTION_PERMSSION => '25',
  37. self::ACTION_CLONE => '50',
  38. self::ACTION_SYNC => '75',
  39. self::ACTION_LINK => '100',
  40. ];
  41. /**
  42. * @inheritdoc
  43. */
  44. public static function tableName()
  45. {
  46. return 'record';
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function rules()
  52. {
  53. return [
  54. [['user_id', 'task_id', 'status'], 'required'],
  55. [['user_id', 'task_id', 'status', 'created_at', 'duration', 'action'], 'integer'],
  56. [['memo', 'command'], 'string'],
  57. ];
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'user_id' => 'user_id',
  67. 'task_id' => 'task_id',
  68. 'status' => 'Status',
  69. 'created_at' => 'created_at',
  70. 'action' => 'action',
  71. 'duration' => 'duration',
  72. 'memo' => 'memo',
  73. ];
  74. }
  75. /**
  76. * 保存记录
  77. *
  78. * @param Command $commandObj
  79. * @param $task_id
  80. * @param $action
  81. * @param $duration
  82. * @return mixed
  83. */
  84. public static function saveRecord(Command $commandObj, $task_id, $action, $duration) {
  85. $record = new static();
  86. $record->attributes = [
  87. 'user_id' => \Yii::$app->user->id,
  88. 'task_id' => $task_id,
  89. 'status' => (int)$commandObj->getExeStatus(),
  90. 'action' => $action,
  91. 'created_at' => time(),
  92. 'command'=> var_export($commandObj->getExeCommand(), true),
  93. 'memo' => var_export($commandObj->getExeLog(), true),
  94. 'duration' => $duration,
  95. ];
  96. // file_put_contents('/tmp/xx', var_export($record->attributes, true).PHP_EOL.PHP_EOL, 8);
  97. return $record->save();
  98. }
  99. }