capability to run as service

This commit is contained in:
skilion 2015-11-29 21:12:44 +01:00
parent 3f40728779
commit ea970890a2
5 changed files with 47 additions and 12 deletions

View file

@ -27,8 +27,10 @@ clean:
install: onedrive onedrive.conf
install onedrive $(DESTDIR)
install onedrive.conf $(CONFDIR)
install -m 644 onedrive.conf $(CONFDIR)
install -m 644 onedrive.service /usr/lib/systemd/user
uninstall:
rm -f $(DESTDIR)/onedrive
rm -f $(CONFDIR)/onedrive.conf
rm -f /usr/lib/systemd/user/onedrive.service

10
onedrive.service Normal file
View file

@ -0,0 +1,10 @@
[Unit]
Description=OneDrive Free Client
Documentation=https://github.com/skilion/onedrive
[Service]
ExecStart=/usr/local/bin/onedrive -m
Restart=no
[Install]
WantedBy=default.target

View file

@ -1,6 +1,6 @@
import core.memory, core.time, core.thread;
import std.getopt, std.file, std.path, std.process, std.stdio;
import config, itemdb, monitor, onedrive, sync;
import config, itemdb, monitor, onedrive, sync, util;
void main(string[] args)
@ -50,6 +50,11 @@ void main(string[] args)
auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path);
if (verbose) writeln("Initializing the OneDrive API ...");
bool online = testNetwork();
if (!online && !monitor) {
writeln("No network connection");
return;
}
auto onedrive = new OneDriveApi(cfg, verbose);
onedrive.onRefreshToken = (string refreshToken) {
std.file.write(refreshTokenFilePath, refreshToken);
@ -60,7 +65,6 @@ void main(string[] args)
} catch (FileException e) {
onedrive.authorize();
}
// TODO check if the token is valid
if (verbose) writeln("Opening the item database ...");
auto itemdb = new ItemDatabase(databaseFilePath);
@ -81,7 +85,7 @@ void main(string[] args)
// swallow exception
}
sync.init(statusToken);
performSync(sync);
if (online) performSync(sync);
if (monitor) {
if (verbose) writeln("Initializing monitor ...");
@ -123,13 +127,16 @@ void main(string[] args)
immutable auto checkInterval = dur!"seconds"(45);
auto lastCheckTime = MonoTime.currTime();
while (true) {
m.update();
m.update(online);
auto currTime = MonoTime.currTime();
if (currTime - lastCheckTime > checkInterval) {
lastCheckTime = currTime;
performSync(sync);
// discard all events that may have been generated by the sync
m.update(false);
online = testNetwork();
if (online) {
performSync(sync);
// discard all events that may have been generated by the sync
m.update(false);
}
GC.collect();
} else {
Thread.sleep(dur!"msecs"(100));

View file

@ -63,7 +63,6 @@ final class OneDriveApi
void setRefreshToken(string refreshToken)
{
this.refreshToken = refreshToken;
newToken();
}
// https://dev.onedrive.com/items/view_changes.htm

View file

@ -1,11 +1,16 @@
import std.conv, std.digest.crc, std.digest.digest, std.file, std.path;
import std.regex, std.stdio, std.string: chomp;
import std.conv;
import std.digest.crc;
import std.file;
import std.path;
import std.regex;
import std.socket;
import std.stdio;
import std.string;
private string deviceName;
static this()
{
import std.socket;
deviceName = Socket.hostName;
}
@ -66,3 +71,15 @@ Regex!char wild2regex(const(char)[] pattern)
str ~= "$";
return regex(str, "i");
}
// return true if the network connection is available
bool testNetwork()
{
try {
auto addr = new InternetAddress("login.live.com", 443);
auto socket = new TcpSocket(addr);
return socket.isAlive();
} catch (SocketException) {
return false;
}
}