Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lockfile

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lockfile

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.

  • 0.4.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is lockfile?

The lockfile npm package is used to create and manage lock files, which are useful for ensuring that only one instance of a process is running at a time. This can be particularly useful in scenarios where you want to prevent race conditions or ensure that a resource is not accessed concurrently by multiple processes.

What are lockfile's main functionalities?

Creating a Lockfile

This feature allows you to create a lockfile to ensure that only one instance of a process is running. The code sample demonstrates how to acquire a lock with specific options and release it after the work is done.

const lockFile = require('lockfile');

const options = {
  wait: 10000, // Wait for 10 seconds
  pollPeriod: 100, // Check every 100ms
  stale: 30000, // Consider the lock stale after 30 seconds
  retries: 3, // Retry 3 times
  retryWait: 100 // Wait 100ms between retries
};

lockFile.lock('path/to/file.lock', options, function (err) {
  if (err) {
    console.error('Failed to acquire lock:', err);
  } else {
    console.log('Lock acquired');
    // Do some work
    lockFile.unlock('path/to/file.lock', function (err) {
      if (err) {
        console.error('Failed to release lock:', err);
      } else {
        console.log('Lock released');
      }
    });
  }
});

Checking Lockfile Status

This feature allows you to check if a lockfile is currently active. The code sample demonstrates how to check the status of a lockfile and handle the result.

const lockFile = require('lockfile');

lockFile.check('path/to/file.lock', function (err, isLocked) {
  if (err) {
    console.error('Error checking lock status:', err);
  } else if (isLocked) {
    console.log('File is locked');
  } else {
    console.log('File is not locked');
  }
});

Updating Lockfile Options

This feature allows you to update the options for a lockfile, such as the stale time. The code sample demonstrates how to acquire a lock with updated options and release it after the work is done.

const lockFile = require('lockfile');

const options = {
  stale: 60000 // Consider the lock stale after 60 seconds
};

lockFile.lock('path/to/file.lock', options, function (err) {
  if (err) {
    console.error('Failed to acquire lock:', err);
  } else {
    console.log('Lock acquired with updated options');
    // Do some work
    lockFile.unlock('path/to/file.lock', function (err) {
      if (err) {
        console.error('Failed to release lock:', err);
      } else {
        console.log('Lock released');
      }
    });
  }
});

Other packages similar to lockfile

Keywords

FAQs

Package last updated on 16 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc