Exemple #1
0
        public void POST(string key, RequestInfo info)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                string[] args;
                using (var sr = new StreamReader(info.Request.Body, System.Text.Encoding.UTF8, true))
                    args = Newtonsoft.Json.JsonConvert.DeserializeObject <string[]>(sr.ReadToEnd());

                var k = new ActiveRun();
                k.Writer = new LogWriter(k);

                m_activeItems[k.ID] = k;
                StartCleanupTask();

                k.Task = Runner.CreateCustomTask((sink) =>
                {
                    try
                    {
                        k.Thread  = System.Threading.Thread.CurrentThread;
                        k.Started = true;

                        var code = Duplicati.CommandLine.Program.RunCommandLine(k.Writer, k.Writer, c => {
                            k.Task.SetController(c);
                            c.AppendSink(sink);
                        }, args);
                        k.Writer.WriteLine(string.Format("Return code: {0}", code));
                    }
                    catch (Exception ex)
                    {
                        var rx = ex;
                        if (rx is System.Reflection.TargetInvocationException)
                        {
                            rx = rx.InnerException;
                        }

                        if (rx is Library.Interface.UserInformationException)
                        {
                            k.Log.Add(rx.Message);
                        }
                        else
                        {
                            k.Log.Add(rx.ToString());
                        }

                        throw rx;
                    }
                    finally
                    {
                        k.Finished = true;
                        k.Thread   = null;
                    }
                });

                Program.WorkThread.AddTask(k.Task);

                info.OutputOK(new
                {
                    ID = k.ID
                });
            }
            else
            {
                if (!key.EndsWith("/abort", StringComparison.OrdinalIgnoreCase))
                {
                    info.ReportClientError("Only abort commands are allowed");
                    return;
                }

                key = key.Substring(0, key.Length - "/abort".Length);
                if (string.IsNullOrWhiteSpace(key))
                {
                    info.ReportClientError("No task key found");
                    return;
                }

                ActiveRun t;
                if (!m_activeItems.TryGetValue(key, out t))
                {
                    info.OutputError(code: System.Net.HttpStatusCode.NotFound);
                    return;
                }

                var tt = t.Task;
                if (tt != null)
                {
                    tt.Abort();
                }

                var tr = t.Thread;
                if (tr != null)
                {
                    tr.Abort();
                }

                info.OutputOK();
            }
        }
Exemple #2
0
 public LogWriter(ActiveRun target)
 {
     m_target = target;
 }