INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Scheduling/automating Handheld Sync  (Read 617 times)

d_pert

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 398
  • I love music and great audio!
Scheduling/automating Handheld Sync
« on: October 05, 2020, 02:22:32 am »

Hi,

Is there any way to schedule / automate multiple Handheld Device syncs as a scheduled task?

I'd like to sync four devices (which are actually folders on an HDD) during the wee hours (e.g., 3 a.m.). That way any new media added during the previous day would "just be there" the next day. MC would have to queue the multiple devices so that multiple independent syncs wouldn't try to run at the same time.

Thank you.
Logged
Derek Pert
(Windows 11 Pro x64 / 32GB RAM)

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Scheduling/automating Handheld Sync
« Reply #1 on: October 05, 2020, 11:47:21 am »

Yes. I do exactly this. There are MCWS commands that can launch the Handheld Sync. A few tips though:

I've found MC doesn't always fully calculate the changes in handheld syncs until you've quit and relaunched MC or opened the Handheld item in the Tree and viewed it onscreen. It always calculates it properly after being relaunched, but if you run it multiple times without closing and restarting MC "in between" you may not always get all of the changes you should. So, I've scripted MC to shut itself down and relaunch every night before running the sync.

Here's a VBS script that does that:
Code: [Select]
' glynor's MC-Quit_Relaunch_Server.vbs script
' ************************************************************************************************************
' ************************************************************************************************************
' PURPOSE: Quits and relaunches MC in Media Server Mode..
' ************************************************************************************************************
' RELEASE NOTES:
' ************************************************************************************************************
' 1.0.0 (2020-07-19)
' http://glynor.com/files/jriver/MC-Quit_Relaunch_Server.zip
' 1. Initial Release

'Create our handy dandy Shell object
set WshShell = WScript.CreateObject("WScript.Shell")

'Quit MC Entirely
WshShell.Run "MCcl.exe /Close"

'Wait a minute
WScript.Sleep 60000

'Relaunch MC in Media Server Mode
WshShell.Run "MCcl.exe /MediaServer"
Download: http://glynor.com/files/jriver/MC-Quit_Relaunch_Server.zip

EDIT: To be clear, it doesn't matter exactly when you exit and re-launch MC, so long as it happens at some point "between runs" of the sync job (and it may not be needed at all if your sync list is more manageable than mine). I have MC-Quit_Relaunch_Server.vbs set to run at 4am daily, and then my various different sync jobs at 4:30am (different ones on different days). Works perfectly.

Second, if your sync list is large (one of mine is 40k files) it can take a LONG time to do the initial sync calculation. You can trigger MC to automatically "recalculate the sync" by telling it to sync (and if it isn't ready, it'll get ready). But, if the recalculation takes too long, MCWS will reply back that the sync failed (but keep going recalculating). Therefore, when you script it, you'll probably want to have it run a few times (waiting in between) to get it to fully sync. The first try "fails" (but does the recalculation), and the second one actually runs the sync.

I have a script for this too. It runs the sync, checks to see if it fails or succeeded. If it failed, then it waits a configurable period of time and retries. You have to edit this one to point at your server, and then you run it with the /handheld option to specify which handheld in MC you want to sync.
Code: [Select]
' glynor's MC-Sync_Handheld.vbs script
' ************************************************************************************************************
' ************************************************************************************************************
' PURPOSE: Runs the specified Handheld sync in MC multiple times until it succeeds.
' REQUIRES: MC25
' MC must have Media Network enabled and a Username and Password set.
' USAGE: MC-Sync_Handheld.vbs /handheld:"Device Name"
' Optional Parameters:
' /verbosity:on - Asks for confirmation before continuing, and provides additional information.
' RETURNS: 0 if successful, 1 if failed
' ************************************************************************************************************
' RELEASE NOTES:
' ************************************************************************************************************
' 1.0.0 (2020-10-05)
' http://glynor.com/files/jriver/MC-Sync_Handheld.zip
' 1. Initial Release

' ************************************************************************************************************
' USER DEFINED VARIABLES
' ************************************************************************************************************
' You need to modify the items below to point at your MC Server.
const strServerAddress = "localhost:52199"

const strUsername = "username"
const strPassword = "password"

' This specifies the amount of time (in minutes) to wait between sync attempts.
const sleepBetweenAttempts = 10
' This specifies how many times to re-try the sync.
const maxAttempts = 3

' END USER DEFINED VARIABLES
' ************************************************************************************************************
' ************************************************************************************************************
' ALL USER DEFINED VARIABLES ARE ABOVE THIS LINE
' DON'T MODIFY ANYTHING BELOW HERE

' Info about this script
const strMyName = "MC-Sync_Handheld.vbs"

' Get command line arguments
dim colNamedArguments
dim strNewLibrary

set colNamedArguments = WScript.Arguments.Named
' Check for "/verbosity:on" argument
dim strVerbosity
if colNamedArguments.Exists("verbosity") then
' Assign the value to the strVerbosity variable and check to see if it's valid
strVerbosity = colNamedArguments.Item("verbosity")
if not (strVerbosity = "on" or strVerbosity = "off") then
ReportError strMyName & " ERROR:" & VbCrLf & VbCrLf & "/verbosity option given with invalid value.  Available values are ""on"" or ""off"".  Value given was: " & strVerbosity, 15, 1
end if
else
' Set strVerbosity to the default value of OFF
strVerbosity = "off"
'strVerbosity = "on" ' development mode
end if

' Check for "/handheld" argument
if colNamedArguments.Exists("handheld") then
' Assign the value to the targetFileType variable
strHandheldName = colNamedArguments.Item("handheld")
else
' Report the error and exit
ReportError strMyName & " ERROR:" & VbCrLf & VbCrLf & "/handheld option is required.", 0, 2
end if

' Set up the GET String
' Example:
' oXMLHTTP.open "GET", "http://" & strServerAddress & "/MCWS/v1/Handheld/Sync?Device=Wallpaper&DeviceType=Name&ShowWarnings=0", False, strUsername, strPassword
dim strUrl
strUrl = "http://" & strServerAddress & "/MCWS/v1/Handheld/Sync?Device=" & strHandheldName & "&DeviceType=Name&ShowWarnings=0"

' Set up re-attempt counter
dim attemptCount

do while attemptCount < maxAttempts
attemptCount = attemptCount + 1
' Do it, Doug!
dim result
set result = SendHttpGet

' Check results
if result.status = 200 then
if strVerbosity = "on" then ReportError strMyName & " Success!" & VbCrLf & VbCrLf & "URL: " & strUrl, 0, 0
WScript.quit 0
elseif result.status = 500 then
if attemptCount >= maxAttempts then ReportError strMyName & " FAILED Last Attempt: " & attemptCount & " (Max: " & maxAttempts & ")" & VbCrLf & VbCrLf & "URL: " & strUrl & VbCrLf & VbCrLf & "HTTP Status: " & result.status, 120, 500
ReportError strMyName & " FAILED Attempt: " & attemptCount & " (Max: " & maxAttempts & ")" & VbCrLf & VbCrLf & "URL: " & strUrl & VbCrLf & VbCrLf & "HTTP Status: " & result.status & VbCrLf & VbCrLf & "Retrying after " & sleepBetweenAttempts & " min delay...", 120, 0
WScript.Sleep sleepBetweenAttempts * 60000
else
ReportError strMyName & " FAILED:" & VbCrLf & VbCrLf & "URL: " & strUrl & VbCrLf & VbCrLf & "HTTP Status: " & result.status, 120, 1
end if
loop

function SendHttpGet
' This function actually sends the command and returns the result
dim oXMLHTTP
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
On Error Resume Next
oXMLHTTP.open "GET", strUrl, false, strUsername, strPassword
oXMLHTTP.send
' Report any errors
if Err.number <> 0 then
ReportError strMyName & " ERROR: " & Err.number & VbCrLf & VbCrLf & "Description: " & Err.Description, 120, Err.Number
end if
' Set the result
set SendHttpGet = oXMLHTTP
end function

sub ReportError (strErrorMessage, intTimeout, intFatalError)
' This procedure reports an error message to the user in a popup, with an timeout
' And can optionally end the script execution with a error code given.
' Usage:
' srtErrorMessage - The full text of the error message shown to the user
' intTimeout - auto-close the error message box after this many second (set to 0 to never close the box)
' intFatalError - if > 0 this will cause the script to stop executing, and return the given value as an error code

' Create our handy dandy Shell object
dim errWshShell
set errWshShell = WScript.CreateObject("WScript.Shell")

errWshShell.Popup strErrorMessage, intTimeout, , vbExclamation

if intFatalError > 0 then
WScript.quit intFatalError
end if
end sub
Download: http://glynor.com/files/jriver/MC-Sync_Handheld.zip

Hope this helps!
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Scheduling/automating Handheld Sync
« Reply #2 on: October 05, 2020, 02:11:23 pm »

Oh, one other note!

The MC-Quit_Relaunch_Server.vbs script uses my MCcl.exe utility to run the command line switches for MC. This little tool just forwards commands to the current MC Command Launcher (for example mc27.exe), so that you don't have to manually edit your scripts each time you upgrade MC to a new major version.

That's handy, so if you want it, you can grab it here:
https://blog.glynor.com/mccl

If not, in that first Quit/Relaunch script, just change all instances of the MCcl.exe to MC27.exe.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

d_pert

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 398
  • I love music and great audio!
Re: Scheduling/automating Handheld Sync
« Reply #3 on: October 05, 2020, 04:01:55 pm »

Hi glynor:

Wow -- super cool.

That said, and not to reduce your past efforts, it would be nice if the functionality was just built into:

Services & Plug-ins > Scheduler > Add Task.  ;D

Thank you.
Logged
Derek Pert
(Windows 11 Pro x64 / 32GB RAM)

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Scheduling/automating Handheld Sync
« Reply #4 on: October 05, 2020, 06:02:15 pm »

Well, I don't have any control over that, though I have been asking for little bits here and there over the years (and Matt has been kind enough to oblige from time to time) that has allowed me to automate it fully.

If it were me, I would prefer to use the Windows Task Scheduler (which can run all the time, even when MC is closed, crashed, or whatever) and not the Scheduler built into MC (which would only "go" if MC is open and running). But you could, if you preferred for some reason, actually use the scheduler in MC to implement this. Just add command line tasks that point at the scripts.

Another pro-tip, if you are going to use these scripts and call them from the Windows Task Scheduler (or the MC one), you will want to point it at the wscript program (because you can't run VBS files directly at the command line in Windows).

So, scheduling the Relaunch Server script looks like:
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/
Pages: [1]   Go Up