title: WorkManager start Worker twice
body: <p>I have a set of big tasks which I want to execute in background:</p>
  
  <ol>
  <li>Loding data</li>
  <li>Parsing bunch of files and storing them in <code>Room</code></li>
  </ol>
  
  <p>For this reason I created unique chain of <code>Worker</code>'s with the same <code>tag</code>.</p>
  
  <pre><code>class GtfsStaticManager() {
      private val workerManager = WorkManager.getInstance()
  
      override fun load() {
          val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
  
          val inputData = GtfsStaticLoadDataWorker.inputData(staticUrl, cacheDir)
          // 1. Loading data
          val downloadWorkRequest = OneTimeWorkRequest.Builder(GtfsStaticLoadDataWorker::class.java)
              .addTag("GTFS")
              .setConstraints(constraints)
              .setInputData(inputData)
              .build()
  
          // 2. List of Workers to parse and store data to the Room
          val parseWorkers = GtfsFile.values().map {
              OneTimeWorkRequest.Builder(GtfsStaticParseFileWorker::class.java)
                  .setInputData(GtfsStaticParseFileWorker.inputData(it.file, cacheDir + File.separator + "feed"))
                  .addTag("GTFS")
                  .build()
          }
  
          workerManager
              .beginUniqueWork("GTFS", ExistingWorkPolicy.KEEP, downloadWorkRequest)
              .then(parseWorkers)
              .enqueue()
      }
  }
  </code></pre>
  
  <p>Everything works fine except one thing: one of the files has 4 million records and it takes around 10-15 minutes to finish it. After some time I noticed that it was enqueued again <strong>BUT</strong> the first job was still running, so as result I have 2 huge jobs running in background and of course my data was duplicated.</p>
  
  <p>I followed <a href="https://codelabs.developers.google.com/codelabs/android-workmanager-kt/#0" rel="nofollow noreferrer">codelabs</a> tutorial, did I miss something?</p>
  
  <p>Below is my <code>Worker</code> with parsing logic:</p>
  
  <pre><code>class GtfsStaticParseFileWorker(
      context: Context,
      workerParameters: WorkerParameters
  ) : Worker(context, workerParameters) {
      private val fileName: String get() = inputData.getString(FILE_NAME) ?: ""
      private val cacheDir: String get() = inputData.getString(UNZIP_FOLDER) ?: ""
  
      companion object {
          private const val FILE_NAME = "FILE_NAME"
          private const val UNZIP_FOLDER = "UNZIP_FOLDER"
          fun inputData(fileName: String, cacheDir: String) = Data
              .Builder()
              .putString(FILE_NAME, fileName)
              .putString(UNZIP_FOLDER, cacheDir)
              .build()
      }
  
      override fun doWork(): Result {
          val db = LvivTransportTrackerDataBase.getUpdateInstance(applicationContext)
          val agencyRepository = AgencyRepository(db.agencyDao())
          val calendarRepository = CalendarRepository(db.calendarDao())
          val calendarDateRepository = CalendarDateRepository(db.calendarDateDao())
          val routeRepository = RouteRepository(db.routeDao())
          val stopTimeRepository = StopTimeRepository(db.stopTimeDao())
          val stopRepository = StopRepository(db.stopDao())
          val tripRepository = TripRepository(db.tripDao())
  
          val file = File(cacheDir + File.separator + fileName)
          val fileType = GtfsFile.from(fileName) ?: return Result.failure()
  
          when (fileType) {
              GtfsFile.Agency -&gt; agencyRepository.deleteAll()
              GtfsFile.CalendarDates -&gt; calendarDateRepository.deleteAll()
              GtfsFile.Calendar -&gt; calendarRepository.deleteAll()
              GtfsFile.Routes -&gt; routeRepository.deleteAll()
              GtfsFile.StopTimes -&gt; stopTimeRepository.deleteAll()
              GtfsFile.Stops -&gt; stopRepository.deleteAll()
              GtfsFile.Trips -&gt; tripRepository.deleteAll()
          }
  
          FileInputStream(file).use { fileInputStream -&gt;
              InputStreamReader(fileInputStream).use inputStreamReader@{ inputStreamReader -&gt;
                  val bufferedReader = BufferedReader(inputStreamReader)
                  val headers = bufferedReader.readLine()?.split(',') ?: return@inputStreamReader
                  var line: String? = bufferedReader.readLine()
  
                  while (line != null) {
                      val mapLine = headers.zip(line.split(',')).toMap()
  
                      Log.d("GtfsStaticParse", "$fileType: $line")
                      when (fileType) {
                          GtfsFile.Agency -&gt; agencyRepository.create(AgencyEntity(mapLine))
                          GtfsFile.CalendarDates -&gt; calendarDateRepository.create(CalendarDateEntity(mapLine))
                          GtfsFile.Calendar -&gt; calendarRepository.create(CalendarEntity(mapLine))
                          GtfsFile.Routes -&gt; routeRepository.create(RouteEntity(mapLine))
                          GtfsFile.StopTimes -&gt; stopTimeRepository.create(StopTimeEntity(mapLine))
                          GtfsFile.Stops -&gt; stopRepository.create(StopEntity(mapLine))
                          GtfsFile.Trips -&gt; tripRepository.create(TripEntity(mapLine))
                      }
  
                      line = bufferedReader.readLine()
                  }
              }
          }
  
          return Result.success()
      }
  }
  </code></pre>
  
  <p>P.S. my dependency is <code>implementation "android.arch.work:work-runtime:1.0.0"</code></p>