swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 390 forks source link

Any way to run locally without restart swoole server to apply code changes? [SOLVED] #418

Closed Arkanius closed 4 years ago

Arkanius commented 4 years ago

Doubt

Hello, I'd like to know if there is a way to use swoole locally and restart swoole server automatically when code changes.

I just installed it in my app and worked well but it's a little boring to need to restart swoole server every time that code changes.

Arkanius commented 4 years ago

I'm currently using Ubuntu here and tried to install fswatch as mentioned in this package but it doesn't worked. Does anyone have any idea?

mRamadan0101 commented 4 years ago

till now u must restart swoole server

Arkanius commented 4 years ago

@mRamadan0101 so, we can't use this locally, right?

paschaldev commented 4 years ago

You can setup auto restart, this works for me.

Install these packages

npm i colors chokidar is-running

or 

yarn add colors chokidar is-running

Add this file in your root directory

watcher.js

let fs = require('fs')
let chokidar = require('chokidar')
let colors = require('colors/safe')
let isRunning = require('is-running')

// Define our watching parameters
let basePath = process.cwd()
let pidPath = basePath + '/storage/logs/swoole_http.pid'
let pid = fs.readFileSync(pidPath, 'utf8').split(',')
let ready = false

let logger = (color, message, level = 'log', skipSignal = false) => {
  console[level](colors[color](message))

  if (ready && !skipSignal) {
    sendSignal()
  }
}

let sendSignal = () => {
  pid.forEach((pid) => {
    if (!isRunning(pid)) {
      ready = false
      logger('red', `PID ${pid} is not alive. Close watcher process.`, 'error')
      process.exit()
    }

    process.kill(pid, 'SIGUSR1')
    logger('green', `Reloading process PID ${pid}...`, 'log', true)
  })
}

pid.forEach((pid) => {
  if (!isRunning(pid)) {
    logger('red', `PID ${pid} is not alive.`, 'error')
    return
  } else {
    logger('green', `PID ${pid} is alive, start watching process...`)
  }
})

// Initialize watcher.
// Define your paths here.
let watcher = chokidar.watch([
    basePath + '/app',
    basePath + '/resources',
    basePath + '/routes'
  ], {
  ignored: /(^|[\/\\])\../,
  persistent: true
})

// Add event listeners.
watcher
  .on('add', path => logger('yellow', `File ${path} has been added.`))
  .on('change', path => logger('yellow', `File ${path} has been changed.`))
  .on('unlink', path => logger('yellow', `File ${path} has been removed.`))
  .on('addDir', path => logger('yellow', `Directory ${path} has been added.`))
  .on('unlinkDir', path => logger('yellow', `Directory ${path} has been removed.`))
  .on('error', error => logger('red', `Watcher error: ${error}`))
  .on('ready', () => {
    logger('green', 'Initial scan is finished. Ready for watching changes...')
    ready = true
  })

Add this to your .env file

SWOOLE_HOT_RELOAD_ENABLE = true

After you start your server, run node watcher.js

TIP: Make sure your server has started before running the node script. Don't run the node script first else it won't work because it needs to pick the exact process ID.

mRamadan0101 commented 4 years ago

@mRamadan0101 so, we can't use this locally, right?

you need to restart server when you make any change

Arkanius commented 4 years ago

Thanks @paschaldev ! Worked well. I just had to change the SIGUSR1 to SIGKILL (my supervisor is restarting the watcher.js)

paschaldev commented 4 years ago

@Arkanius You're welcome 👍👍