add configable cronjob expressions

This commit is contained in:
stubbfel
2018-04-01 00:02:01 +02:00
parent cbb5761199
commit 7c53f8dd20
5 changed files with 170 additions and 126 deletions

View File

@@ -22,32 +22,49 @@ mailsend -smtp ${cfg.smtp.address} -port ${portString} -from $FROMADDR -to $TOAD
sleep 60
'';
callConvertScripts =
reps = mapAttrsToList (name: value:
let
makeCallLine = recipient:
map (makeCallLineRecipe recipient.mail) recipient.recipes;
makeCallLineRecipe = recipientMail: recipe:
if isNull recipe.content then
"bash ${convertScript} ${recipe.name} ${calibreWithRecipes}/var/news2kindle/recipes ${cfg.mobiPath} ${cfg.fromMail} ${recipientMail}\n"
else
''
mkdir -p /tmp/news2kindle/recipes
echo "${recipe.content}" > /tmp/news2kindle/recipes/${recipe.name}.recipe
bash ${convertScript} ${recipe.name} /tmp/news2kindle/recipes/ ${cfg.mobiPath} ${cfg.fromMail} ${recipientMail}
'';
mailAddress = if value.mailAddress != null
then value.mailAddress
else name;
cronJobs = mapAttrsToList ( jobExpr: jobValue:
let
cronExpression = if jobValue.cronExpression != "0 5 * * *"
then jobValue.cronExpression
else jobExpr;
in
jobValue // {cronExpression = cronExpression;}) value.cronJobs;
in
concatMap makeCallLine cfg.recipients;
value // { mailAddress = name; cronJobs = cronJobs; }
) cfg.recipients;
jobs = concatMap(recipient: concatMap(cronjob:
let
scriptName = builtins.replaceStrings ["*"] ["star"] (builtins.replaceStrings [" "] ["_"] (builtins.replaceStrings ["@"] [""] "${recipient.mailAddress}-${cronjob.cronExpression}.sh"));
scriptlines = concatMapStrings(recipe:
if isNull recipe.content then
"bash ${convertScript} ${recipe.name} ${calibreWithRecipes}/var/news2kindle/recipes ${cfg.mobiPath} ${recipient.fromMail} ${recipient.mailAddress}\n"
else
''
mkdir -p /tmp/news2kindle/recipes
echo "${recipe.content}" > /tmp/news2kindle/recipes/${recipe.name}.recipe
bash ${convertScript} ${recipe.name} /tmp/news2kindle/recipes/ ${cfg.mobiPath} ${recipient.fromMail} ${recipient.mailAddress}
'' ) cronjob.recipes;
script = pkgs.writeText "${scriptName}" ''#!/bin/bash
rm -f ${cfg.mobiPath}/*.mobi
mkdir -p ${cfg.mobiPath}
${scriptlines}
'';
in
["${cronjob.cronExpression} root bash ${script}"]) recipient.cronJobs) reps;
dailyUpdateScript = pkgs.writeText "dailyUpdates.sh" ''#!/bin/bash
rm -f ${cfg.mobiPath}/*.mobi
mkdir -p ${cfg.mobiPath}
${concatStrings callConvertScripts}
'';
odfpyNoTest = pkgs.python2Packages.odfpy.overrideAttrs (oldAttrs: rec {
doInstallCheck = false;
});
calibreWithRecipes = pkgs.calibre.overrideAttrs (oldAttrs: rec {
installPhase = ''
mkdir -p $out/var/news2kindle/recipes
@@ -108,49 +125,7 @@ in
recipients = mkOption {
type = with types; listOf (submodule {
options = {
mail = mkOption {
type = types.str;
default = null;
description = ''
name of recipe
'';
};
recipes = mkOption {
type = with types; listOf (submodule {
options = {
name = mkOption {
type = types.str;
default = null;
description = ''
name of recipe
'';
};
content = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
content of the recipe file, when its null the use the builtins recipes
'';
};
};
});
default = [];
example = [ { name = "heise";} { name = "Postillon"; port = ''
class BasicUserRecipe1414943033(AutomaticNewsRecipe):
title = u'Postillon'
oldest_article = 7
max_articles_per_feed = 100
auto_cleanup = True
feeds= [(u'Postilion', u'http://feeds.feedburner.com/blogspot/rkEL')] ''; } ];
description = ''
List of recipes wich the user wants fetch. If recipe content is not specified the fetcher uses builtins recipes
'';
};
};
});
default = [];
type = types.attrsOf (types.submodule (import ./recipient-options.nix { inherit config lib; }));
};
smtp = {
@@ -190,6 +165,6 @@ in
config = mkIf cfg.enable{
environment.systemPackages = [pkgs.python27Packages.pyqt5 pkgs.python36Packages.pyqt5 pkgs.mailsend calibreWithRecipes];
services.cron.enable = true;
services.cron.systemCronJobs = [ "0 5 * * * root bash ${dailyUpdateScript}"];
services.cron.systemCronJobs = jobs;
};
}