1
0
Fork 0

Only run create-plans script once per day on hera

This commit is contained in:
Malte Brandy 2022-03-24 00:12:57 +01:00
parent 771904f67b
commit ce6d608100
3 changed files with 48 additions and 44 deletions

View file

@ -162,5 +162,6 @@ in {
./roles/mail2rss.nix
./roles/headless-mpd.nix
./roles/headless.nix
./roles/create-plans.nix
]);
}

View file

@ -0,0 +1,47 @@
{pkgs, ...}: let
plans = pkgs.privateValue
{
"workDay" = "pass";
"weekend" = "pass";
}
"plans";
createPlans = pkgs.writeHaskellScript
{
name = "create-plans";
bins = [pkgs.khal pkgs.vdirsyncer];
imports = [
"Data.Time"
];
} ''
main = do
today <- localDay . zonedTimeToLocalTime <$> getZonedTime
[0..7] & fmap (`addDays` today) & mapM_ \day -> do
planned <- khal ["list", "-a", "Planung", show day, "06:00", "24h", "--notstarted"] |> captureTrim
when (planned == "No events") $ do
say $ "Creating events for " <> show day
if (dayOfWeek day `elem` [Saturday, Sunday]) then do
${plans.weekend}
else do
${plans.workDay}
'';
in {
systemd.user = {
services.create-plans = {
Unit.Description = "Create planning appointments in calendar";
Service = {
Type = "oneshot";
ExecStart = toString (pkgs.writeShellScript "update-plans" ''
set -e
${pkgs.vdirsyncer}/bin/vdirsyncer sync nextcloud_calendar/planung
${createPlans}/bin/create-plans
${pkgs.vdirsyncer}/bin/vdirsyncer sync nextcloud_calendar/planung
'');
};
};
timers.create-plans = {
Unit.Description = "Create planning appointments in calendar";
Timer.OnCalendar = "00:01:00";
Install.WantedBy = ["timers.target"];
};
};
}

View file

@ -1,30 +1,5 @@
{pkgs, ...}: let
calendars = pkgs.privateValue [] "calendars";
plans = pkgs.privateValue
{
"workDay" = "pass";
"weekend" = "pass";
}
"plans";
createPlans = pkgs.writeHaskellScript
{
name = "create-plans";
bins = [pkgs.khal pkgs.vdirsyncer];
imports = [
"Data.Time"
];
} ''
main = do
today <- localDay . zonedTimeToLocalTime <$> getZonedTime
[0..7] & fmap (`addDays` today) & mapM_ \day -> do
planned <- khal ["list", "-a", "Planung", show day, "06:00", "24h", "--notstarted"] |> captureTrim
when (planned == "No events") $ do
say $ "Creating events for " <> show day
if (dayOfWeek day `elem` [Saturday, Sunday]) then do
${plans.weekend}
else do
${plans.workDay}
'';
ui = pkgs.writeShellScriptBin "calendar" ''
ikhal -d Serien
'';
@ -57,23 +32,4 @@ in {
calendars
}
'';
systemd.user = {
services.create-plans = {
Unit.Description = "Create planning appointments in calendar";
Service = {
Type = "oneshot";
ExecStart = toString (pkgs.writeShellScript "update-plans" ''
set -e
${pkgs.vdirsyncer}/bin/vdirsyncer sync nextcloud_calendar/planung
${createPlans}/bin/create-plans
${pkgs.vdirsyncer}/bin/vdirsyncer sync nextcloud_calendar/planung
'');
};
};
timers.create-plans = {
Unit.Description = "Create planning appointments in calendar";
Timer.OnCalendar = "hourly";
Install.WantedBy = ["timers.target"];
};
};
}