xfangfang / Macast

Macast is a cross-platform application which using mpv as DLNA Media Renderer.
https://xfangfang.github.io/Macast/
GNU General Public License v3.0
5.91k stars 373 forks source link

Add Singleton support #120

Closed happytommyl closed 2 years ago

happytommyl commented 2 years ago

103

I have made a quick and dirty way to check if an instance of the program is already running, by using the singleton module included in tendo. It will create a dummy file in the default local temp folder (platform dependant) and check if the file existed. If the file exists, it will raise an error and quit the program.

TODO:

  1. Add a toggle option in the context menu. Currently, it needs to be changed in the macast_setting.json file. I don't quite understand how the context menu works. :(
  2. A more elegant way to handle the program entry point.
xfangfang commented 2 years ago

Thanks for your contribution!

  1. The current code conflicts with the dev_test branch,
  2. If it is not necessary, do not import third-party libraries. (Because the project is packaged by pyinstaller, it requires a lot of compatibility testing, and need to fix problems with build script)
  3. Because this problem does not exist on MacOS or Linux, it can be applied only to windows at present. something like: if sys.platform == 'win32': balabala
  4. I'll handle the menu problem~ (At present, I am preparing to transfer the low-frequency menu items to Advanced Setting and set them on the web page)
happytommyl commented 2 years ago

Thanks for your contribution!

  1. The current code conflicts with the dev_test branch,
  2. If it is not necessary, do not import third-party libraries. (Because the project is packaged by pyinstaller, it requires a lot of compatibility testing, and need to fix problems with build script)
  3. ~Because this problem does not exist on MacOS or Linux, it can be applied only to windows at present. something like: if sys.platform == 'win32': balabala~
  4. I'll handle the menu problem~ (At present, I am preparing to transfer the low-frequency menu items to Advanced Setting and set them on the web page)
  1. I made a fork from the main branch, I'll check the conflicts with dev_test later. Or maybe there's a better way to handle that?
  2. It's a one-file solution for now and handles platform dependency. Maybe I'll reimplement the functionality later if you think so fit. (simple Copy-Paste will break license or not?)
  3. As mentioned above, it has platform handler built in.
  4. Thanks for that part :)
xfangfang commented 2 years ago
  1. Fork dev branch is recommended. (It's my fault. I'll update the documentation later)
  2. I don't know the difference between different licenses, but code modifications should be allowed to tendo. (You can place SingleInstance in macast/utils.py)
  3. Should the location of lockfile be set to SETTING_DIR/macast.lock manually? (Under different operating systems, different startup methods sys.argv[0] may not be an fixed value)

Ask a question: If the application exits unexpectedly and the lock file is not emptied in time, will it fail to start later?

  1. I made a fork from the main branch, I'll check the conflicts with dev_test later. Or maybe there's a better way to handle that?
  2. It's a one-file solution for now and handles platform dependency. Maybe I'll reimplement the functionality later if you think so fit. (simple Copy-Paste will break license or not?)
  3. As mentioned above, it has platform handler built in.
  4. Thanks for that part :)
happytommyl commented 2 years ago
  1. Fork dev branch is recommended. (It's my fault. I'll update the documentation later)
  2. I don't know the difference between different licenses, but code modifications should be allowed to tendo. (You can place SingleInstance in macast/utils.py)
  3. Should the location of lockfile be set to SETTING_DIR/macast.lock manually? (Under different operating systems, different startup methods sys.argv[0] may not be an fixed value)

Ask a question: If the application exits unexpectedly and the lock file is not emptied in time, will it fail to start later?

  1. Good to know
  2. I think that'll work. The implementation itself requires python built-in libraries only.
  3. os.path.abspath(sys.argv[0]) will be the same to the executable, ie Macast.exe or Macast.py. So if a user has two different copies Macast.exe file in two different locations, it can run simultaneously. If I change it to a fixed value, it won't be able to run. Both methods are OK, depending on the design purpose.
  4. The program will try to delete the .lock file first, and will raise an exception if the .lock file is in use. So there won't be a problem with unexpected exits.

Current problem:

  1. If the user started the program with single-mode turned off, then turn on the option, it won't create the .lock file until the next start-up. Maybe create the .lock file when the user toggles the switch?
xfangfang commented 2 years ago

If I put the settings for singleton mode into advanced settings, Macast will restart automatically after the settings are modified, which just avoids this problem.

Current problem:

  1. If the user started the program with single-mode turned off, then turn on the option, it won't create the .lock file until the next start-up. Maybe create the .lock file when the user toggles the switch?
xfangfang commented 2 years ago

os.path.abspath(sys.argv[0]) will be the same to the executable, ie Macast.exe or Macast.py. So if a user has two different copies Macast.exe file in two different locations, it can run simultaneously. If I change it to a fixed value, it won't be able to run. Both methods are OK, depending on the design purpose.

I think it would be better to set it to a fixed value, which is also convenient for debugging from the source code.

happytommyl commented 2 years ago

Ok, I'll make some changes and merge it to dev branch, I'll close this PR then.