Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MaarchParapheur
Manage
Activity
Members
Plan
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
MaarchParapheur
Commits
d80e0c39
Commit
d80e0c39
authored
3 years ago
by
Guillaume Heurtier
Browse files
Options
Downloads
Patches
Plain Diff
FEAT #17163 TIME 4:30 create/delete scheduler script in crontab
parent
2f384aef
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lang/fr.json
+3
-1
3 additions, 1 deletion
lang/fr.json
rest/index.php
+8
-5
8 additions, 5 deletions
rest/index.php
src/app/notifications/controllers/NotificationsScheduleController.php
+138
-0
138 additions, 0 deletions
...fications/controllers/NotificationsScheduleController.php
with
149 additions
and
6 deletions
lang/fr.json
+
3
−
1
View file @
d80e0c39
...
@@ -594,6 +594,8 @@
...
@@ -594,6 +594,8 @@
"periodicNotification"
:
"Notification(s) périodique(s)"
,
"periodicNotification"
:
"Notification(s) périodique(s)"
,
"batchMasterNotInitialized"
:
"Le planificateur des notifications n'a pas été <b>initialisé</b> ! Les notifications ne pourront pas être envoyé."
,
"batchMasterNotInitialized"
:
"Le planificateur des notifications n'a pas été <b>initialisé</b> ! Les notifications ne pourront pas être envoyé."
,
"initializeBatchMaster"
:
"Initialiser le planificateur"
,
"initializeBatchMaster"
:
"Initialiser le planificateur"
,
"noScheduledNotif"
:
"Aucune notification périodique n'est disponible."
"noScheduledNotif"
:
"Aucune notification périodique n'est disponible."
,
"schedulerScriptCreated"
:
"Planificateur créé"
,
"schedulerScriptDeleted"
:
"Planificateur supprimé"
}
}
}
}
This diff is collapsed.
Click to expand it.
rest/index.php
+
8
−
5
View file @
d80e0c39
...
@@ -162,10 +162,13 @@ $app->get('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateContr
...
@@ -162,10 +162,13 @@ $app->get('/workflowTemplates/{id}', \Workflow\controllers\WorkflowTemplateContr
$app
->
delete
(
'/workflowTemplates/{id}'
,
\Workflow\controllers\WorkflowTemplateController
::
class
.
':delete'
);
$app
->
delete
(
'/workflowTemplates/{id}'
,
\Workflow\controllers\WorkflowTemplateController
::
class
.
':delete'
);
//NotificationsSchedule
//NotificationsSchedule
$app
->
post
(
'/schedule'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':create'
);
$app
->
post
(
'/schedule/initialization'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':initializeSchedulerScript'
);
$app
->
get
(
'/schedule'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':get'
);
$app
->
get
(
'/schedule/initialization'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':checkSchedulerScriptInitialized'
);
$app
->
get
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':getById'
);
$app
->
delete
(
'/schedule'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':deleteSchedulerScript'
);
$app
->
put
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':update'
);
$app
->
post
(
'/schedule'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':create'
);
$app
->
delete
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':delete'
);
$app
->
get
(
'/schedule'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':get'
);
$app
->
get
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':getById'
);
$app
->
put
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':update'
);
$app
->
delete
(
'/schedule/{id}'
,
\Notification\controllers\NotificationsScheduleController
::
class
.
':delete'
);
$app
->
run
();
$app
->
run
();
This diff is collapsed.
Click to expand it.
src/app/notifications/controllers/NotificationsScheduleController.php
+
138
−
0
View file @
d80e0c39
...
@@ -14,12 +14,15 @@
...
@@ -14,12 +14,15 @@
namespace
Notification\controllers
;
namespace
Notification\controllers
;
use
Configuration\models\ConfigurationModel
;
use
Notification\models\NotificationsScheduleModel
;
use
Notification\models\NotificationsScheduleModel
;
use
Group\controllers\PrivilegeController
;
use
Group\controllers\PrivilegeController
;
use
History\controllers\HistoryController
;
use
History\controllers\HistoryController
;
use
Respect\Validation\Validator
;
use
Respect\Validation\Validator
;
use
Slim\Http\Request
;
use
Slim\Http\Request
;
use
Slim\Http\Response
;
use
Slim\Http\Response
;
use
SrcCore\models\CoreConfigModel
;
use
SrcCore\models\ValidatorModel
;
class
NotificationsScheduleController
class
NotificationsScheduleController
{
{
...
@@ -214,6 +217,141 @@ class NotificationsScheduleController
...
@@ -214,6 +217,141 @@ class NotificationsScheduleController
return
$response
->
withStatus
(
204
);
return
$response
->
withStatus
(
204
);
}
}
public
function
initializeSchedulerScript
(
Request
$request
,
Response
$response
)
{
if
(
!
PrivilegeController
::
hasPrivilege
([
'userId'
=>
$GLOBALS
[
'id'
],
'privilege'
=>
'manage_notifications'
]))
{
return
$response
->
withStatus
(
403
)
->
withJson
([
'errors'
=>
'Privilege forbidden'
]);
}
$configuration
=
ConfigurationModel
::
getByIdentifier
([
'identifier'
=>
'customization'
]);
$configuration
=
$configuration
[
0
];
$configuration
[
'value'
]
=
json_decode
(
$configuration
[
'value'
],
true
);
if
(
empty
(
$configuration
[
'value'
][
'schedulerPath'
])
||
!
is_file
(
$configuration
[
'value'
][
'schedulerPath'
]))
{
$schedulerScript
=
NotificationsScheduleController
::
createSchedulerScript
();
$configuration
[
'value'
][
'schedulerPath'
]
=
$schedulerScript
[
'path'
];
}
// Check if script already in crontab
exec
(
'crontab -l|grep '
.
$configuration
[
'value'
][
'schedulerPath'
],
$output
);
if
(
empty
(
$output
))
{
// run scheduler every minutes
$crontabLine
=
'*/1 * * * * '
.
$configuration
[
'value'
][
'schedulerPath'
];
exec
(
'(crontab -l; echo "'
.
$crontabLine
.
'")|crontab -'
);
HistoryController
::
add
([
'code'
=>
'OK'
,
'objectType'
=>
'configurations'
,
'objectId'
=>
$configuration
[
'id'
],
'type'
=>
'MODIFICATION'
,
'message'
=>
"
{
schedulerScriptCreated
}
:
{
$configuration
[
'value'
][
'schedulerPath'
]
}
"
]);
}
return
$response
->
withStatus
(
204
);
}
public
function
checkSchedulerScriptInitialized
(
Request
$request
,
Response
$response
)
{
if
(
!
PrivilegeController
::
hasPrivilege
([
'userId'
=>
$GLOBALS
[
'id'
],
'privilege'
=>
'manage_notifications'
]))
{
return
$response
->
withStatus
(
403
)
->
withJson
([
'errors'
=>
'Privilege forbidden'
]);
}
$configuration
=
ConfigurationModel
::
getByIdentifier
([
'identifier'
=>
'customization'
]);
$configuration
=
$configuration
[
0
];
$configuration
=
json_decode
(
$configuration
[
'value'
],
true
);
if
(
empty
(
$configuration
[
'schedulerPath'
])
||
!
is_file
(
$configuration
[
'schedulerPath'
]))
{
return
$response
->
withStatus
(
400
)
->
withJson
([
'errors'
=>
'No scheduler created'
]);
}
exec
(
'crontab -l|grep '
.
$configuration
[
'schedulerPath'
],
$output
);
if
(
empty
(
$output
))
{
return
$response
->
withStatus
(
400
)
->
withJson
([
'errors'
=>
'Scheduler not set in crontab'
]);
}
return
$response
->
withStatus
(
204
);
}
public
function
deleteSchedulerScript
(
Request
$request
,
Response
$response
)
{
if
(
!
PrivilegeController
::
hasPrivilege
([
'userId'
=>
$GLOBALS
[
'id'
],
'privilege'
=>
'manage_notifications'
]))
{
return
$response
->
withStatus
(
403
)
->
withJson
([
'errors'
=>
'Privilege forbidden'
]);
}
$configuration
=
ConfigurationModel
::
getByIdentifier
([
'identifier'
=>
'customization'
]);
$configuration
=
$configuration
[
0
];
$configuration
[
'value'
]
=
json_decode
(
$configuration
[
'value'
],
true
);
if
(
empty
(
$configuration
[
'value'
][
'schedulerPath'
]))
{
return
$response
->
withStatus
(
204
);
}
if
(
is_file
(
$configuration
[
'value'
][
'schedulerPath'
]))
{
unlink
(
$configuration
[
'value'
][
'schedulerPath'
]);
}
exec
(
'crontab -l|grep '
.
$configuration
[
'value'
][
'schedulerPath'
],
$output
);
if
(
empty
(
$output
))
{
return
$response
->
withStatus
(
204
);
}
exec
(
'(crontab -l| grep -v "'
.
$configuration
[
'value'
][
'schedulerPath'
]
.
'")|crontab -'
);
$configuration
[
'value'
][
'schedulerPath'
]
=
''
;
$configuration
[
'value'
]
=
json_encode
(
$configuration
[
'value'
]);
ConfigurationModel
::
update
([
'set'
=>
[
'value'
=>
$configuration
[
'value'
]],
'where'
=>
[
'identifier = ?'
],
'data'
=>
[
'customization'
]]);
HistoryController
::
add
([
'code'
=>
'OK'
,
'objectType'
=>
'configurations'
,
'objectId'
=>
$configuration
[
'id'
],
'type'
=>
'SUPPRESSION'
,
'message'
=>
"
{
schedulerScriptDeleted
}
:
{
$configuration
[
'value'
][
'schedulerPath'
]
}
"
]);
return
$response
->
withStatus
(
204
);
}
private
static
function
createSchedulerScript
()
{
$filename
=
'scheduler_'
.
rand
()
.
'.sh'
;
$corePath
=
str_replace
(
'src/app/notifications/controllers'
,
''
,
__DIR__
);
$corePath
=
str_replace
(
'src/app/notification/models'
,
''
,
$corePath
);
$configPath
=
CoreConfigModel
::
getConfigPath
();
$fullPath
=
$corePath
.
'bin/'
.
$filename
;
$file_open
=
fopen
(
$fullPath
,
'w+'
);
fwrite
(
$file_open
,
'#!/bin/sh'
);
fwrite
(
$file_open
,
"
\n
"
);
fwrite
(
$file_open
,
'path=\''
.
$corePath
.
'\''
);
fwrite
(
$file_open
,
"
\n
"
);
fwrite
(
$file_open
,
'cd $path'
);
fwrite
(
$file_open
,
"
\n
"
);
fwrite
(
$file_open
,
'filePath=\''
.
$corePath
.
'bin/scheduler.php\''
);
fwrite
(
$file_open
,
"
\n
"
);
fwrite
(
$file_open
,
'php $filePath '
.
$configPath
);
fwrite
(
$file_open
,
"
\n
"
);
fclose
(
$file_open
);
shell_exec
(
'chmod +x '
.
escapeshellarg
(
$fullPath
));
$configuration
=
ConfigurationModel
::
getByIdentifier
([
'identifier'
=>
'customization'
]);
$configuration
=
$configuration
[
0
];
$configuration
[
'value'
]
=
json_decode
(
$configuration
[
'value'
],
true
);
$configuration
[
'value'
][
'schedulerPath'
]
=
$fullPath
;
$configuration
[
'value'
]
=
json_encode
(
$configuration
[
'value'
]);
ConfigurationModel
::
update
([
'set'
=>
[
'value'
=>
$configuration
[
'value'
]],
'where'
=>
[
'id = ?'
],
'data'
=>
[
$configuration
[
'id'
]]]);
return
[
'path'
=>
$fullPath
];
}
private
static
function
validateBody
(
$body
)
private
static
function
validateBody
(
$body
)
{
{
if
(
empty
(
$body
))
{
if
(
empty
(
$body
))
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment