Ejemplo n.º 1
0
        public async Task <object> ExecuteLongTask(LongTask task, bool allowCancellation = false)
        {
            p_Progress progress = new p_Progress();

            progress.Cancellable = allowCancellation;

            p_Document document = MainFrame.Content as p_Document;

            if (document != null)
            {
                document.Header.IsMenuEnabled = false;
            }
            try
            {
                AnimatedNavigate(MiddleLayerFrame, progress, NavigationAnimation.FadeToLeft, true);
                var result = await task(progress, progress.CancellationToken);

                return(result);
            } finally
            {
                if (document != null)
                {
                    document.Header.IsMenuEnabled = true;
                }
            }
        }
Ejemplo n.º 2
0
        void OnFileCreated(string storage, long file)
        {
            string mimetype;
            string filename;
            long   rev;

            Storage.GetDownloadFileInfo(storage, file, out mimetype, out filename, out rev);

            if (mimetype.StartsWith("video/"))
            {
                lock (instanceLock) {
                    if (!runningTasks.ContainsKey(storage + ":" + file + ":mp4") &&
                        !File.Exists(basePath + "/" + storage + "/mp4/" + file))
                    {
                        LongTask task = new LongTask(delegate {
                            BuildMp4(storage, file);
                        }, null, "Build MP4 for " + storage + ":" + file);
                        longRunningTaskScheduler.Start(task);
                        runningTasks[storage + ":" + file + ":mp4"] = task;
                    }
                    if (!runningTasks.ContainsKey(storage + ":" + file + ":webm") &&
                        !File.Exists(basePath + "/" + storage + "/webm/" + file))
                    {
                        LongTask task = new LongTask(delegate {
                            BuildWebm(storage, file);
                        }, null, "Build WEBM for " + storage + ":" + file, LongTaskPriority.Low);
                        longRunningTaskScheduler.Start(task);
                        runningTasks[storage + ":" + file + ":webm"] = task;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        void OnFileCreated(string storage, long file)
        {
            string mimetype;
            string filename;
            long   rev;

            Storage.GetDownloadFileInfo(storage, file, out mimetype, out filename, out rev);

            if (mimetype.StartsWith("audio/"))
            {
                lock (instanceLock) {
                    if (!runningTasks.ContainsKey(storage + ":" + file + ":mp3") &&
                        !File.Exists(basePath + "/" + storage + "/mp3/" + file))
                    {
                        LongTask task = new LongTask(delegate {
                            BuildMp3(storage, file);
                        }, null, "Build MP3 " + storage + ":" + file);
                        longRunningTaskScheduler.Start(task);
                        runningTasks[storage + ":" + file + ":mp3"] = task;
                    }
                    if (!runningTasks.ContainsKey(storage + ":" + file + ":ogg") &&
                        !File.Exists(basePath + "/" + storage + "/ogg/" + file))
                    {
                        LongTask task = new LongTask(delegate {
                            BuildOgg(storage, file);
                        }, null, "Build OGG " + storage + ":" + file, LongTaskPriority.Low);

                        longRunningTaskScheduler.Start(task);
                        runningTasks[storage + ":" + file + ":ogg"] = task;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public AudioEncoder ScheduleEncode(Audio audio)
        {
            AudioEncoder encoder = null;

            lock (instanceLock)
            {
                lock (instanceLock)
                {
                    if (!runningEncoders.ContainsKey(audio.node.id))
                    {
                        encoder = new AudioEncoder(this, audio);
                        LongTask task = new LongTask(() => {
                            encoder.Run();
                            lock (instanceLock)
                            {
                                runningEncoders.Remove(encoder.Audio.node.id);
                                runningEncodersById.Remove(encoder.Id);
                            }
                        }, audio.node.owner, $"Build MP3 for '{audio.node.name}' (id: {audio.node.id})");
                        scheduler.Start(task);
                        runningEncoders[audio.node.id]  = encoder;
                        runningEncodersById[encoder.Id] = encoder;
                    }
                    else
                    {
                        encoder = runningEncoders[audio.node.id];
                    }
                }
            }
            return(encoder);
        }
Ejemplo n.º 5
0
 public void InteractWithNoble(Noble noble)
 {
     if (noble.currentWish == HoldableObject.LETTER)
     {
         DeactivateAllSprites();
         longTask    = new LongTask(letter, HoldableObject.LETTER, UnityEngine.Random.Range(1.0f, 2.0f));
         objectOwner = noble;
         progressBarBack.SetActive(true);
     }
     else if (objectInHand == noble.currentWish && noble.currentWish != HoldableObject.LETTER)
     {
         Debug.Log("Je suis content ! Merci");
         DeactivateAllSprites();
         objectInHand = HoldableObject.NONE;
         longTask     = null;
         noble.FulfillWish();
     }
     else if (objectInHand == HoldableObject.POISONED_TEA)
     {
         Debug.Log("AaAahHh ! C'est du poison !! Salaud !!");
         DeactivateAllSprites();
         objectInHand = HoldableObject.NONE;
         longTask     = null;
         noble.Poison(this);
     }
     else
     {
         Debug.Log("Gardes, débarassez moi de cet incompétent !");
         DeactivateAllSprites();
         objectInHand = HoldableObject.NONE;
         noble.FailWish();
         longTask = null;
     }
 }
Ejemplo n.º 6
0
        public VideoEncoder ScheduleEncode(Video video)
        {
            VideoEncoder encoder = null;

            lock (instanceLock)
            {
                lock (instanceLock)
                {
                    if (!runningEncoders.ContainsKey(video.node.id))
                    {
                        encoder = new VideoEncoder(this, video);
                        LongTask task = new LongTask(() => {
                            encoder.Run();
                            lock (instanceLock)
                            {
                                runningEncoders.Remove(encoder.Video.node.id);
                                runningEncodersById.Remove(encoder.Id);
                            }
                        }, video.node.owner, $"Build MP4 for '{video.node.name}' (id: {video.node.id})");
                        scheduler.Start(task);
                        runningEncoders[video.node.id]  = encoder;
                        runningEncodersById[encoder.Id] = encoder;
                    }
                    else
                    {
                        encoder = runningEncoders[video.node.id];
                    }
                }
            }
            return(encoder);
        }
Ejemplo n.º 7
0
        private async void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            var i = await LongTask.StartLongTask();

            textBox1.Text   = "Resultados es " + i;
            button1.Enabled = true;
        }
Ejemplo n.º 8
0
 public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
 {
     Task.Run(() =>
     {
         var counter = new LongTask();
         counter.RunCounter().Wait();
     });
     return StartCommandResult.Sticky;
 }
        public void then_long_task_is_not_completed_directly_after_begin()
        {
            const int seconds = 2;

            LongTask lt = new LongTask(seconds); // Prove that the Wait-until-done technique works
            IAsyncResult ar = lt.BeginDoTask(null, null);
            ar = lt.BeginDoTask(null, null);
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            Assert.False(ar.IsCompleted);
        }
        public void then_long_task_is_not_completed_directly_after_begin()
        {
            const int seconds = 2;

            LongTask     lt = new LongTask(seconds);         // Prove that the Wait-until-done technique works
            IAsyncResult ar = lt.BeginDoTask(null, null);

            ar = lt.BeginDoTask(null, null);
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            Assert.False(ar.IsCompleted);
        }
        public void then_long_task_is_completed_at_correct_time()
        {
            const int seconds = 2;

            LongTask lt = new LongTask(seconds); // Prove that the Wait-until-done technique works
            IAsyncResult ar = lt.BeginDoTask(null, null);
            ar = lt.BeginDoTask(null, null);
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            while (!ar.IsCompleted)
            {
                Thread.Sleep(1000);
            }
            Assert.InRange(stopwatch.Elapsed.TotalSeconds, seconds, int.MaxValue);
        }
        public void then_long_task_is_completed_at_correct_time()
        {
            const int seconds = 2;

            LongTask     lt = new LongTask(seconds);         // Prove that the Wait-until-done technique works
            IAsyncResult ar = lt.BeginDoTask(null, null);

            ar = lt.BeginDoTask(null, null);
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            while (!ar.IsCompleted)
            {
                Thread.Sleep(1000);
            }
            Assert.InRange(stopwatch.Elapsed.TotalSeconds, seconds, int.MaxValue);
        }
		public void ContiniousTasksCancelledOnDelayTest()
		{
			LongTask longTask = new LongTask();
			Assert.AreEqual(LongTaskState.NotStarted, longTask.State);

			Task task = longTask.StartTask();
			Thread.Sleep(500);
			Assert.AreEqual(LongTaskState.Started, longTask.State);

			longTask.Cancel();
			Thread.Sleep(500);
			Assert.AreEqual(LongTaskState.Started, longTask.State);

			if (!task.IsCompleted)
			{
				task.Wait();
			}
		}
		public void ContiniousTasksCompletionTest()
		{
			LongTask longTask = new LongTask();
			Assert.AreEqual(LongTaskState.NotStarted, longTask.State);

			Task task = longTask.StartTask();
			Thread.Sleep(500);
			Assert.AreEqual(LongTaskState.Started, longTask.State);

			Thread.Sleep(600);
			Assert.AreEqual(LongTaskState.InAction, longTask.State);

			Thread.Sleep(1100);
			Assert.AreEqual(LongTaskState.Completed, longTask.State);

			if (!task.IsCompleted)
			{
				task.Wait();
			}
		}
Ejemplo n.º 15
0
 public void QueuePdfGenerate(string storage, long file)
 {
     lock (instanceLock) {
         if (!runningTasks.ContainsKey(storage + ":" + file))
         {
             LongTask task = new LongTask(delegate {
                 try {
                     PdfGenerate(storage, file);
                 } finally {
                     // remove the task
                     lock (instanceLock) {
                         if (runningTasks.ContainsKey(storage + ":" + file))
                         {
                             runningTasks.Remove(storage + ":" + file);
                         }
                     }
                 }
             }, null, "Build PDF " + storage + ":" + file);
             longRunningTaskScheduler.Start(task);
             runningTasks[storage + ":" + file] = task;
         }
     }
 }
Ejemplo n.º 16
0
    public void HandleClicOnSpecialTile(WorldTile wt)
    {
        switch (wt.tileType)
        {
        case TileType.WELL:
            longTask = new LongTask(waterBucket, HoldableObject.WATER_BUCKET, UnityEngine.Random.Range(2.0f, 4.0f));
            DeactivateAllSprites();
            progressBarBack.SetActive(true);
            break;

        case TileType.LARDER:
            longTask = new LongTask(chicken, HoldableObject.CHICKEN, UnityEngine.Random.Range(1.5f, 3.0f));
            DeactivateAllSprites();
            progressBarBack.SetActive(true);
            break;

        case TileType.KITCHEN:
            switch (objectInHand)
            {
            case HoldableObject.WATER_BUCKET:
                DeactivateAllSprites();
                longTask = new LongTask(waterBucket, teaPot, HoldableObject.TEA_POT, UnityEngine.Random.Range(2.0f, 5.0f));
                progressBarBack.SetActive(true);
                break;

            case HoldableObject.CHICKEN:
                DeactivateAllSprites();
                longTask = new LongTask(chicken, cookedChicken, HoldableObject.COOKED_CHICKEN, UnityEngine.Random.Range(2.0f, 5.0f));
                progressBarBack.SetActive(true);
                break;
            }
            break;

        case TileType.POISONED_BUSH:
            if (objectInHand == HoldableObject.WATER_BUCKET)
            {
                DeactivateAllSprites();
                longTask = new LongTask(waterBucket, poisonedTea, HoldableObject.POISONED_TEA, UnityEngine.Random.Range(2.0f, 5.0f));
                progressBarBack.SetActive(true);
            }
            break;

        case TileType.THRONE:
            switch (objectInHand)
            {
            case HoldableObject.NONE:
                Debug.Log("Pas possible, y'a rien à donner");
                break;

            default:
                objectInHand = HoldableObject.NONE;
                DeactivateAllSprites();
                break;
            }
            break;

        case TileType.STABLE:
            switch (objectInHand)
            {
            case HoldableObject.LETTER:
                objectInHand = HoldableObject.NONE;
                objectOwner.FulfillWish();
                DeactivateAllSprites();
                break;
            }
            break;
        }
    }
Ejemplo n.º 17
0
        public void BeginExecuting_WhenJobAlreadyRunning_ShouldDoNothing()
        {
            var longTask = new LongTask
                           	{
                           		Seconds = 30
                           	};

            const string resultId = "addition/1";
            var task1 = new AdditionTask
                            {
                                FirstNubmer = 2,
                                SecondNumber = 2,
                                ResultId = resultId
                            };

            _taskManager.Save(longTask);
            _target.BeginExecuting();

            _taskManager.Save(task1);
            _target.BeginExecuting();

            Store.WaitForEntityToExist(resultId,10);

            var result = Store.Load<ComputationResult<int>>(resultId);
            result.Should().BeNull();
        }
Ejemplo n.º 18
0
        public override void ProcessRequest(HttpContext context)
        {
            string[] parts = context.Request.Path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
            long     file  = 0;

            // GET /[storage]/[file] get audio content
            if ((context.Request.Method == "GET") && (parts.Length == 2) && long.TryParse(parts[1], out file))
            {
                string storage = parts[0];
                string format  = "mp3";
                if (context.Request.QueryString.ContainsKey("format"))
                {
                    format = context.Request.QueryString["format"];
                }
                // if format is not set choose a format supported by the user agent
                else if (context.Request.Headers.ContainsKey("user-agent"))
                {
                    if ((context.Request.Headers["user-agent"].IndexOf("Firefox/") >= 0) ||
                        (context.Request.Headers["user-agent"].IndexOf("Opera/") >= 0))
                    {
                        format = "ogg";
                    }
                }
                string mimetype = "audio/mpeg";
                if (format == "ogg")
                {
                    mimetype = "audio/ogg";
                }
                string fileName = Path.Combine(basePath, storage, format, file.ToString());
                if (File.Exists(fileName))
                {
                    context.Response.StatusCode = 200;
                    context.Response.Headers["content-type"]  = mimetype;
                    context.Response.Headers["cache-control"] = "max-age=" + cacheDuration;
                    context.Response.SupportRanges            = true;
                    context.Response.Content = new FileContent(fileName);
                }
                else
                {
                    context.Response.StatusCode = 404;
                    context.Response.Content    = new StringContent("File not found\r\n");
                }
            }
            // GET /[storage]/[file]/info get audio info
            else if ((context.Request.Method == "GET") && (parts.Length == 3) && long.TryParse(parts[1], out file) && (parts[2] == "info"))
            {
                string    storage = parts[0];
                JsonValue json    = new JsonObject();
                json["storage"] = storage;
                json["file"]    = file;
                string format = "mp3";
                if (context.Request.Headers.ContainsKey("user-agent"))
                {
                    if ((context.Request.Headers["user-agent"].IndexOf("Firefox/") >= 0) ||
                        (context.Request.Headers["user-agent"].IndexOf("Opera/") >= 0))
                    {
                        format = "ogg";
                    }
                }
                json["support"] = format;
                JsonValue status = new JsonObject();
                json["status"] = status;

                lock (instanceLock) {
                    if (File.Exists(Path.Combine(basePath, storage, "mp3", file.ToString())))
                    {
                        status["mp3"] = "ready";
                    }
                    else
                    {
                        if (runningTasks.ContainsKey(storage + ":" + file + ":mp3"))
                        {
                            status["mp3"] = "building";
                        }
                        else
                        {
                            LongTask task = new LongTask(delegate {
                                BuildMp3(storage, file);
                            }, null, "Build MP3 " + storage + ":" + file);
                            longRunningTaskScheduler.Start(task);
                            runningTasks[storage + ":" + file + ":mp3"] = task;
                            status["mp3"] = "building";
                        }
                    }
                    if (File.Exists(Path.Combine(basePath, storage, "ogg", file.ToString())))
                    {
                        status["ogg"] = "ready";
                    }
                    else
                    {
                        if (runningTasks.ContainsKey(storage + ":" + file + ":ogg"))
                        {
                            status["ogg"] = "building";
                        }
                        else
                        {
                            LongTask task = new LongTask(delegate {
                                BuildOgg(storage, file);
                            }, null, "Build OGG " + storage + ":" + file, LongTaskPriority.Low);
                            longRunningTaskScheduler.Start(task);
                            runningTasks[storage + ":" + file + ":ogg"] = task;
                            status["ogg"] = "building";
                        }
                    }
                }

                context.Response.StatusCode = 200;
                context.Response.Content    = new JsonContent(json);
            }
        }
		public void ContiniousTasksCreationTest()
		{
			LongTask longTask = new LongTask();
			Assert.AreEqual(LongTaskState.NotStarted, longTask.State);
		}
Ejemplo n.º 20
0
        public override void ProcessRequest(HttpContext context)
        {
            string[] parts = context.Request.Path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
            long     id    = 0;
            long     id2   = 0;

            // GET /[storage]/[file] get video content
            if ((context.Request.Method == "GET") && (parts.Length == 2) && long.TryParse(parts[1], out id2))
            {
                string storage = parts[0];
                string format  = "mp4";
                if (context.Request.QueryString.ContainsKey("format"))
                {
                    format = context.Request.QueryString["format"];
                }
                // if format is not set choose a format supported by the user agent
                else if (context.Request.Headers.ContainsKey("user-agent"))
                {
                    if ((context.Request.Headers["user-agent"].IndexOf("Firefox/") >= 0) ||
                        (context.Request.Headers["user-agent"].IndexOf("Opera/") >= 0))
                    {
                        format = "webm";
                    }
                }
                string mimetype = "video/mp4";
                if (format == "webm")
                {
                    mimetype = "video/webm";
                }
                string fileName = basePath + "/" + storage + "/" + format + "/" + id2;
                if (File.Exists(fileName))
                {
                    context.Response.StatusCode = 200;
                    context.Response.Headers["content-type"]  = mimetype;
                    context.Response.Headers["cache-control"] = "max-age=" + cacheDuration;
                    context.Response.SupportRanges            = true;
                    context.Response.Content = new FileContent(fileName);
                }
                else
                {
                    context.Response.StatusCode = 404;
                    context.Response.Content    = new StringContent("File not found\r\n");
                }
            }
            // GET /[storage]/[file]/info get video info
            else if ((context.Request.Method == "GET") && (parts.Length == 3) && long.TryParse(parts[1], out id2) && (parts[2] == "info"))
            {
                string    storage = parts[0];
                long      file    = id2;
                JsonValue json    = new JsonObject();
                json["storage"] = storage;
                json["file"]    = file;
                string format = "mp4";
                if (context.Request.Headers.ContainsKey("user-agent"))
                {
                    if ((context.Request.Headers["user-agent"].IndexOf("Firefox/") >= 0) ||
                        (context.Request.Headers["user-agent"].IndexOf("Opera/") >= 0))
                    {
                        format = "webm";
                    }
                }
                json["support"] = format;
                JsonValue status = new JsonObject();
                json["status"] = status;

                //Console.WriteLine("video info storage: " + storage + ", file: " + file);

                if (File.Exists(basePath + "/" + storage + "/mp4/" + file.ToString()))
                {
                    status["mp4"] = "ready";
                }
                else
                {
                    lock (instanceLock) {
                        if (runningTasks.ContainsKey(storage + ":" + file.ToString() + ":mp4"))
                        {
                            status["mp4"] = "building";
                        }
                        else
                        {
                            LongTask task = new LongTask(delegate {
                                BuildMp4(storage, file);
                            }, context.User, "Build MP4 for " + storage + ":" + file);
                            longRunningTaskScheduler.Start(task);
                            runningTasks[storage + ":" + file.ToString() + ":mp4"] = task;
                            status["mp4"] = "building";
                        }
                    }
                }

                if (File.Exists(basePath + "/" + storage + "/webm/" + file.ToString()))
                {
                    status["webm"] = "ready";
                }
                else
                {
                    lock (instanceLock) {
                        if (runningTasks.ContainsKey(storage + ":" + file.ToString() + ":webm"))
                        {
                            status["webm"] = "building";
                        }
                        else
                        {
                            LongTask task = new LongTask(delegate {
                                BuildWebm(storage, file);
                            }, context.User, "Build WEBM for " + storage + ":" + file, LongTaskPriority.Low);
                            longRunningTaskScheduler.Start(task);
                            runningTasks[storage + ":" + file.ToString() + ":webm"] = task;
                            status["webm"] = "building";
                        }
                    }
                }
                context.Response.StatusCode = 200;
                context.Response.Content    = new JsonContent(json);
                context.SendResponse();
            }
        }
Ejemplo n.º 21
0
        public void BeginExecuting_WhenMaxNumberOfParallelJobsIsExecutong_ShouldNotStartANewJpb()
        {
            var task1 = new LongTask { Seconds = 10 };
            var task2 = new LongTask { Seconds = 10 };
            var task3 = new LongTask { Seconds = 10 };

            const string resultId = "addition/1";
            var task4 = new AdditionTask
            {
                FirstNubmer = 2,
                SecondNumber = 2,
                ResultId = resultId
            };

            _taskManager.Save(task1);
            _target.BeginExecuting();

            _taskManager.Save(task2);
            _target.BeginExecuting();

            _taskManager.Save(task3);
            _target.BeginExecuting();

            _taskManager.Save(task4);
            _target.BeginExecuting();

            Store.WaitForEntityToExist(resultId, 10);

            var result = Store.Load<ComputationResult<int>>(resultId);
            result.Result.Should().Be(4);
        }