Пример #1
0
 public void HandleTask()
 {
     while (!canStop)
     {
         TaskData td = processQueue.Consume();
         //td cannot be null
         ContentProcessResult cpr = Process(td);
         InvokeTaskProcessDoneEvent(this, new TaskProcessDoneEventHandlerArgs
         {
             ContentProcessResult = cpr,
             ProxyInfo            = td.ProxyInfo,
             Task = td.Task
         });
     }
 }
Пример #2
0
        protected void DaemonEnqueue()
        {
            List <Task> inserts = new List <Task>();

            while (!canStop)
            {
                EnqueueTaskInfo eti;
                if (inserts.Count == 0)
                {
                    eti = enqueueQueue.Consume();
                }
                else
                {
                    if (!enqueueQueue.TryConsume(out eti))
                    {
                        //submit inserts
                        try
                        {
                            InsertTask(inserts, TaskState.NOT_FINISHED);
                            inserts.Clear();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        continue;
                    }
                    else
                    {
                        if (eti.TaskState != TaskState.NOT_FINISHED || inserts.Count > 10)
                        {
                            try
                            {
                                InsertTask(inserts, TaskState.NOT_FINISHED);
                                inserts.Clear();
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                            inserts.Clear();
                        }
                    }
                }


                Task task = eti.Task;

                switch (eti.TaskState)
                {
                case TaskState.NOT_FINISHED:
                {
//							if (taskQueue.Count >= BUFFER_SIZE)
//							{
                    if (task.TaskId != 0)
                    {
                        try
                        {
                            UpdateTask(task, TaskState.NOT_FINISHED);
                        }
                        catch (Exception e)
                        {
                            enqueueQueue.Produce(eti);
                            Console.WriteLine(e);
                        }
                    }
                    else
                    {
                        inserts.Add(eti.Task);
                    }
//							}
//							else
//							{
////								taskQueue.Produce(task);
//								//save to database
//								inserts.Add(eti.Task);
//							}

                    break;
                }

                case TaskState.FINISHED:
                {
                    try
                    {
                        UpsertTask(task, TaskState.FINISHED);
                    }
                    catch (Exception e)
                    {
                        enqueueQueue.Produce(eti);
                        Console.WriteLine(e);
                    }
                    break;
                }

                case TaskState.FAILED:
                {
                    task.FailTimes++;
                    if (task.FailTimes < MAX_FAIL_TIMES)
                    {
                        //re-enqueue
                        if (taskQueue.Count >= BUFFER_SIZE)
                        {
                            try
                            {
                                UpsertTask(task, TaskState.NOT_FINISHED);
                            }
                            catch (Exception e)
                            {
                                enqueueQueue.Produce(eti);
                                Console.WriteLine(e);
                            }
                        }
                        else
                        {
                            TaskQueue.Produce(task);
                        }
                    }
                    else
                    {
                        try
                        {
                            UpsertTask(task, TaskState.FAILED);
                        }
                        catch (Exception e)
                        {
                            enqueueQueue.Produce(eti);
                            Console.WriteLine(e);
                        }
                    }
                    break;
                }

                case TaskState.IN_MEMORY:
                    throw new ArgumentOutOfRangeException();

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }