Ejemplo n.º 1
0
        // アイテムのProfileNameからプロファイルを決定して、
        // オプションでwaits!=nullのときはクライアントに通知
        // 戻り値: プロファイルが変更された場合(結果、エラーになった場合も含む)
        private bool UpdateProfileItem(QueueItem item, List <Task> waits)
        {
            var getResult = server.GetProfile(item, item.ProfileName);
            var profile   = (getResult != null) ? ServerSupport.DeepCopy(getResult.Profile) : server.PendingProfile;
            var priority  = (getResult != null && getResult.Priority > 0) ? getResult.Priority : item.Priority;

            if (item.Profile == null ||
                item.Profile.Name != profile.Name ||
                item.Profile.LastUpdate != profile.LastUpdate ||
                item.Priority != priority)
            {
                // 変更
                item.Profile  = profile;
                item.Priority = priority;

                // ハッシュリスト取得
                if (profile != server.PendingProfile && // ペンディングの場合は決定したときに実行される
                    item.IsSeparateHashRequired)
                {
                    var hashpath = Path.GetDirectoryName(item.SrcPath) + ".hash";
                    if (hashCache.ContainsKey(hashpath) == false)
                    {
                        if (File.Exists(hashpath) == false)
                        {
                            item.State      = QueueState.LogoPending;
                            item.FailReason = "ハッシュファイルがありません: " + hashpath;
                            return(true);
                        }
                        else
                        {
                            try
                            {
                                hashCache.Add(hashpath, new DirHash()
                                {
                                    DirPath  = hashpath,
                                    HashDict = HashUtil.ReadHashFile(hashpath)
                                });
                            }
                            catch (IOException e)
                            {
                                item.State      = QueueState.LogoPending;
                                item.FailReason = "ハッシュファイルの読み込みに失敗: " + e.Message;
                                return(true);
                            }
                        }
                    }

                    var cacheItem = hashCache[hashpath];
                    var filename  = item.FileName;

                    if (cacheItem.HashDict.ContainsKey(filename) == false)
                    {
                        item.State      = QueueState.LogoPending;
                        item.FailReason = "ハッシュファイルにこのファイルのハッシュがありません";
                        return(true);
                    }

                    item.Hash = cacheItem.HashDict[filename];
                }

                server.ReScheduleQueue();
                UpdateQueueItem(item, waits);

                waits?.Add(ClientQueueUpdate(new QueueUpdate()
                {
                    Type = UpdateType.Add,
                    Item = item
                }));

                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private async Task StartEncode()
        {
            NowEncoding = true;

            // 待たなくてもいいタスクリスト
            var waitList = new List <Task>();

            // 状態を更新
            waitList.Add(RequestState());

            try
            {
                while (queue.Count > 0)
                {
                    // 不正な設定は強制的に直しちゃう
                    if (appData.setting.NumParallel <= 0 ||
                        appData.setting.NumParallel > 64)
                    {
                        appData.setting.NumParallel = 1;
                    }

                    int numParallel = appData.setting.NumParallel;

                    // 足りない場合は追加
                    while (taskList.Count < numParallel)
                    {
                        taskList.Add(new EncodeTask()
                        {
                            id          = taskList.Count,
                            consoleText = new ConsoleText(500),
                            logText     = new ConsoleText(1 * 1024 * 1024)
                        });
                    }
                    // 多すぎる場合は削除
                    while (taskList.Count > numParallel)
                    {
                        taskList.RemoveAt(taskList.Count - 1);
                    }

                    affinityCreator.NumProcess = numParallel;

                    var dir = queue[0];

                    Dictionary <string, byte[]> hashList = null;
                    if (dir.Path.StartsWith("\\\\"))
                    {
                        var hashpath = dir.Path + ".hash";
                        if (File.Exists(hashpath) == false)
                        {
                            throw new IOException("ハッシュファイルがありません: " + hashpath + "\r\n" +
                                                  "ネットワーク経由の場合はBatchHashCheckerによるハッシュファイル生成が必須です。");
                        }
                        hashList = HashUtil.ReadHashFile(hashpath);
                    }

                    Task[] tasks = new Task[numParallel];
                    for (int i = 0; i < numParallel; ++i)
                    {
                        taskList[i].hashList = hashList;
                        taskList[i].tmpBase  = Util.CreateTmpFile(appData.setting.WorkPath);
                        tasks[i]             = taskList[i].ProcessDiretoryItem(this, dir);
                    }

                    await Task.WhenAll(tasks);

                    for (int i = 0; i < numParallel; ++i)
                    {
                        File.Delete(taskList[i].tmpBase);
                    }

                    if (encodePaused)
                    {
                        break;
                    }
                    queue.Remove(dir);
                    waitList.Add(client.OnQueueUpdate(new QueueUpdate()
                    {
                        Type    = UpdateType.Remove,
                        DirPath = dir.Path,
                    }));
                }
            }
            catch (Exception e)
            {
                waitList.Add(AddEncodeLog(
                                 "エラーでエンコードが停止しました: " + e.Message));
            }

            NowEncoding = false;

            // 状態を更新
            waitList.Add(RequestState());

            await Task.WhenAll(waitList.ToArray());
        }