Skip to content

Commit

Permalink
Service API V2: Add methods for webapp setup to control backend
Browse files Browse the repository at this point in the history
- Add SchedulingEnabled to Myth/GetBackendInfo response
- Add Myth/Shutdown method with Restart option.
- Add Myth/ManageScheduler method to enable or disable scheduler
  • Loading branch information
bennettpeter committed Mar 25, 2023
1 parent 8af2030 commit 6efed54
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
10 changes: 10 additions & 0 deletions mythtv/programs/mythbackend/mythbackend.cpp
Expand Up @@ -153,6 +153,16 @@ int main(int argc, char **argv)

gCoreContext->SetAsBackend(true);
retval = run_backend(cmdline);
// Retcode 256 is a special value to signal to mythbackend to restart
// This is used by the V2Myth/Shutdown?Restart=true API call
if (retval == 258) {
LOG(VB_GENERAL, LOG_INFO,
QString("Restarting mythbackend"));
usleep(50000);
int rc = execvp(argv[0], argv);
LOG(VB_GENERAL, LOG_ERR,
QString("execvp failed prog %1 rc=%2 errno=%3").arg(argv[0]).arg(rc).arg(errno));
}
return retval;
}

Expand Down
1 change: 1 addition & 0 deletions mythtv/programs/mythbackend/scheduler.h
Expand Up @@ -108,6 +108,7 @@ class Scheduler : public MThread, public MythScheduler

void DisableScheduling(void) { m_schedulingEnabled = false; }
void EnableScheduling(void) { m_schedulingEnabled = true; }
bool QueryScheduling(void) { return m_schedulingEnabled; }
void GetNextLiveTVDir(uint cardid);
void ResetIdleTime(void);

Expand Down
5 changes: 4 additions & 1 deletion mythtv/programs/mythbackend/servicesv2/v2envInfo.h
Expand Up @@ -25,6 +25,7 @@ class V2EnvInfo : public QObject
SERVICE_PROPERTY2( QString, HOME )
SERVICE_PROPERTY2( QString, USER )
SERVICE_PROPERTY2( QString, MYTHCONFDIR );
SERVICE_PROPERTY2( bool, SchedulingEnabled );

public:

Expand All @@ -34,7 +35,8 @@ class V2EnvInfo : public QObject
m_LCALL ( "" ),
m_LCCTYPE ( "" ),
m_HOME ( "" ),
m_MYTHCONFDIR ( "" )
m_MYTHCONFDIR ( "" ),
m_SchedulingEnabled (false)
{
}

Expand All @@ -46,6 +48,7 @@ class V2EnvInfo : public QObject
m_HOME = src->m_HOME;
m_USER = src->m_USER;
m_MYTHCONFDIR = src->m_MYTHCONFDIR;
m_SchedulingEnabled = src->m_SchedulingEnabled;
}

private:
Expand Down
31 changes: 31 additions & 0 deletions mythtv/programs/mythbackend/servicesv2/v2myth.cpp
Expand Up @@ -1113,6 +1113,9 @@ V2BackendInfo* V2Myth::GetBackendInfo( void )
pEnv->setUSER ( qEnvironmentVariable("USER",
qEnvironmentVariable("USERNAME")) );
pEnv->setMYTHCONFDIR ( qEnvironmentVariable("MYTHCONFDIR") );
auto *scheduler = dynamic_cast<Scheduler*>(gCoreContext->GetScheduler());
if (scheduler != nullptr)
pEnv->setSchedulingEnabled(scheduler->QueryScheduling());
// This needs to be first assigned to a new QString because setLogArgs
// uses a std_move which clears out the source variable while setting.
QString logArgs = logPropagateArgs;
Expand Down Expand Up @@ -1219,3 +1222,31 @@ bool V2Myth::ManageUrlProtection( const QString &sServices,
return gCoreContext->SaveSettingOnHost("HTTP/Protected/Urls",
protectedURLs.join(';'), "");
}

bool V2Myth::ManageScheduler ( bool Enable, bool Disable )
{
auto *scheduler = dynamic_cast<Scheduler*>(gCoreContext->GetScheduler());
if (scheduler == nullptr)
throw QString("Scheduler is null");
// onle and only one of enable and disable must be supplied
if (Enable == Disable)
return false;
if (Enable)
scheduler->EnableScheduling();
if (Disable)
scheduler->DisableScheduling();
return true;
}

bool V2Myth::Shutdown ( int Retcode, bool Restart )
{
if (Retcode < 0 || Retcode > 255)
return false;
if (Restart)
// Retcode 258 is a special value to signal to mythbackend to restart
// This is designed so that if the execvp fails it will give return code of 2,
// indicating failure and maybe causing the service module to restart.
Retcode = 258;
QCoreApplication::exit(Retcode);
return true;
}
7 changes: 7 additions & 0 deletions mythtv/programs/mythbackend/servicesv2/v2myth.h
Expand Up @@ -46,6 +46,8 @@ class V2Myth : public MythHTTPService
Q_CLASSINFO( "ManageDigestUser", "methods=POST" )
Q_CLASSINFO( "ManageUrlProtection", "methods=POST" )
Q_CLASSINFO( "SetConnectionInfo", "methods=POST" )
Q_CLASSINFO("ManageScheduler", "methods=POST")
Q_CLASSINFO("Shutdown", "methods=POST")


public:
Expand Down Expand Up @@ -177,6 +179,11 @@ class V2Myth : public MythHTTPService
static bool ManageUrlProtection ( const QString &Services,
const QString &AdminPassword );

static bool ManageScheduler ( bool Enable,
bool Disable );

static bool Shutdown ( int Retcode, bool Restart);

private:
Q_DISABLE_COPY(V2Myth)

Expand Down

0 comments on commit 6efed54

Please sign in to comment.