-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerthread.c
118 lines (96 loc) · 3 KB
/
workerthread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Copyright (C) 2007 Christian Wieninger
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
The author can be reached at cwieninger@gmx.de
The project's page is at http://winni.vdr-developer.org/taskman
*/
#include <string>
#include <vdr/plugin.h>
#include "workerthread.h"
#include "taskmanager_thread.h"
#include "task.h"
#include "log.h"
#include "menu_tasks.h"
using namespace std;
cWorkerThread::cWorkerThread(long TaskId)
: taskid(TaskId)
{
cTask* task = Tasks.TaskFromId(taskid);
if (task)
{
SetDescription("'%s' worker", task->Name());
LogFile.Log(3,"creating worker thread for task '%s'", task->Name());
}
}
cWorkerThread::~cWorkerThread()
{
cTask* task = Tasks.TaskFromId(taskid);
if (task)
LogFile.Log(3,"deleting worker thread for task '%s'", task->Name());
cMenuTasks::update = true;
}
string cWorkerThread::Execute(cTask* task)
{
string command = task->Cmd();
string result = "";
dsyslog("taskman: executing command '%s'", command.c_str());
cPipe p;
if (p.Open(command.c_str(), "r")) {
int c;
while ((c = fgetc(p)) != EOF)
{
result += char(c);
task->SetCurrentOutput(result);
}
p.Close();
}
else
esyslog("ERROR: can't open pipe for command '%s'", command.c_str());
return result;
}
void cWorkerThread::Stop()
{
Cancel();
}
void cWorkerThread::Action(void)
{
cTask* task = Tasks.TaskFromId(taskid);
if (!task)
{
LogFile.Log(2,"ooops. No task with id: %ld", taskid);
return;
}
LogFile.Log(1,"starting '%s' (cmd: %s)", task->Name(), task->Cmd());
if (task->announceMode & 1)
Skins.QueueMessage(mtInfo, cString::sprintf(tr("%s started"), task->Name()), 5, 0);
task->lastStart = time(NULL);
string result = Execute(task);
if (task->logging)
{
task->SetCurrentOutput("");
task->taskLog.Insert(result);
}
cMenuTasks::update = true;
LogFile.Log(1,"finished '%s' (cmd: %s)", task->Name(), task->Cmd());
if (!result.empty())
LogFile.Log(3,"result from '%s':\n%s", task->Name(), result.c_str());
if (task->announceMode & 2)
Skins.QueueMessage(mtInfo, cString::sprintf(tr("%s finished"), task->Name()), 5, 0);
if (cTasks::IsTemp(task))
{
cMutexLock TempTasksLock(&TempTasks);
TempTasks.Del(task);
}
cTaskManagerThread::SetFinished(taskid);
}