updating scheduled tasks

This commit is contained in:
jakani24
2024-02-17 15:09:25 +01:00
parent f21276fd5c
commit 57cf07b679
19 changed files with 118 additions and 77 deletions

View File

@@ -63,6 +63,7 @@ int main() {
//unlock_task("tsk1"); else it will only be executed once. but this function has to be called at the end of the task. else it will nvr be executed again. this would be bad :(
//start a thread that executes check_scan_dir to scan folders for new files. this thread then should start a ock so only one scanfolder thread runs at a time
//Sleep(1000);
if (can_run_thread()) {
int queue_size = get_queue_size();
for (int i = 0; i < queue_size; i++) {
@@ -70,7 +71,6 @@ int main() {
queue_entry[0] = '\0';
queue_pop(queue_entry);
//execute the function which starts the threads
// printf("%s\n", queue_entry);
start_thread(queue_entry);
delete[] queue_entry;
}

View File

@@ -89,16 +89,16 @@ int download_file_from_srv(const char* url, const char* output_file_path) {
fclose(output_file);
return 5;
}
else if(check_cert(buf, SECRETS)!=0){
if (rename(temp_path, output_file_path)) {
fclose(output_file);
else if(check_cert(buf, SECRETS)==0){
remove(output_file_path);//remove old file, so it can be overwritten
fclose(output_file);
if (rename(temp_path, output_file_path)!=0) {
return 6;
}
}else {
fclose(output_file);
return 7;
}
fclose(output_file);
}
delete[] buf;
delete[] temp_path;

View File

@@ -85,7 +85,7 @@ void unlock_task(const std::string& task_name) {
int check_for_sched_tasks(const char* sched_name, const char* sched_path) {
FILE* fp = nullptr;
char* command = new char[300];
char* command = new char[505];
if ((fopen_s(&fp, sched_path, "r")) != 0) {
//panic, create log entry, return 1;
//no schedule file found. this is not normal
@@ -94,77 +94,87 @@ int check_for_sched_tasks(const char* sched_name, const char* sched_path) {
return 1;
}
else {
fscanf_s(fp, "%s", command, 500); //the cert is always the firs tline
fgetc(fp); //get the newline
while (!feof(fp)) {
//read date-time config. it starts with " and ends with "
char* datetime = new char[300];
datetime[0] = '\0';
//search for datetime, starting ", then loop until ending "
int cnt = 0;
int chr = 0;
chr = fgetc(fp);//read in the first ", or at least try it
//printf("%c\n", chr);
if (chr == '\"'){
if (!feof(fp)) {
//read date-time config. it starts with " and ends with "
char* datetime = new char[300];
datetime[0] = '\0';
//search for datetime, starting ", then loop until ending "
int cnt = 0;
int chr = 0;
chr = fgetc(fp);//read in the first ", or at least try it
//printf("%c\n", chr);
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
datetime[cnt] = chr;
datetime[cnt + 1] = '\0';
cnt++;
//printf("scanning...\n");
}
}
//now we had datetime. we can scan the command and the path now
fscanf_s(fp, "%s", command, 295); // get the command
char* path = new char[300];
path[0] = '\0';
//search for arg, starting ", then loop until ending "
cnt = 0;
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
datetime[cnt] = chr;
datetime[cnt + 1] = '\0';
cnt++;
//printf("scanning...\n");
}
}
//now we had datetime. we can scan the command and the path now
fscanf_s(fp, "%s", command, 295); // get the command
char* path = new char[300];
path[0] = '\0';
//search for datetime, starting ", then loop until ending "
cnt = 0;
chr = 0;
fgetc(fp); //get th ewhitespoace after the command
chr = fgetc(fp);//read in the first ", or at least try it
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
path[cnt] = chr;
path[cnt + 1] = '\0';
cnt++;
}
}
//now get the taskname
char* taskname = new char[300];
taskname[0] = '\0';
fscanf_s(fp, "%s", taskname, 295); // get the taskname
fgetc(fp); //get th ewhitespoace after the command
chr = fgetc(fp);//read in the first ", or at least try it
//lets check if the command should be executed
//get the current time
std::time_t t = std::time(nullptr);
struct std::tm current_time;
localtime_s(&current_time, &t);
//printf("%s\n", datetime);
if (strcmp(datetime, "") != 0 && strcmp(command, "") != 0 && strcmp(path, "") != 0 && strcmp(taskname, "") != 0) {
if (is_task_due(taskname,datetime, current_time)) {
//printf("command:%s\n", command);
//printf("path:%s\n", path);
//now we can build up the command for the queue
char* queue_entry = new char[300 * 2 + 5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry, 600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
strcat_s(queue_entry, 600, path); //add the path
queue_push(queue_entry);
delete[] queue_entry;
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
path[cnt] = chr;
path[cnt + 1] = '\0';
cnt++;
}
}
//now get the taskname
char* taskname = new char[300];
taskname[0] = '\0';
fscanf_s(fp, "%s", taskname, 295); // get the taskname
fgetc(fp); //get the newline
//lets check if the command should be executed
//get the current time
std::time_t t = std::time(nullptr);
struct std::tm current_time;
localtime_s(&current_time, &t);
//printf("%s\n", datetime);
if (strcmp(datetime, "") != 0 && strcmp(command, "") != 0 && strcmp(path, "") != 0 && strcmp(taskname, "") != 0) {
if (is_task_due(taskname, datetime, current_time)) {
//printf("command:%s\n", command);
//printf("path:%s\n", path);
//now we can build up the command for the queue
char* queue_entry = new char[300 * 2 + 5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry, 600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
strcat_s(queue_entry, 600, path); //add the path
queue_push(queue_entry);
delete[] queue_entry;
}
}
//else {
// printf("datetime:%s\n", datetime);
// printf("command:%s\n", command);
// printf("path:%s\n", path);
// printf("taskname:%s\n", taskname);
// log(LOGLEVEL::ERR, "[check_for_sched_tasks()]: Error reading schedule file: ", sched_path, " while checking for scheduled tasks (malformat); aborting");
//}
delete[] datetime;
delete[] path;
delete[] taskname;
}
else {
log(LOGLEVEL::ERR, "[check_for_sched_tasks()]: Error reading schedule file: ", sched_path, " while checking for scheduled tasks (malformat); aborting");
}
delete[] datetime;
delete[] path;
delete[] taskname;
}
}
delete[] command;

View File

@@ -21,8 +21,9 @@ int load_settings() {
return 1;
}
else {
char*settings_cmd=new char[300];
char*settings_cmd=new char[505];
char*settings_arg= new char[300];
fscanf_s(fp, "%s", settings_cmd, 500); //the cert is always the firs tline
while (!feof(fp)) {
fscanf_s(fp, "%s", settings_cmd, 295); // get the command
//now check which setting it is.
@@ -95,7 +96,8 @@ void load_included_folders() {
return;
}
else {
char* path = new char[300];
char* path = new char[505];
fscanf_s(fp, "%s", path, 500); //the cert is always the firs tline
while (!feof(fp)) {
//get the path of an excluded folder
path[0] = '\0';
@@ -136,7 +138,8 @@ void load_excluded_folders() {
return;
}
else {
char* path = new char[300];
char* path = new char[505];
fscanf_s(fp, "%s", path,500); //the cert is always the firs tline
while (!feof(fp)) {
//get the path of an excluded folder
path[0] = '\0';

View File

@@ -5,6 +5,7 @@
#include "well_known.h"
#include "scan.h"
#include "app_ctrl.h"
#include "update.h"
void split(char* input,const char delimiter, char* out1, char* out2) {
//split a string at the delimiter. the delimiter only occurs once. so the first part is out1 and the second part is out2
int i = 0;
@@ -48,6 +49,11 @@ int start_thread(const char* command) {
std::thread t1(action_scanfolder, out2);
t1.detach();
}
else if (strcmp(out1, "update_settings") == 0) {
//start a new thread with the scanfolder function
std::thread t1(action_update_settings);
t1.detach();
}
while (can_run_thread()) {
//delay a bit, in order to wait until the thread is started

View File

@@ -53,7 +53,7 @@ int update_settings(const char*settings_type) {
int res = 1;
if(strcmp(settings_type,"settings")==0)
res = download_file_from_srv(url, SETTINGS_DB);
else if (strcmp(settings_type, "rtp_inlcuded") == 0)
else if (strcmp(settings_type, "rtp_included") == 0)
res = download_file_from_srv(url, INCLUDED_FOLDERS);
else if (strcmp(settings_type, "rtp_excluded") == 0)
res = download_file_from_srv(url, EXCLUDED_FOLDERS);
@@ -68,4 +68,24 @@ int update_settings(const char*settings_type) {
delete[] url;
return 0;
}
int action_update_settings() {
//update the settings
int err = 0;
if (update_settings("settings") != 0) {
err= 1;
}
//update the included folders
if (update_settings("rtp_included") != 0) {
err= 2;
}
//update the excluded folders
if (update_settings("rtp_excluded") != 0) {
err= 3;
}
//update the schedule
if (update_settings("sched") != 0) {
err= 4;
}
return err;
}
#endif

View File

@@ -5,4 +5,5 @@
#include<string>
int update_db(const std::string& folder_path);
int update_settings(const char*settings_type);
int action_update_settings();
#endif

View File

@@ -1,3 +1,3 @@
 Quellen werden auf Modulabhängigkeiten überprüft...
update.cpp
local_schedule.cpp
client_backend.vcxproj -> C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe

View File

@@ -424,6 +424,7 @@ function load_settings(){
<option value="choose_action">Choose an action</option>
<option value="scanfile">scanfile</option>
<option value="scanfolder">scanfolder</option>
<option value="update_settings">update settings</option>
</select>
</td>
<td><input type="text" id="task_argument" class="form-control" name="task_argument"></td>