Skip to content

Commit

Permalink
Send chunks of tasks for transform
Browse files Browse the repository at this point in the history
  • Loading branch information
piascikj committed Mar 19, 2018
1 parent 4cc9be4 commit 4b99117
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 120 deletions.
71 changes: 24 additions & 47 deletions lib/services/imdoneio-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -306,59 +306,36 @@ class ImdoneioClient extends Emitter
next()
(next) =>
tasks = @getTransformableTasks tasks
@doPost("/transform").send({config, tasks, utcOffset: moment().format()}).end (err, res) =>
chunks = _.chunk tasks, 20
modifiedTasks = []
total = 0
log "Sending #{tasks.length} tasks to imdone.io"
async.eachOfLimit chunks, 2, (chunk, i, cb) =>
log "Sending chunk #{i}:#{chunks.length} of #{chunk.length} tasks to imdone.io"
setTimeout () => # Not sure why, but this sometimes hangs without using setTimeout
@doPost("/transform").send({config, tasks: chunk, utcOffset: moment().format()}).end (err, res) =>
#console.log "Received Sync Response #{i} err:#{err}"
if err && err.code == 'ECONNREFUSED' && @authenticated
#console.log "Error on syncing tasks with imdone.io", err
@emit 'unavailable'
delete @authenticated
delete @user
return cb err if err
data = res.body
modifiedTasks.push data
total += data.length
log "Received #{i}:#{chunks.length} #{total} tasks from imdone.io"
cb()
log "Chunk #{i}:#{chunks.length} of #{chunk.length} tasks sent to imdone.io"
,10
, (err) ->
return next err if err
next(null, res.body)
next err, _.flatten modifiedTasks
], (err, result) =>
return cb err if err
cb(null, result[1].map (task) => new Task(task))
)

syncTasks: (repo, tasks, cb) ->
gitRepo = helper.repoForPath repo.getPath()
projectId = @getProjectId repo
chunks = _.chunk tasks, 20
modifiedTasks = []
total = 0
log "Sending #{tasks.length} tasks to imdone.io"
repo.emit 'sync.percent', 0
async.eachOfLimit chunks, 2, (chunk, i, cb) =>
log "Sending chunk #{i}:#{chunks.length} of #{chunk.length} tasks to imdone.io"
data =
tasks: chunk
branch: gitRepo && gitRepo.branch
setTimeout () => # Not sure why, but this sometimes hangs without using setTimeout
@doPost("/projects/#{projectId}/tasks").send(data).end (err, res) =>
#console.log "Received Sync Response #{i} err:#{err}"
if err && err.code == 'ECONNREFUSED' && @authenticated
#console.log "Error on syncing tasks with imdone.io", err
@emit 'unavailable'
delete @authenticated
delete @user
return cb err if err
data = res.body
modifiedTasks.push data
total += data.length
repo.emit 'sync.percent', Math.ceil(total/tasks.length*100)
log "Received #{i}:#{chunks.length} #{total} tasks from imdone.io"
cb()
log "Chunk #{i}:#{chunks.length} of #{chunk.length} tasks sent to imdone.io"
,10
, (err) ->
return cb err if err
cb err, _.flatten modifiedTasks

syncTasksForDelete: (repo, tasks, cb) ->
projectId = @getProjectId repo
taskIds = _.map tasks, (task) -> task.meta.id[0]
@doPost("/projects/#{projectId}/taskIds").send(taskIds: taskIds).end (err, res) =>
if err && err.code == 'ECONNREFUSED' && @authenticated
@emit 'unavailable'
delete @authenticated
delete @user
return cb err if err
cb err, res.body

db: (collection) ->
path = require 'path'
collection = path.join.apply @, arguments if arguments.length > 1
Expand Down
67 changes: 0 additions & 67 deletions lib/services/imdoneio-store.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ module.exports = (repo) ->
repo.setProjectName project.name
return unless repo.isImdoneIOProject()
_.set repo, 'sync.sort', project.taskOrder if sortEnabled()
# repo.syncTasks repo.getTasks(), (err, done) =>
repo.emit 'project.found', project
repo.initProducts()
# done err if done

checkForIIOProject() if client.isAuthenticated()
repo.on 'authenticated', => checkForIIOProject()
Expand Down Expand Up @@ -108,53 +106,6 @@ module.exports = (repo) ->
return if err == ERRORS.NO_CONTENT
throw err if err

repo.syncTasks = syncTasks = (tasks, cb) ->
return cb("unauthenticated", ()->) unless client.isAuthenticated()
return cb("not enabled") unless repo.getProjectId()
tasks = [tasks] unless _.isArray tasks
return cb() unless tasks.length > 0

cm.emit 'tasks.syncing'
#console.log "sending #{tasks.length} tasks to imdone-io "
client.syncTasks repo, tasks, (err, ioTasks) ->
return if err # TODO: Do something with this error gh:116 id:81
#console.log "received tasks from imdone-io:", ioTasks
async.eachSeries ioTasks,
(task, cb) ->
currentTask = repo.getTask task.id
taskToModify = _.assign currentTask, task
return cb "Task not found" unless Task.isTask taskToModify
repo.modifyTask taskToModify, cb
(err) ->
if err
#console.log "Sync Error:", err
return cm.emit 'sync.error', err
repo.saveModifiedFiles (err, files)->
client.syncTasksForDelete repo, repo.getTasks(), (err, deletedTasks) ->
return syncDone(tasks)(err) unless cb
cb err, syncDone(tasks)

syncFile = (file, cb) ->
return cb("unauthenticated", ()->) unless client.isAuthenticated()
return cb("not enabled") unless repo.getProjectId()
cm.emit 'tasks.syncing'
#console.log "sending tasks to imdone-io for: #{file.path}"
client.syncTasks repo, file.getTasks(), (err, tasks) ->
return if err # TODO: Do something with this error gh:116 id:96
#console.log "received tasks from imdone-io for: %s", tasks
async.eachSeries tasks,
(task, cb) ->
taskToModify = _.assign repo.getTask(task.id), task
return cb "Task not found" unless Task.isTask taskToModify
repo.modifyTask taskToModify, cb
(err) ->
return cm.emit 'sync.error', err if err
repo.writeFile file, (err, fileAfterSave)->
file ?= fileAfterSave
client.syncTasksForDelete repo, file.getTasks(), (err, deletedTasks) ->
return syncDone(tasks)(err) unless cb
cb err, syncDone(tasks)

loadSort = (cb) ->
loadSortFile cb

Expand Down Expand Up @@ -240,16 +191,6 @@ module.exports = (repo) ->
_moveTasks tasks, newList, newPos, shouldSync, (err, tasksByList) ->
repo.emit 'tasks.moved', tasks
return cb err if err
# if shouldSync
#
# #console.log "Tasks moved. Syncing with imdone.io"
# syncTasks tasks, (err, done) ->
# repo.emit 'tasks.moved', tasks
# return cb null, tasksByList unless sortEnabled()
# saveSort (err) ->
# done err
# cb err, tasksByList
# else
return cb null, tasksByList unless sortEnabled()
saveSort (err) -> cb err, tasksByList

Expand All @@ -263,14 +204,6 @@ module.exports = (repo) ->
return tasksByList unless sortEnabled()
({name: list.name, tasks: sortBySyncId(list.name, list.tasks)} for list in tasksByList)

# repo.emitFileUpdate = (file) ->
# return _emitFileUpdate file unless client.isAuthenticated() && repo.project
# if repo.shouldEmitFileUpdate file
# syncFile file, (err, done) ->
# _emitFileUpdate file
# done err
#

repo.init = (cb) ->
cb ?= ()->
fns = [
Expand Down
5 changes: 1 addition & 4 deletions lib/services/worker-watched-fs-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ function mixin(repo, fs) {
var _refresh = repo.refresh;
let refreshing = false;
repo.refresh = function(cb) {
// console.log('refresh called')
if (refreshing) {
if (cb) return cb()
return
}
// console.log('running refresh')
refreshing = true
emitter.once('refresh', function() {
// console.log('received refresh')
_refresh.call(repo, function(err, files) {
repo.reminders.listen();
repo.initWatcher();
Expand Down Expand Up @@ -95,7 +92,7 @@ function mixin(repo, fs) {
},
addDir: function(path) {log('Directory', path, 'has been added');},
change: function(path) {
console.log(`received a change for ${path}`)
log(`received a change for ${path}`)
log("Watcher received change event for file: " + path);
var relPath = repo.getRelativePath(path);
var file = repo.getFile(relPath) || relPath;
Expand Down
2 changes: 0 additions & 2 deletions lib/views/imdone-atom-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ class ImdoneAtomView extends ScrollView
@emitter.on 'error', (mdMsg) => atom.notifications.addWarning "OOPS!", description: mdMsg, dismissable: true, icon: 'alert'

@emitter.on 'task.modified', (task) => @onRepoUpdate()
#console.log "Task modified. Syncing with imdone.io"
# @imdoneRepo.syncTasks [task], (err) => @onRepoUpdate()

@emitter.on 'menu.toggle', =>
@boardWrapper.toggleClass 'shift'
Expand Down

0 comments on commit 4b99117

Please sign in to comment.