clinton-hall / nzbToMedia

Provides NZB and Torrent postprocessing To CouchPotatoServer, SickBeard/SickRage, HeadPhones, Mylar and Gamez
GNU General Public License v3.0
674 stars 176 forks source link

Check if running and then exit #466

Closed depraved22 closed 10 years ago

depraved22 commented 10 years ago

I have an interesting setup that I have a home box where the majority of my items run and a seedbox that moves stuff around via btsync.

I've been using your tools now for about a week and it works very well. My only caveat so far is I use the automated postprocessing setup in SickBeard/CouchPotato for NZBs. Works great.

With torrents, I end up getting files automatically from my seedbox and they are placed in a staging are on my home box.

I use nzbToCouchPotato/SickBeard via cron and use the automated watch dirs in there. Works very well.

Only issue I get is that if for whatever reason, I get 2 instances of nzbToCouchPotato running, they overlap and the logs/movements get a little ugly. I can wrap my own script around calling them and create a lock file, but was wondering if they would be an enhancement to add.

I had an issue when it removed the directory before it actually processed it because of the overlap in time.

Make sense or is there a better method perhaps that I am not thinking of?

Thanks.

clinton-hall commented 10 years ago

Good in theory. The issue I see is how to allow the script to run later? I.e. Do we just error and stop (show an error in SABnzbd/NZBget or do we just do a while lock file, wait, so that we run when safe to do so?

The other simple way is to create a second version of the scripts. Use these for your cron for Torrents... These won't interfere with the nzb version since they are in a different directory....

I think creating a lock file and making sure this is removed on exit should work... With a wait check done on script entry should work fine. I just need to see how to remove the lock file if the script hangs/crashes.

depraved22 commented 10 years ago

Hmm. Yeah, I have to admit that I did not think completely through yet which is why I'm just a regular ole user :)

I ended just using a simple job wrapper -> http://timkay.com/solo/solo

That just creates a quick port and if it sees the port is open, it just exits out. That way my cron jobs never stomp on each other. Since my NZBGet Directory and Watch Directories are different, nothing should clobber each other now:

[CouchPotato]
    #### autoProcessing for Movies
    #### movie - category that gets called for post-processing with CPS
    [[movie]]
        enabled = 1
        apikey = "XXXs"
        host = localhost
        port = 5050
        ###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ######
        ssl = 0
        web_root = /couchpotato
        # Enable/Disable linking for Torrents
        Torrent_NoLink = 0
        method = renamer
        delete_failed = 0
        wait_for = 2
        extract = 1
        # Set this to minimum required size to consider a media file valid (in MB)
        minSize = 0
        # Enable/Disable deleteing ignored files (samples and invalid media files)
        delete_ignored = 0
        ##### Enable if Couchpotato is on a remote server for this category
        remote_path = 0
        ##### Set to path where download client places completed downloads locally for this category
        watch_dir = /data/stage/Done/Movies
    [[Movies]]
        enabled = 1
        apikey = XXX
        host = localhost
        port = 5050
        ssl = 0
        web_root = /couchpotato
        Torrent_NoLink = 0
        method = renamer
        delete_failed = 0
        wait_for = 10
        extract = 1
        minSize = 0
        delete_ignored = 0
        remote_path = 0
        watch_dir = /data/stage/Done/Movies

The only strange thing I've noticed so far is my 1st apikey= blanks out every once in awhile. Not quite sure why yet.

clinton-hall commented 10 years ago

I'm going to have a look and see if there is anything that can be done to prevent this... The reality is anyone using TorrentToMedia and nzbTo* could encounter this conflict...

For the apikey blanking out, just check to see if this happens after nzbget has run the script... I suspect it may be related to the nzbget migrate code...

depraved22 commented 10 years ago

Yep, that was it from the logs as it was there up to that ran, you can see my cron job ran over as well:

Logs: http://pastebin.com/Ysh1b6xN

clinton-hall commented 10 years ago

So what is the difference between [[movie]] and [[Movies]] which one does NZNGet use?

In nzbget web UI, for nzbToCouchPotato, what is the cpsCategory and do you have the Apikey entered here? Do you ever run nzbToMedia directly for any category? If so, check to see what apikey you have entered for CouchPotato in nzbToMedia settings...

depraved22 commented 10 years ago

Hmm. Let me clean that up. Seems like my file got merged a bit with the spec file somehow. Let me start clean and redo my config file and see if that issue goes away.

depraved22 commented 10 years ago

I should be good now. I had some inconsistencies in the config using "tv" and "TV" and "movie" and "Movies" when comparing to the nzbGet config and the cfg file as I just started using nzbGet over Sab.

I fixed all those items ran a few times and my cfg looks good. If I had a mismatch in names, it would kinda merge the cfg file with those duplicate sections.

depraved22 commented 10 years ago

I think most apps write a PID file out and check for that process. If that process is still running, it would not run a new one. If there is no process running, it removes the stale pid file and then continues on.

I noticed sickbeard removing my stale pid before :)

clinton-hall commented 10 years ago

I have just tested code to check for running instance... works fine on posix. I have Windows code, but just need to test.

clinton-hall commented 10 years ago

please let me know if this works for you... I have done a lot of testing here... but need confirmation from another system.

depraved22 commented 10 years ago

I pulled from the nightly and did some quick testing while an update was going, it waited for that to finish and then ran properly:

/opt/nzbToMedia$ ./nzbToCouchPotato.py
!!! Waiting for existing session to end!
!!! Waiting for existing session to end!
[08:45:52] [INFO]::MAIN: Loading config from [/opt/nzbToMedia/autoProcessMedia.cfg]
[08:45:52] [INFO]::MAIN: Checking database structure...
[08:45:52] [INFO]::MAIN: Checking if git needs an update
[08:45:52] [ERROR]::MAIN: Unable to determine installed version via git, please check your logs!
[08:45:52] [INFO]::MAIN: No update needed
[08:45:52] [INFO]::MAIN: nzbToMedia Version:10.0 Branch:master (Linux 3.13.0-30-generic)
[08:45:52] [INFO]::MAIN: #########################################################
[08:45:52] [INFO]::MAIN: ## ..::[nzbToMedia.pyc]::.. ##
[08:45:52] [INFO]::MAIN: #########################################################
[08:45:52] [WARNING]::MAIN: Invalid number of arguments received from client, Switching to manual run mode ...
[08:45:52] [INFO]::MAIN: Searching /data/stage/Done/Movies for mediafiles to post-process ...
[08:45:52] [INFO]::MAIN: The ./nzbToCouchPotato.py script completed successfully.

I'll keep it running on this build and see what happens over the course of a few days.

clinton-hall commented 10 years ago

Thanks... I tested on Linux and windows. In both cases, it waited for the first instance to finish... I even CRASHED the first instance and the second instance resumed.

It does the polling at 30 second intervals, so the resume is not instantaneous, but for what we want to achieve I thought 30s was a good rate.

depraved22 commented 10 years ago

I am testing on Ubuntu 14.04.

I broke a few instances as well just to test that and it worked for me as well.I can see a few waiting sessions over the last few hours that resumed properly as well.

Looks great and thanks for the insanely fast turn around!

depraved22 commented 10 years ago

I sent a little thanks as well. Have a few beers on me for your help and support :)

clinton-hall commented 10 years ago

No worries. Thanks for the suggestion, testing and support.

clinton-hall commented 10 years ago

I'm going to close this now... please let me know if anything pops up.